opencv学习(三)书本《学习Opencv》(中文版)第四章的样例Ex4-1(opencv3.0.0+VS2012+win7)

第一个样例需要改的地方就是 cvCopy( image, temp );找不到cvCopyImage, 应该也是由于opencv3.0和以前版本的不同


void my_mouse_callback(   int event, int x, int y, int flags, void* param );

参数:第一个参数是事件类型如CV_EVENT_MOUSEMOVE,第二,三个参数是事件发生时鼠标位置的x,y坐标, 第四个参数指定事件发生时的状态如CV_EVENT_FLAG_SHIFTKEY,第五个参数是void指针,传递额外任何结构信息


cvSetMouseCallback(     "Box Example",     my_mouse_callback,     (void*) image   );

参数:第一个参数是产生事件的窗口,第二个参数是回调函数,第三个是额外信息


非常精彩的一个样例,稳定性很好:

/*总体的程序结构是先在temp图像上进行画矩阵,
然后将其画到image中,while循环中不断的将image拷贝到temp中,否则会有很多矩形框
再画下一个矩阵*/

#include <cv.h>
#include <highgui.h>
 
// Define our callback which we will install for
// mouse events.
// 定义我们的为鼠标事件安装的回调函数
void my_mouse_callback(
   int event, int x, int y, int flags, void* param 
);

CvRect box;//全局变量
bool drawing_box = false;

// A litte subroutine to draw a box onto an image
// 一个小的用来在图像上画方框的副进程
void draw_box( IplImage* img, CvRect rect ) {
  cvRectangle (
    img, 
    cvPoint(box.x,box.y),
    cvPoint(box.x+box.width,box.y+box.height),
    cvScalar(0xff,0x00,0x00)    /* red *///RGB,但是画出来的是蓝色的?
	//还是因为黑底填充了红色所以看不见,那线条的蓝色怎么来的?
  );
}
 
int main( int argc, char* argv[] ) {
  
  box = cvRect(-1,-1,0,0);//int x, y, width, height

  IplImage* image = cvCreateImage( 
    cvSize(400,400),
    IPL_DEPTH_8U,
    3
  );
  cvZero( image );
  //void cvZero( CvArr* arr );
  //This function sets all values in all channels of the array to 0.
  //这个函数把阵列所有通道的值均设为0
  IplImage* temp = cvCloneImage( image );
  
  cvNamedWindow( "Box Example" );
 
  // Here is the crucial moment that we actually install
  // the callback.  Note that we set the value ‘param’ to
  // be the image we are working with so that the callback
  // will have the image to edit.
  // 这就是我们安装回调函数的关键时刻。
  // 注意我们把param设为了image,因此该回调函数有图像能编辑
  cvSetMouseCallback( 
    "Box Example", 
    my_mouse_callback, 
    (void*) image 
  );
 
  // The main program loop.  Here we copy the working image
  // to the ‘temp’ image, and if the user is drawing, then
  // put the currently contemplated box onto that temp image.
  // display the temp image, and wait 15ms for a keystroke,
  // then repeat…
  //
  while( 1 ) {
 
    cvCopy( image, temp );//Copy elements of one array to another
	  //void cvCopy(const CvArr* src,CvArr* dst,const CvArr* mask = NULL);

    if( drawing_box ) draw_box( temp, box ); 
    cvShowImage( "Box Example", temp );
 
    if( cvWaitKey( 15 )==27 ) break;
  }
 
  // Be tidy
  //
  cvReleaseImage( &image );
  cvReleaseImage( &temp );
  cvDestroyWindow( "Box Example" );
}
 
// This is our mouse callback.  If the user
// presses the left button, we start a box.
// when the user releases that button, then we
// add the box to the current image.  When the
// mouse is dragged (with the button down) we 
// resize the box.
//
void my_mouse_callback(
   int event, int x, int y, int flags, void* param )
{
 
  IplImage* image = (IplImage*) param;

  switch( event ) {
    case CV_EVENT_MOUSEMOVE: {
      if( drawing_box ) {
        box.width  = x-box.x;
        box.height = y-box.y;
      }
    }
    break;
    case CV_EVENT_LBUTTONDOWN: {
      drawing_box = true;
      box = cvRect( x, y, 0, 0 );
    }
    break;   
    case CV_EVENT_LBUTTONUP: {
      drawing_box = false; 
      if( box.width<0  ) { 
        box.x+=box.width;//保持原点的一致,都为左上角  
        box.width *=-1; 
      }
      if( box.height<0 ) { 
        box.y+=box.height; 
        box.height*=-1; 
      }
      draw_box( image, box );
    }
    break;   
  }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值