#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 //声明鼠标事件的回调函数;
//event0 代表鼠标事件;x,y代表发生鼠标事件时鼠标位置坐标;flags表示发生时的状态;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(0x00,0x00,0xff) /* red */
);
}
int main( int argc, char* argv[] ) {
box = cvRect(-1,-1,0,0);
IplImage* image = cvCreateImage( //定义图像:200X200,8位,三通道
cvSize(400,400),
IPL_DEPTH_8U,
3
);
cvZero( image );//图像背景为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.
//声明回调函数,把整个图像作为参数传入
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 ) {
cvCopyImage( image, temp );
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;
}
}
#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 //声明鼠标事件的回调函数;
//event0 代表鼠标事件;x,y代表发生鼠标事件时鼠标位置坐标;flags表示发生时的状态;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(0x00,0x00,0xff) /* red */
);
}
int main( int argc, char* argv[] ) {
box = cvRect(-1,-1,0,0);
IplImage* image = cvCreateImage( //定义图像:200X200,8位,三通道
cvSize(400,400),
IPL_DEPTH_8U,
3
);
cvZero( image );//图像背景为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.
//声明回调函数,把整个图像作为参数传入
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 ) {
cvCopyImage( image, temp );
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;
}
}