也比较简单,参考了网上的代码:
#include "cv.h" #include "highgui.h" void mouseHandler(int event, int x, int y, int flags, void* param) { IplImage* img0, * img1; CvFont font; uchar* ptr; char label[20]; img0 = (IplImage*) param; img1 = cvCloneImage(img0); cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, .8, .8, 0, 1, 8); if (event == CV_EVENT_LBUTTONDOWN) { /* 读取像素 */ ptr = cvPtr2D(img1, y, x, NULL); /* * 显示像素值,注意是BGR */ 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, 0) ); cvShowImage("img", img1); } } int main() { IplImage* img = cvLoadImage("E:\\分类数据1\\5273-分类1.bmp", 1); /* always check */ assert(img); /* create a window and install mouse handler */ cvNamedWindow("img", 1); cvSetMouseCallback("img", mouseHandler, (void*)img); cvShowImage("img", img); cvWaitKey(0); /* be tidy */ cvDestroyAllWindows(); cvReleaseImage(&img); return 0; }