Using a controller to communicate with processing modules

Source: Internet
Author: User

Create a simple dialog-based application with two button, one to select an image and one to start the Processing

Now, we will first add a button that will allow us to select the file that contains the image. go under the resource and drag a button onto the dialog. you can also resize the dialog and take the time to look at all the widget available in the toolbox.
Change the caption of the button (see the properties Panel) to open image. Right click on
The button and select Add event handler ...; This will allow you to specify the name of the handler method that will be called when the user will click on this button.

Here, we use the colordetector class of the previous recipe.

class ColorDetector {private:// minimum acceptable distanceint minDist;// target colorcv::Vec3b target;// image containing resulting binary mapcv::Mat result;public:// empty constructorColorDetector() : minDist(100) {// default parameter initialization heretarget[0]= target[1]= target[2]= 0;}// Sets the color distance threshold.// Threshold must be positive,// otherwise distance threshold is set to 0.void setColorDistanceThreshold(int distance) {if (distance<0)distance=0;minDist= distance;}// Get the color distance thresholdint getColorDistanceThreshold() const {return minDist;}// Sets the color to be detectedvoid setTargetColor(unsigned char red,unsigned char green,unsigned char blue) {// BGR ordertarget[2]= red;target[1]= green;target[0]= blue;}// Sets the color to be detectedvoid setTargetColor(cv::Vec3b color) {target= color;}// Gets the color to be detectedcv::Vec3b getTargetColor() const {return target;}int getDistance(const cv::Vec3b& color) const {return abs(color[0]-target[0])+   abs(color[1]-target[1])+   abs(color[2]-target[2]);}cv::Mat ColorDetector::process(const cv::Mat &image) {// re-allocate binary map if necessary// same size as input image, but 1-channelresult.create(image.rows,image.cols,CV_8U);// get the iteratorscv::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 each pixelfor ( ; it!= itend; ++it, ++itout) {// process each pixel -----------------------// compute distance from target colorif (getDistance(*it)<minDist) {*itout= 255;} else {*itout= 0;}// end of pixel processiong -----------------}return result;}};class ColorDetectController {private:// the algorithm classColorDetector *cdetect;cv::Mat image;// The image to be processedcv::Mat result;// The image resultpublic:ColorDetectController() {// setting up the applicationcdetect= new ColorDetector();}// Set the color distance thresholdvoid setColorDestanceThreshold(int distance) {cdetect->setColorDistanceThreshold(distance);}// Get the color distance thresholdint getColorDistanceThreshold() const {return cdetect->getColorDistanceThreshold();}//Set the color to be detectedvoid setTargetColor(unsigned char red,unsigned char green,unsigned char blue) {cdetect->setTargetColor(red,green,blue);}// Get the color to be detectedvoid getTargetColor(unsigned char &red,unsigned char &green,unsigned char &blue) const {cv::Vec3b color= cdetect->getTargetColor();red= color[2];green= color[1];blue= color[0];}// Set the input image. Reads it fram file.bool setInputImage(std::string filename) {image= cv::imread(filename);if (!image.data)return false;elsereturn true;}// Return the current input image.const cv::Mat getInputImage() const {return image;}// Perform image procesing.void process() {result= cdetect->process(image);}// Return the image result from the latest processing.const cv::Mat getLastResult() const {return result;}// Detects processor objects created by the controller.~ColorDetectController() {delete cdetect;}};

Let us now use the cfiledialog class in order to create a file dialog. this one will show up by adding the following code to the onopen member function. you just add a colordetectcontroller member variable to the dialog class (called colordetect
Here). In the case of a MFC dialog, the Open button wowould then look as follows:

// Callback method of "Open" buttonvoid Cp76colordetector1Dlg::OnBnClickedButton1(){// TODO: Add your control notification handler code here// MFC widget to select a file of type bmp or jpgCFileDialog dlg(TRUE,_T("*.bmp"),NULL,OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,_T("image file (*.bmp; *.jpg)|*.bmp;*.jpg|All Files (*.*)|*.*||"),NULL);dlg.m_ofn.lpstrTitle= _T("Open Image");// if  filename has been selectedif (dlg.DoModal() == IDOK) {// get the path of the selected filenamestd::string filename= dlg.GetPathName();// set and display the input imagecolordetect.setInputImage(filename);cv::imshow("Input Image",colordetect.getInputImage());}}// Callback method of "process" buttonvoid Cp76colordetector1Dlg::OnBnClickedButton2(){// TODO: Add your control notification handler code here// target color is hard-coded herecolordetect.setTargetColor(130,190,230);// process the input image and display resultcolordetect.process();cv::imshow("Output Result",colordetect.getLastResult());}

Running...

Click the button of open image

Click the button of Process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.