opencv利用鼠标获取图像的像素值

简单的点击鼠标可以查找图像的像素值,方法很简单。

code:

void mouseHandler(int event, int x,int y ,int flags,void *p)
{
	IplImage *img0, *img1;
	img0=(IplImage*)p;
	img1=cvCloneImage(img0);
	CvFont font;
	uchar *ptr;
	char label[20];
	cvInitFont (&font,CV_FONT_HERSHEY_PLAIN,0.8,0.8,0,1,8);
	if(event==CV_EVENT_LBUTTONDOWN)
	{
		ptr=cvPtr2D(img1, y,x,NULL);
		sprintf(label, "(%d, %d, %d)",ptr[0], ptr[1], ptr[2]);
		cvRectangle(img1, cvPoint(x,y-12),cvPoint(x+100, y+4),
		CV_RGB(255,0,0),CV_FILLED, 8,0);
		cvPutText(img1,label,cvPoint(x,y),&font,CV_RGB(255,255,255) );
		cvShowImage("src",img1);
	}
}

int main()
{
	IplImage *src;
	src=cvLoadImage("e://kankan//fish.jpg");
	assert(src);
	 cvNamedWindow("src", 1);
	cvSetMouseCallback("src", mouseHandler, (void*)src);
	mouseHandler(0,0,0,0,src);
	cvShowImage("src",src);
	cvWaitKey(0);
	return 0;


}


 

Result:

你可能感兴趣的:(OPENCV)