《学习OpenCV》练习题第四章第二题
1 #include <highgui.h> 2 #include <cv.h> 3 4 #pragma comment (lib,"opencv_calib3d231d.lib") 5 #pragma comment (lib,"opencv_contrib231d.lib") 6 #pragma comment (lib,"opencv_core231d.lib") 7 #pragma comment (lib,"opencv_features2d231d.lib") 8 #pragma comment (lib,"opencv_flann231d.lib") 9 #pragma comment (lib,"opencv_gpu231d.lib") 10 #pragma comment (lib,"opencv_haartraining_engined.lib") 11 #pragma comment (lib,"opencv_highgui231d.lib") 12 #pragma comment (lib,"opencv_imgproc231d.lib") 13 #pragma comment (lib,"opencv_legacy231d.lib") 14 #pragma comment (lib,"opencv_ml231d.lib") 15 #pragma comment (lib,"opencv_objdetect231d.lib") 16 #pragma comment (lib,"opencv_ts231d.lib") 17 #pragma comment (lib,"opencv_video231d.lib") 18 19 /* 20 *《学习OpenCV》第四章第二题 21 * 完成时间:0:38 3/11 星期一 2013 22 */ 23 24 CvFont font; 25 26 /* 27 * 鼠标事件回调函数 28 */ 29 void my_mouse_callback(int event, int x, int y, int flags, void* param) 30 { 31 // 得到图像 32 IplImage* image = (IplImage*) param; 33 34 // 获取图像对应像素的颜色值 35 CvScalar pixel_value; 36 pixel_value = cvGet2D( image, x, y ); 37 38 // 构造要显示的文本 39 char display_text[14]; 40 sprintf( display_text, "(%d %d %d)", (int) pixel_value.val[0], 41 (int)pixel_value.val[1], (int)pixel_value.val[2] ); 42 43 44 switch(event) 45 { 46 // 鼠标左键点击事件 47 case CV_EVENT_LBUTTONDOWN: 48 printf("(%d, %d) --- (%f\t%f\t%f)\n", x, y, pixel_value.val[0], pixel_value.val[1], pixel_value.val[2]); 49 cvPutText( image, display_text, cvPoint(x, y), &font, pixel_value); 50 break; 51 } 52 } 53 54 int main() 55 { 56 IplImage* image = cvLoadImage("1.jpg", 1); 57 cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1.0f, 1.0f, 0, 1, 8); 58 59 cvNamedWindow("image"); 60 cvSetMouseCallback( "image", my_mouse_callback, (void*)image ); 61 62 while(1) 63 { 64 cvShowImage("image", image); 65 66 if(cvWaitKey(15) == 27) break; 67 } 68 69 cvReleaseImage( &image ); 70 cvDestroyAllWindows(); 71 return 0; 72 }
运行结果: