例4-1:用鼠标在窗口中画方形的程序
#include"pch.h"//看情况加
#include <cv.h>
#include <highgui.h>
CvRect box; //定义绘制的矩形
bool drawing_box = false; //状态-鼠标正在移动
void draw_box(IplImage* img, CvRect rect){
//绘制矩形
cvRectangle(
img, cvPoint(rect.x, rect.y), cvPoint(rect.x+ rect.width, rect.y+ rect.height),
cvScalar(0xff,0xf0,0x0f));
}
//鼠标响应函数-回调函数
void my_mouse_callback(
int event, int x, int y, int flags, void* param)
{
IplImage* img = (IplImage*)param;
switch (event)
{
case CV_EVENT_MOUSEMOVE: //mouse move
if (drawing_box) {
box.width = x - box.x;
box.height = y - box.y;
}
break;
case CV_EVENT_LBUTTONDOWN: //left button be pressed
drawing_box = true; //drawing flag set 1
box = cvRect(x, y, 0, 0);
break;
case CV_EVENT_LBUTTONUP: //left button be released
drawing_box = false;
// redefine the left position of box
if (box.width < 0) {
box.x +&#