Runtime ROI Selection using Mouse

来源:互联网 发布:设计师导航源码 编辑:程序博客网 时间:2024/05/04 12:38

http://nashruddin.com/Runtime_ROI_Selection_using_Mouse

Listing 1: Runtime ROI Selection using Mouse

  1. #include <stdio.h>
  2. #include "cv.h"
  3. #include "highgui.h"
  4.  
  5. IplImage* img0, * img1;
  6. CvPoint   point;
  7. int       drag = 0;
  8.  
  9. void
  10. mouseHandler(int event, int x, int y, int flags, void* param)
  11. {
  12.     /* user press left button */
  13.     if (event == CV_EVENT_LBUTTONDOWN && !drag)
  14.     {
  15.         point = cvPoint(x, y);
  16.         drag  = 1;
  17.     }
  18.  
  19.     /* user drag the mouse */
  20.     if (event == CV_EVENT_MOUSEMOVE && drag)
  21.     {
  22.         img1 = cvClone(img0);
  23.        
  24.         cvRectangle(
  25.             img1,
  26.             point,
  27.             cvPoint(x, y),
  28.             CV_RGB(255, 0, 0),
  29.             1, 8, 0
  30.         );
  31.        
  32.         cvShowImage("img", img1);
  33.     }
  34.  
  35.     /* user release left button */
  36.     if (event == CV_EVENT_LBUTTONUP && drag)
  37.     {
  38.         img1 = cvClone(img0);
  39.        
  40.         cvSetImageROI(
  41.             img1,
  42.             cvRect(
  43.                 point.x,
  44.                 point.y,
  45.                 x - point.x,
  46.                 y - point.y
  47.             )
  48.         );
  49.        
  50.         cvNot(img1, img1);    // or do whatever with the ROI
  51.        
  52.         cvResetImageROI(img1);
  53.         cvShowImage("img", img1);
  54.         drag = 0;
  55.     }
  56.  
  57.     /* user click right button: reset all */
  58.     if (event == CV_EVENT_RBUTTONUP)
  59.     {
  60.         cvShowImage("img", img0);
  61.         drag = 0;
  62.     }
  63. }
  64.  
  65. int
  66. main(int argc, char** argv)
  67. {
  68.     if (argc != 2)
  69.     {
  70.         fprintf(stderr, "Usage: %s <image>\n", argv[0]);
  71.         return 1;
  72.     }
  73.  
  74.     img0 = cvLoadImage(argv[1], 1);
  75.  
  76.     cvNamedWindow("img", 1);
  77.     cvSetMouseCallback("img", mouseHandler, NULL);
  78.     cvShowImage("img", img0);
  79.     cvWaitKey(0);
  80.     cvDestroyWindow("img");
  81.     cvReleaseImage(&img0);
  82.     cvReleaseImage(&img1);
  83.  
  84.     return 0;
  85. }
  86.  

Some final notes: you define the ROI by dragging the mouse from the upper left corner to the bottom right corner. Otherwise it generates errors. I will leave you to fix the bugs.

Update on April 14th, 2010:

Compiling the code above using C++ compiler generates error:

error C2440: '=' : cannot convert from 'void *' to 'IplImage *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast

This is because cvClone() at line 22 and 38 returns void* while img1 is type of IplImage*. To fix this bug, simply replace:

img1 = cvClone(img0)

with:

img1 = cvCloneImage(img0)

Thanks to sycluap for reporting this.


原创粉丝点击