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

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

#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

        

时间: 2024-08-28 21:12:07

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

odoo10学习笔记十四:mixin其他功能模块

odoo提供了许多有用的功能,比如:讨论.通知.网站等.我们可以在开发自己的模块时,引入这些功能. 一:消息系统 在模型中整合消息系统是很简单的,只需要从mail.thread继承模型并将对应的字段和widget添加到视图中就可以了. http://www.jianshu.com/p/84c6518d7dbf

Opencv学习笔记(六)SURF学习笔记

原创文章,转载请注明出处:http://blog.csdn.net/crzy_sparrow/article/details/7392345 本人挺菜的,肯定有非常多错误纰漏之处 ,希望大家不吝指正. 看了harris角点检測之后,開始研究SURF角点检測,发现挺复杂的,一时也仅仅了解了大概,把了解的东西总结下,以便下次深入学习. SURF角点检測算法是对SIFT的一种改进,主要体如今速度上,效率更高.它和SIFT的主要差别是图像多尺度空间的构建方法不同. 在计算视觉领域,尺度空间被象征性的表述

opencv学习笔记(四)投影

opencv学习笔记(四)投影 任选了一张图片用于测试,图片如下所示: 1 #include <cv.h> 2 #include <highgui.h> 3 using namespace std; 4 using namespace cv; 5 int main() 6 { 7 IplImage * src = cvLoadImage("cat.png", 0); //强制转化读取图像为灰度图 8 cvShowImage("灰度图像", s

OpenCV学习笔记(01)我的第一个OpenCV程序(环境配置)

昨天刚刚考完编译原理,私心想着可以做一些与考试无关的东西了.一直想做和图像处理相关的东西,趁这段时间有空学习一下OpenCV,搭建环境真是一件麻烦的事情,搞了近三个小时终于OK了.先来张图: 大致描述一下步骤吧: 一.安装前准备 1.VS2012(网上看到很多用的VS2010,但是基本不影响) 2.OpenCV 安装包(我下载的是最新的2.4.9) 二.安装OpenCV 1.解压OPenCV 说是安装,其实就是解压,OpenCV的Windows安装程序就是一个自解压程序: 这里我解压到C:\Pr

opencv学习笔记(03)——遍历图像(迭代器法)

1 #include <opencv2\highgui\highgui.hpp> 2 #include <opencv2\imgproc\imgproc.hpp> 3 #include <opencv2\core\core.hpp> 4 5 void colorReduce(cv::Mat& img, int div=64); 6 7 8 int main() 9 { 10 cv::Mat img_orginal = cv::imread("F:\\i

OpenCV学习笔记[3]Java Demo人脸识别

OpenCV学习笔记:Java Demo人脸识别 [简介] 我记得在很久以前,CSDN似乎搞过一个活动,给一个橘子林的照片,让程序计算相片里有多少个橘子.之所以对这个问题记忆犹新,是因为在专业学习初期,相比于排序遍历搜索等简单算法而言,"图像识别"算法一直是难以理解的东西,而我偏偏又痴迷于此,不管自己多么无知,对于令我迷惑的问题总是充满着解决的渴望. 通过对OpenCV的初步了解,我发现图像识别的很多问题都可以用它方便的解决,本次将是一个来自官方的人脸识别的实例,我们提供图像,使用内置

OpenCV 学习笔记(模板匹配)

OpenCV 学习笔记(模板匹配) 模板匹配是在一幅图像中寻找一个特定目标的方法之一.这种方法的原理非常简单,遍历图像中的每一个可能的位置,比较各处与模板是否"相似",当相似度足够高时,就认为找到了我们的目标. 在 OpenCV 中,提供了相应的函数完成这个操作. matchTemplate 函数:在模板和输入图像之间寻找匹配,获得匹配结果图像 minMaxLoc 函数:在给定的矩阵中寻找最大和最小值,并给出它们的位置 在具体介绍这两个函数之前呢,我们还要介绍一个概念,就是如何来评价两

openCV学习笔记(2)--cvCreateTrackbar

int cvCreateTrackbar( const char* trackbar_name, //滑动条的名称 const char* window_name, //窗口的名称,滑动条不会遮挡图像 int* value, //当滑动条被拖到时,OpenCV会自动将当前位置所代表的值传给指针指向的整数 int count, //滑动条所能达到的最大值 CvTrackbarCallback on_change //可选的回调函数,回调函数可参见http://wapedia.mobi/zhtrad

OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波

http://blog.csdn.net/chenyusiyuan/article/details/8710462 OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波 2013-03-23 17:44 16963人阅读 评论(28) 收藏 举报 分类: 机器视觉(34) 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] KAZE系列笔记: OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波 OpenCV学习笔记(28)KA