1. 程式人生 > >OPENCV學習筆記16_用控制器設計模式實現功能模塊間通信

OPENCV學習筆記16_用控制器設計模式實現功能模塊間通信

.get src read require char http result div exe

  在構建更復雜的程序時,需要創建多個算法來協同工作,以實現一些高級功能。要合理地構建程序並讓所有的類能互相通信,程序將會變得越來越復雜。因此在一個類中集中對程序進行控制,是非常有益的。這正是控制器設計模式背後的思想。

技術分享
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <stdio.h>

#include "colorDetectController.h"


int main()
{
    // Create the controller
ColorDetectController controller; // To display the result namedWindow("Image"); // The following code simulate a user Interface // based on the use of a controller // Interaction with user is simply done // using key pressed cout << "q: to quit" <<endl; cout
<< "f: to input a filename" << endl; cout << "t: to input target color values" << endl; cout << "c: to input color distance threshold" << endl; cout << "v: to view the different parameter values" <<endl; cout << "r: to run" << std::endl;
char key = ; string filename; while ((key = getchar()) != q) { switch (key) { uchar r, g, b; case f: // read an image cout << endl << "Filename? "; cin >> filename; cout << endl; if (controller.setInputImage(filename)) cout << "...image successfully opened" << endl; else cout << "...cannot find image: " << filename << endl; break; case t: // input target color int ir, ig, ib; cout << endl << "Target color? "; cin >> ir >> ig >> ib; cout << endl; controller.setTargetColor(ir, ig, ib); break; case c: // input threshold int th; cout << endl << "Color distance threshold? "; cin >> th; cout << endl; controller.setColorDistanceThreshold(th); break; case v: // view the parameters cout <<endl << "Image name: " << filename << endl; controller.getTargetColour(r, g, b); cout << endl << "Target color: " << static_cast<int>(r) << "," << static_cast<int>(g) << "," << static_cast<int>(b) << endl; cout << endl << "Distance thresdhold: " << controller.getColorDistanceThreshold() << endl; cout << endl; break; case i: // show input image imshow("Image", controller.getInputImage()); waitKey(10); // for window to repaint break; case r: // run color detection controller.process(); imshow("Image", controller.getLastResult()); waitKey(10); // for window to repaint break; } } return 0; }
main.cpp 技術分享
#if !defined CD_CNTRLLR
#define CD_CNTRLLR

#include <opencv2/highgui/highgui.hpp>
#include "colordetector.h"


class ColorDetectController {

private:
    //create the classes required to execute the application
    ColorDetector *cdetect;
    //need two member variables in order to hold a reference to the input and output results
    Mat image;
    Mat result;
public:
    ColorDetectController() {
        //use a dynamic allocation for our class  setting up the application
        cdetect = new ColorDetector();                 //need release
    }

    void setColorDistanceThreshold(int distance) {
        cdetect->setColorDistanceThreshold(distance);   //-> not .
    }

    int getColorDistanceThreshold() const {
        return cdetect->getColorDistanceThreshold();
    }

    void setTargetColor(unsigned char red, unsigned char green, unsigned char blue) {
        cdetect->setTargetColor(blue, green, red);
    }

    void getTargetColour(unsigned char &red, unsigned char &green, unsigned char &blue) const {
        Vec3b colour = cdetect->getTargetColor();
        red = colour[2];
        green = colour[1];
        blue = colour[0];
    }

    bool setInputImage(std::string filename) {
        image = imread(filename);
        return !image.empty();
    }

    const Mat getInputImage() const {
        return image;
    }

    void process() {
        result = cdetect->process(image);
    }

    const cv::Mat getLastResult() const {
        return result;
    }


    // Deletes all processor objects created by the controller.
    ~ColorDetectController() {
        delete cdetect;
    }
};

#endif
colorDetectController.h 技術分享
#if !defined COLORDETECT
#define COLORDETECT

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;

class ColorDetector {
  private:
      int maxDist;        // minimum acceptable distance
      cv::Vec3b target;   // target color
      cv::Mat result;     // image containing resulting binary map
  public:
      ColorDetector() : maxDist(20), target(0,0,0){}
      int getDistanceToTargetColor(const cv::Vec3b& color) const;      //  no{ }
      int getColorDistance(const cv::Vec3b& color1, const cv::Vec3b& color2) const;
      cv::Mat process(const cv::Mat &image);
      void setColorDistanceThreshold(int distance);
      int getColorDistanceThreshold() const;
      void setTargetColor(uchar blue, uchar green, uchar red);
      void setTargetColor(cv::Vec3b color);
      cv::Vec3b getTargetColor() const;
};  // semicolons need

#endif
colordetector.h 技術分享
#include "colordetector.h"
#include <vector>


int ColorDetector::getDistanceToTargetColor(const cv::Vec3b& color) const{
    return getColorDistance(color, target);
}

int ColorDetector::getColorDistance(const cv::Vec3b& color1, const cv::Vec3b& color2) const{
    return abs(color1[0] - color2[0]) + abs(color1[1] - color2[1]) + abs(color1[2] - color2[2]);
}

void ColorDetector::setColorDistanceThreshold(int distance){
    if (distance < 0)
    distance = 0;
    maxDist = distance;
}


int ColorDetector::getColorDistanceThreshold() const {
    return maxDist;
}

void  ColorDetector::setTargetColor(uchar blue, uchar green, uchar red) {
    target = cv::Vec3b(blue, green, red);
}

void  ColorDetector::setTargetColor(cv::Vec3b color) {
    target = color;
}

cv::Vec3b ColorDetector::getTargetColor() const {
    return target;
}

cv::Mat ColorDetector::process(const cv::Mat &image) {
      result.create(image.size(),CV_8U);
      cv::Mat_<cv::Vec3b>::const_iterator it= image.begin<cv::Vec3b>();
      cv::Mat_<cv::Vec3b>::const_iterator itend= image.end<cv::Vec3b>();
      cv::Mat_<uchar>::iterator itout= result.begin<uchar>();
      for ( ; it!= itend; ++it, ++itout)
      {
          // compute distance from target color
          if (getDistanceToTargetColor(*it) < maxDist) {
              *itout= 255;
          }
          else {
              *itout= 0;
          }
      }
      return result;
}
colordetector.cpp

技術分享 技術分享

OPENCV學習筆記16_用控制器設計模式實現功能模塊間通信