OpenCV Learning Notes (ii): Basic operation of the image (vs2010+opencv3.0) __OPENCV

Source: Internet
Author: User
Tags abs

This section corresponds to Opencv.2.computer.vision.application.programming.cookbook

-----3rd Chapter Processingimages with Classes the using a Controller to communicate with processing modules part.

This section details the feeds in the book and gives back to the people who want to be inspired.

Begin

First, open the VS2010, establish the project



Second, the next step, the original "use of Unicode Library" hook removed.


After completion, the editing interface appears



Edit dialog box, design the following interface.


V. Click Solution Explorer to add a header file Colordetector.h



#if!definedcolordetect

#define Colordetect

#include <opencv2/core/core.hpp>

Class Colordetector

{

Private

Minimumacceptable distance

int mindist;

Target Color

CV::VEC3B Target;

imagecontaining resulting binary map

Cv::mat result;

Inlineprivate member function

Computes thedistance from Target color.

int getdistance (const cv::vec3b& color) const;

Public

Empty constructor

Colordetector (): mindist (100)

{

Default parameter initialization here

Target[0] = target[1] = target[2]= 0;

}

Getters Andsetters

Sets Thecolor Distance threshold

Thresholdmust be positive,otherwise distance threshold are set to 0.

void setcolordistancethreshold (int distance);

Gets Thecolor Distance threshold

int getcolordistancethreshold () const;

Sets Thecolor to be detected

void Settargetcolor (Unsignedchar red,unsignedchar Green,unsignedchar blue);

Sets Thecolor to be detected

void Settargetcolor (cv::vec3b color);

Gets Thecolor to be detected

CV::VEC3B Gettargetcolor () const;

Processesthe image. Returns a 1-channel binary image.

Cv::mat proecess (Constcv::mat &image);

};

#endif

Vi. Add source Files Colordetector.cpp

#include "StdAfx.h"

#include "Colordetector.h"

int colordetector::getdistance (constcv::vec3b& color) const

{

Return ABS (Color[0]-target[0]) +abs (color[1]-target[1]) +abs (color[2]-target[2));

}

void colordetector::setcolordistancethreshold (int distance)

{

if (Distance < 0)

{

Distance = 0;

}

Mindist = distance;

}

int Colordetector::getcolordistancethreshold () const

{

Returnmindist;

}

void Colordetector::settargetcolor (Unsignedchar red, Unsignedchar Green,unsignedchar Blue)

{

TARGET[2] = red;

TARGET[1] = green;

Target[0] = blue;

}

void Colordetector::settargetcolor (cv::vec3b color)

{

target = color;

}

Cv::vec3bcolordetector::gettargetcolor () const

{

return target;

}

Cv::matcolordetector::p roecess (const cv::mat&image)

{

Re-allocatebinary Map If necessary

Same size asinput image,but 1-channel

Result.create (image.rows,image.cols,cv_8u);

Get Theiterators

Cv::mat_<cv::vec3b>::const_iteratorit = image.begin<cv::vec3b> ();

Cv::mat_<cv::vec3b>::const_iteratoritend = image.end<cv::vec3b> ();

Cv::mat_<uchar>::iterator itout =result.begin<uchar> ();

For Eachpixel

for (; it!= itend; ++it, ++itout)

{

Process each pixel------------------------

Compute distance from Target color

if (Getdistance (*it) < mindist)

{

*itout = 255;

}

Else

{

*itout = 0;

}

End Ofpixel Processing

}

return result;

}

Vii. adding ColorDetectController.h and colorDetectController.cpp in the same vein

ColorDetectController.h

#if!DEFINEDCD_CNTRLLR

#define Cd_cntrllr

#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>

#include "Colordetector.h"

Class Colordetectcontroller

{

Private

Static Colordetectcontroller *singleton; Pointer to the Singleton

Colordetector *cdetect;

The image Tobe processed

Cv::mat image;

Cv::mat result;

Public

Colordetectcontroller ();

Sets Thecolor Distance threshold

void setcolordistancethreshold (int distance);

Gets Thecolor Distance threshold

int getcolordistancethreshold () const;

Sets Thecolor to be detected

void Settargetcolor (Unsignedchar red,unsignedchar Green,unsignedchar blue);

Gets Thecolor to be detected

void Gettargetcolor (Unsignedchar &red,unsignedchar &green,unsignedchar &blue) const;

Sets theinput image. Reads it from file

BOOL Setinputimage (std::string filename);

Returns thecurrent input image.

Const CV::MAT getinputimage () const;

Performsimage processing

void process ();

Returns theimage result from the latest processing

Const Cv::matgetlastresult () const;

Deletes Allprocessor objects created by the controller

~colordetectcontroller ();

Singletonstatic Members

Static Colordetectcontroller *getinstance ()

{

if (singleton = 0)

singleton= new Colordetectcontroller;

return singleton;

}

Releases Thesingleton instance of this controller.

static void Destroy ();

};

#endif

ColorDetectController.cpp

#include "StdAfx.h"

#include "ColorDetectController.h"

Colordetectcontroller*colordetectcontroller::singleton = 0;

Colordetectcontroller::colordetectcontroller ()

{

Privateconstructor

Setting UpThe Application

Cdetect = new Colordetector ();

}

void Colordetectcontroller::setcolordistancethreshold (Intdistance)

{

Cdetect->setcolordistancethreshold (distance);

}

int Colordetectcontroller::getcolordistancethreshold () const

{

return Cdetect->getcolordistancethreshold ();

}

void Colordetectcontroller::settargetcolor (Unsignedchar red, Unsignedchar Green, Unsignedchar Blue)

{

Cdetect->settargetcolor (Red,green,blue);

}

void Colordetectcontroller::gettargetcolor (unsignedchar&red, Unsignedchar&green, Unsignedchar&blue) Const

{

Cv::vec3b color =cdetect->gettargetcolor ();

red = color[2];

Green = color[1];

blue = color[0];

}

BOOL Colordetectcontroller::setinputimage (Std::stringfilename)

{

Image = Cv::imread (filename);

if (!image.data)

Returnfalse;

Else

Returntrue;

}

Const CV::MAT Colordetectcontroller::getinputimage () const

{

return image;

}

void Colordetectcontroller::p rocess ()

{

result = cdetect->proecess (image);

}

Const CV::MAT Colordetectcontroller::getlastresult () const

{

return result;

}

Colordetectcontroller::~colordetectcontroller ()

{

Delete Cdetect;

}

void Colordetectcontroller::d Estroy ()

{

if (singleton!= 0)

{

Delete Singleton;

Singleton = 0;

}

}


Eight, double-click ColourDetectorDlg.h, appear the editing interface, import header file colorDetectController.h and define variables Colordetect


Ix. Add an event handler for the Openimage button and the process button



Ten, add code separately

void Ccolourdetectordlg::onbnclickedopenbutton ()

{

TODO: Add control notification Handler code here

CFileDialog Dlg (true,_t ("*.bmp"), Null,ofn_filemustexist | Ofn_pathmustexist | ofn_hidereadonly,_t ("ImageFile" (*.bmp; *.jpg) |*.bmp; *.jpg| All Files (*.*) |*.*| | "), NULL);

Dlg.m_ofn.lpstrTitle = _t ("Open Image");

If a filename has been selected

if (idok== dlg. DoModal ())

{

std::string filename = dlg. GetPathName ();

Setand Display the input image

Colordetect.setinputimage (filename);

Cv::imshow ("Input Image", Colordetect.getinputimage ());

}

}

void Ccolourdetectordlg::onbnclickedprocessbutton ()

{

TODO: Add control notification Handler code here

Target Coloris hard-coded here

Colordetect.settargetcolor (130,190,230);

Process theinput image and display result

Colordetect.process ();

Cv::imshow ("Outputresult", Colordetect.getlastresult ());

}

Xi. compiling, appearing

Click ' Open Image ' to appear


Select the picture you want

Click Process to appear


12, the original interface also has a shortage. is the direct click of the "Process" button will be a memory error prompts, because you did not hit the Kaiyuan image can not be processed before the picture.

1, click the "Process" button, pop-up "Properties" box, "Disabled" to "True"


2, double click "ColourDetectorDlg.cpp", add in the processing event of the "Open Image" button

Idc_process_button is the ID of the PROCESS button

GetDlgItem (Idc_process_button)->enablewindow (true);

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.