Opencv--mouse to get image coordinates

In many cases, when we perform image processing, we want to look at the pixel coordinates or color information of a certain point.

This helps to verify the reliability of our algorithms for images. This blog post gives the position information and color information of using the mouse to obtain the image in opencv.

The development environment is VC6.0, OpenCv1.0 .

The properties of the mouse are mainly used here:

void mouseHandler(int event, int x,int y ,int flags,void *p)
{
	IplImage *img0, *img1;
	img0 = (IplImage*)p;
	img1 = cvCloneImage(img0);
	CvFont font, font2;
	uchar *ptr;
	char label[1000];
	char label2[1000];
	cvInitFont (&font,CV_FONT_HERSHEY_PLAIN,1,1,0,1,1);
	cvInitFont (&font2,CV_FONT_HERSHEY_PLAIN,1,1,0,1,1);
	if(event==CV_EVENT_LBUTTONDOWN)
	{
		ptr = cvPtr2D(img1, y,x,NULL);	//获取像素的灰度值
		sprintf(label, "Color:(%d, %d, %d)",ptr[0], ptr[1], ptr[2]);
		sprintf(label2, "Pixel:(%d, %d)", x, y);	//获取坐标点位置
		cvRectangle(img1, cvPoint(x,y-12),cvPoint(x+180, y+20),
			CV_RGB(255,0,0),CV_FILLED, 8,0);
		cvPutText(img1,label,cvPoint(x,y),&font,CV_RGB(255,255,255) );
		cvPutText(img1,label2,cvPoint(x,y+20),&font2,CV_RGB(255,255,255) );
		cvShowImage("src",img1);
	}
}

The experimental results are as follows:

Source code download address: http://download.csdn.net/detail/beijingmake209/7341415

Guess you like

Origin blog.csdn.net/beijingmake209/article/details/25725395