zoukankan      html  css  js  c++  java
  • 在OpenCV中利用鼠标绘制矩形和截取图像的矩形区域

    这是两个相关的程序,前者是后者的基础。实际上前一个程序也是在前面博文的基础上做的修改,请参考《在OpenCV中利用鼠标绘制直线》 。下面贴出代码。

    程序之一,在OpenCV中利用鼠标绘制矩形

    [c-sharp] view plaincopy
    1. #include <cv.h>  
    2. #include <highgui.h>  
    3. #include <stdio.h>  
    4. #pragma comment( lib, "cv.lib" )  
    5. #pragma comment( lib, "cxcore.lib" )  
    6. #pragma comment( lib, "highgui.lib" )  
    7. IplImage* src = 0;   
    8. IplImage* dst = 0;   
    9. void on_mouse( int eventint x, int y, int flags, void* ustc)  
    10. {  
    11.     static CvPoint pre_pt = {-1,-1};  
    12.     static CvPoint cur_pt = {-1,-1};  
    13.     CvFont font;  
    14.     cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);  
    15.     char temp[16];  
    16.       
    17.     ifevent == CV_EVENT_LBUTTONDOWN )  
    18.     {  
    19.         cvCopy(dst,src);  
    20.         sprintf(temp,"(%d,%d)",x,y);  
    21.         pre_pt = cvPoint(x,y);  
    22.         cvPutText(src,temp, pre_pt, &font, cvScalar(0,0, 0, 255));  
    23.         cvCircle( src, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );  
    24.         cvShowImage( "src", src );  
    25.         cvCopy(src,dst);  
    26.     }  
    27.     else ifevent == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))  
    28.     {  
    29.         cvCopy(dst,src);  
    30.         sprintf(temp,"(%d,%d)",x,y);  
    31.         cur_pt = cvPoint(x,y);        
    32.         cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
    33.         cvShowImage( "src", src );  
    34.     }  
    35.     else ifevent == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))  
    36.     {  
    37.         cvCopy(dst,src);  
    38.         sprintf(temp,"(%d,%d)",x,y);  
    39.         cur_pt = cvPoint(x,y);        
    40.         cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
    41.         cvRectangle(src, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );  
    42.         cvShowImage( "src", src );  
    43.     }  
    44.     else ifevent == CV_EVENT_LBUTTONUP )  
    45.     {  
    46.         sprintf(temp,"(%d,%d)",x,y);  
    47.         cur_pt = cvPoint(x,y);        
    48.         cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
    49.         cvCircle( src, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );  
    50.         cvRectangle( src, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );  
    51.         cvShowImage( "src", src );  
    52.         cvCopy(src,dst);  
    53.     }  
    54. }  
    55. int main()  
    56. {  
    57.     src=cvLoadImage("lena.jpg",1);  
    58.     dst=cvCloneImage(src);  
    59.     cvNamedWindow("src",1);  
    60.     cvSetMouseCallback( "src", on_mouse, 0 );  
    61.       
    62.     cvShowImage("src",src);  
    63.     cvWaitKey(0);   
    64.     cvDestroyAllWindows();  
    65.     cvReleaseImage(&src);  
    66.     cvReleaseImage(&dst);  
    67.     return 0;  
    68. }  


    效果图如下

     

    程序之二,在OpenCV中利用鼠标绘制矩形并截取该矩形区域的图像

    [c-sharp] view plaincopy
    1. #include <cv.h>  
    2. #include <highgui.h>  
    3. #include <stdio.h>  
    4. #pragma comment( lib, "cv.lib" )  
    5. #pragma comment( lib, "cxcore.lib" )  
    6. #pragma comment( lib, "highgui.lib" )  
    7. IplImage* org = 0;  
    8. IplImage* img = 0;   
    9. IplImage* tmp = 0;   
    10. IplImage* dst = 0;   
    11. void on_mouse( int eventint x, int y, int flags, void* ustc)  
    12. {  
    13.     static CvPoint pre_pt = {-1,-1};  
    14.     static CvPoint cur_pt = {-1,-1};  
    15.     CvFont font;  
    16.     cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);  
    17.     char temp[16];  
    18.       
    19.     ifevent == CV_EVENT_LBUTTONDOWN )  
    20.     {  
    21.         cvCopy(org,img);  
    22.         sprintf(temp,"(%d,%d)",x,y);  
    23.         pre_pt = cvPoint(x,y);  
    24.         cvPutText(img,temp, pre_pt, &font, cvScalar(0,0, 0, 255));  
    25.         cvCircle( img, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );  
    26.         cvShowImage( "img", img );  
    27.         cvCopy(img,tmp);  
    28.     }  
    29.     else ifevent == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))  
    30.     {  
    31.         cvCopy(tmp,img);  
    32.         sprintf(temp,"(%d,%d)",x,y);  
    33.         cur_pt = cvPoint(x,y);        
    34.         cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
    35.         cvShowImage( "img", img );  
    36.     }  
    37.     else ifevent == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))  
    38.     {  
    39.         cvCopy(tmp,img);  
    40.         sprintf(temp,"(%d,%d)",x,y);  
    41.         cur_pt = cvPoint(x,y);        
    42.         cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
    43.         cvRectangle(img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );  
    44.         cvShowImage( "img", img );  
    45.     }  
    46.     else ifevent == CV_EVENT_LBUTTONUP )  
    47.     {  
    48.         cvCopy(tmp,img);  
    49.         sprintf(temp,"(%d,%d)",x,y);  
    50.         cur_pt = cvPoint(x,y);        
    51.         cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));  
    52.         cvCircle( img, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );  
    53.         cvRectangle( img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );  
    54.         cvShowImage( "img", img );  
    55.         cvCopy(img,tmp);  
    56.         int width=abs(pre_pt.x-cur_pt.x);  
    57.         int height=abs(pre_pt.y-cur_pt.y);  
    58.         if(width==0 || height==0)  
    59.         {  
    60.             cvDestroyWindow("dst");  
    61.             return;  
    62.         }  
    63.         dst=cvCreateImage(cvSize(width,height),org->depth,org->nChannels);  
    64.         CvRect rect;  
    65.         if(pre_pt.x<cur_pt.x && pre_pt.y<cur_pt.y)  
    66.         {  
    67.             rect=cvRect(pre_pt.x,pre_pt.y,width,height);  
    68.         }  
    69.         else if(pre_pt.x>cur_pt.x && pre_pt.y<cur_pt.y)  
    70.         {  
    71.             rect=cvRect(cur_pt.x,pre_pt.y,width,height);  
    72.         }  
    73.         else if(pre_pt.x>cur_pt.x && pre_pt.y>cur_pt.y)  
    74.         {  
    75.             rect=cvRect(cur_pt.x,cur_pt.y,width,height);  
    76.         }  
    77.         else if(pre_pt.x<cur_pt.x && pre_pt.y>cur_pt.y)  
    78.         {  
    79.             rect=cvRect(pre_pt.x,cur_pt.y,width,height);  
    80.         }  
    81.         cvSetImageROI(org,rect);  
    82.         cvCopy(org,dst);  
    83.         cvResetImageROI(org);  
    84.         cvDestroyWindow("dst");  
    85.         cvNamedWindow("dst",1);  
    86.         cvShowImage("dst",dst);  
    87.         cvSaveImage("dst.jpg",dst);  
    88.     }  
    89. }  
    90. int main()  
    91. {  
    92.     org=cvLoadImage("lena.jpg",1);  
    93.     img=cvCloneImage(org);  
    94.     tmp=cvCloneImage(org);  
    95.     cvNamedWindow("img",1);  
    96.     cvSetMouseCallback( "img", on_mouse, 0 );  
    97.       
    98.     cvShowImage("img",img);  
    99.     cvWaitKey(0);   
    100.     cvDestroyAllWindows();  
    101.     cvReleaseImage(&org);  
    102.     cvReleaseImage(&img);  
    103.     cvReleaseImage(&tmp);  
    104.     cvReleaseImage(&dst);  
    105.     return 0;  
    106. }  

     

    效果图如下

     

    from:http://blog.csdn.net/quarryman/article/details/6435527

  • 相关阅读:
    48. Rotate Image
    47. Permutations II
    46. Permutations
    45. Jump Game II
    44. Wildcard Matching
    43. Multiply Strings
    42. Trapping Rain Water
    41. First Missing Positive
    40. Combination Sum II
    39. Combination Sum
  • 原文地址:https://www.cnblogs.com/lidabo/p/3316729.html
Copyright © 2011-2022 走看看