OPENCV学习笔记16_用控制器设计模式实现功能模块间通信
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; }
#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
#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
#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; }
以上是关于OPENCV学习笔记16_用控制器设计模式实现功能模块间通信的主要内容,如果未能解决你的问题,请参考以下文章
OPENCV学习笔记3-4_使用模型-视图-控制器设计应用程序