opencv_显示鼠标点击处像素的颜色值

读入并显示一幅图像,当用户鼠标点击图像时,获取图像对应像素的颜色值(RGB),并在图像上点击鼠标处用文本将颜色值显示出来。

#include 
#include  
void myMouseCallback(int event,int x,int y,int flags,void* param)
{
	IplImage* img = (IplImage*)param;
	IplImage* img1 = cvCloneImage(img);
	CvFont font;
	char text[20];
	uchar* ptr;
	cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, .8, .8, 0, 1, 8);
	
	if(event == CV_EVENT_LBUTTONDOWN)
	{
		ptr = cvPtr2D(img1, y, x, NULL);
		sprintf(text,"(%d,%d,%d)",ptr[2],ptr[1],ptr[0]);
		 
		cvPutText(img1,text,cvPoint(x,y),&font,CV_RGB(255,0,255));
		cvShowImage("MyWindow",img1);
	}
}
int main( int argc, char* argv[] ) 
{ 
	IplImage* img = cvLoadImage("1.jpg");
	cvNamedWindow("MyWindow");
	
	cvSetMouseCallback("MyWindow",myMouseCallback,(void*)img);
	cvShowImage("MyWindow",img);
	cvWaitKey(0);
	cvDestroyAllWindows();
	cvReleaseImage(&img);
	return 0;
}


你可能感兴趣的:(openCV)