Runtime ROI Selection using Mouse

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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值