Using a Controller to communicate with processing modules

6 篇文章 1 订阅
5 篇文章 0 订阅

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 distance
		int minDist;
		// target color
		cv::Vec3b target;
		// image containing resulting binary map
		cv::Mat result;

	public:
		// empty constructor
		ColorDetector() : minDist(100) {
			// default parameter initialization here
			target[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 threshold
		int getColorDistanceThreshold() const {
			return minDist;
		}

		// Sets the color to be detected
		void setTargetColor(unsigned char red,
							unsigned char green,
							unsigned char blue) {
			// BGR order
			target[2]= red;
			target[1]= green;
			target[0]= blue;
		}

		// Sets the color to be detected
		void setTargetColor(cv::Vec3b color) {
			target= color;
		}

		// Gets the color to be detected
		cv::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-channel
			result.create(image.rows,image.cols,CV_8U);

			// get the iterators
			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 each pixel
			for ( ; it!= itend; ++it, ++itout) {
				// process each pixel -----------------------
				// compute distance from target color
				if (getDistance(*it)<minDist) {
					*itout= 255;
				} else {
					*itout= 0;
				}
				// end of pixel processiong -----------------
			}
			return result;
		}
};

class ColorDetectController {
	private:
		// the algorithm class
		ColorDetector *cdetect;
		cv::Mat image;	// The image to be processed
		cv::Mat result;	// The image result

	public:
		ColorDetectController() {
			// setting up the application
			cdetect= new ColorDetector();
		}

		// Set the color distance threshold
		void setColorDestanceThreshold(int distance) {
			cdetect->setColorDistanceThreshold(distance);
		}

		// Get the color distance threshold
		int getColorDistanceThreshold() const {
			return cdetect->getColorDistanceThreshold();
		}

		//Set the color to be detected
		void setTargetColor(unsigned char red,
			unsigned char green,unsigned char blue) {
			cdetect->setTargetColor(red,green,blue);
		}

		// Get the color to be detected
		void 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;
			else
				return 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 would then look as follows:

// Callback method of "Open" button
void Cp76colordetector1Dlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	// MFC widget to select a file of type bmp or jpg
	CFileDialog 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 selected
	if (dlg.DoModal() == IDOK) {
		// get the path of the selected filename
		std::string filename= dlg.GetPathName();
		// set and display the input image
		colordetect.setInputImage(filename);
		cv::imshow("Input Image",colordetect.getInputImage());
	}
}

// Callback method of "process" button
void Cp76colordetector1Dlg::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
	// target color is hard-coded here
	colordetect.setTargetColor(130,190,230);
	// process the input image and display result
	colordetect.process();
	cv::imshow("Output Result",colordetect.getLastResult());
}
running...


Click the button of Open Image


Click the button of Process






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值