這是兩個相關的程序,前者是後者的基礎。實際上前一個程序也是在前面博文的基礎上做的修改,請參考《在OpenCV中利用鼠標繪制直線》http://www.linuxidc.com/Linux/2012-09/70011.htm 。下面貼出代碼。
程序之一,在OpenCV中利用鼠標繪制矩形
- #include <cv.h>
- #include <highgui.h>
- #include <stdio.h>
- #pragma comment( lib, "cv.lib" )
- #pragma comment( lib, "cxcore.lib" )
- #pragma comment( lib, "highgui.lib" )
- IplImage* src = 0;
- IplImage* dst = 0;
- void on_mouse( int event, int x, int y, int flags, void* ustc)
- {
- static CvPoint pre_pt = {-1,-1};
- static CvPoint cur_pt = {-1,-1};
- CvFont font;
- cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);
- char temp[16];
-
- if( event == CV_EVENT_LBUTTONDOWN )
- {
- cvCopy(dst,src);
- sprintf(temp,"(%d,%d)",x,y);
- pre_pt = cvPoint(x,y);
- cvPutText(src,temp, pre_pt, &font, cvScalar(0,0, 0, 255));
- cvCircle( src, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
- cvShowImage( "src", src );
- cvCopy(src,dst);
- }
- else if( event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
- {
- cvCopy(dst,src);
- sprintf(temp,"(%d,%d)",x,y);
- cur_pt = cvPoint(x,y);
- cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
- cvShowImage( "src", src );
- }
- else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
- {
- cvCopy(dst,src);
- sprintf(temp,"(%d,%d)",x,y);
- cur_pt = cvPoint(x,y);
- cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
- cvRectangle(src, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
- cvShowImage( "src", src );
- }
- else if( event == CV_EVENT_LBUTTONUP )
- {
- sprintf(temp,"(%d,%d)",x,y);
- cur_pt = cvPoint(x,y);
- cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
- cvCircle( src, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
- cvRectangle( src, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
- cvShowImage( "src", src );
- cvCopy(src,dst);
- }
- }
- int main()
- {
- src=cvLoadImage("lena.jpg",1);
- dst=cvCloneImage(src);
- cvNamedWindow("src",1);
- cvSetMouseCallback( "src", on_mouse, 0 );
-
- cvShowImage("src",src);
- cvWaitKey(0);
- cvDestroyAllWindows();
- cvReleaseImage(&src);
- cvReleaseImage(&dst);
- return 0;
- }
效果圖如下