码迷,mamicode.com
首页 > 其他好文 > 详细

OPENCV学习笔记16_用控制器设计模式实现功能模块间通信

时间:2017-09-22 21:18:10      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:sig   ati   else   process   构建   img   .cpp   nbsp   following   

  在构建更复杂的程序时,需要创建多个算法来协同工作,以实现一些高级功能。要合理地构建程序并让所有的类能互相通信,程序将会变得越来越复杂。因此在一个类中集中对程序进行控制,是非常有益的。这正是控制器设计模式背后的思想。

技术分享
#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_用控制器设计模式实现功能模块间通信

标签:sig   ati   else   process   构建   img   .cpp   nbsp   following   

原文地址:http://www.cnblogs.com/yunfung/p/7576975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!