本文实例为大家分享了OpenCV实现鼠标框选并显示框选区域的具体代码,供大家参考,具体内容如下
鼠标在图像上框选目标:
1. 用到鼠标回调函数
2. 回调函数里面分三部分
(1)鼠标按下左键
(2)鼠标按下的同时移动左键
(3)鼠标左键弹起
3. 图像显示的刷新
单目标:
#include
#include
#include
IplImage* src = 0;
IplImage* tmp = 0;
void on_mouse( int event, int x, int y, int flags, void* ustc)
{
static CvPoint pre_pt = {-1,-1};
static CvPoint cur_pt = {-1,-1};
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);//初始化字体
char temp[16];
if( (event == CV_EVENT_LBUTTONDOWN)&&(flags) )//鼠标左键按下时
{
sprintf(temp,"(%d,%d)",x,y);//格式化字符串
pre_pt = cvPoint(x,y);//获取当前点坐标值
cvPutText(src,temp, pre_pt, &font, cvScalar(0,0, 0, 255));//在图像是打印字符
cvCircle( src, pre_pt, 2,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );//在图像上画圆
cvShowImage( "src", src );
//cvCopy(src,tmp);//这句有没有,就是单目标和多目标的问题
}
else if( (event == CV_EVENT_MOUSEMOVE) && (flags & CV_EVENT_LBUTTONDOWN))
{//鼠标移动并且鼠标左键按下
sprintf(temp,"(%d,%d)",x,y);//格式化字符串
cur_pt = cvPoint(x,y);//获取当前点坐标值
cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));//在图像是打印字符
cvRectangle(src, pre_pt, cur_pt, cvScalar(0,255,0,0), 2, 8, 0 );//在图像上画矩形
cvShowImage( "src", src );
cvCopy(tmp,src);//将img复制到临时图像tmp上,用于实时显示
}
else if( event == CV_EVENT_LBUTTONUP )
{//鼠标左键弹起
sprintf(temp,"(%d,%d)",x,y);//字体格式化
cur_pt = cvPoint(x,y);//获取当前点坐标值
cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));//在图像是打印字符
cvCircle( src, cur_pt, 2,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );//在图像上画圆
cvRectangle( src, pre_pt, cur_pt, cvScalar(0,255,0,0), 2, 8, 0 );//在图像上画矩形
cvShowImage( "src", src );
}
}
int main()
{
src=cvLoadImage("lena.jpg",1);//读入图像
tmp=cvCloneImage(src);//复制图像到临时图像上
cvNamedWindow("src",1);//新建窗口
cvSetMouseCallback( "src", on_mouse, 0 );//注册鼠标响应回调函数
cvShowImage("src",src);//显示图像
cvWaitKey(0);//等待按键按下
cvDestroyAllWindows();//销毁所有窗口
cvReleaseImage(&src);//释放图像
cvReleaseImage(&tmp);//释放图像
return 0;
}
多目标:
#include
#include
#include
IplImage* src = 0;
IplImage* tmp = 0;
void on_mouse( int event, int x, int y, int flags, void* ustc)
{
static CvPoint pre_pt = {-1,-1};
static CvPoint cur_pt = {-1,-1};
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);//初始化字体
char temp[16];
if( (event == CV_EVENT_LBUTTONDOWN)&&(flags) )//鼠标左键按下时
{
sprintf(temp,"(%d,%d)",x,y);//格式化字符串
pre_pt = cvPoint(x,y);//获取当前点坐标值
cvPutText(src,temp, pre_pt, &font, cvScalar(0,0, 0, 255));//在图像是打印字符
cvCircle( src, pre_pt, 2,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );//在图像上画圆
cvShowImage( "src", src );
cvCopy(src,tmp);//这句有没有,就是单目标和多目标的问题
}
else if( (event == CV_EVENT_MOUSEMOVE) && (flags & CV_EVENT_LBUTTONDOWN))
{//鼠标移动并且鼠标左键按下
sprintf(temp,"(%d,%d)",x,y);//格式化字符串
cur_pt = cvPoint(x,y);//获取当前点坐标值
cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));//在图像是打印字符
cvRectangle(src, pre_pt, cur_pt, cvScalar(0,255,0,0), 2, 8, 0 );//在图像上画矩形
cvShowImage( "src", src );
cvCopy(tmp,src);//将img复制到临时图像tmp上,用于实时显示
}
else if( event == CV_EVENT_LBUTTONUP )
{//鼠标左键弹起
sprintf(temp,"(%d,%d)",x,y);//字体格式化
cur_pt = cvPoint(x,y);//获取当前点坐标值
cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));//在图像是打印字符
cvCircle( src, cur_pt, 2,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );//在图像上画圆
cvRectangle( src, pre_pt, cur_pt, cvScalar(0,255,0,0), 2, 8, 0 );//在图像上画矩形
cvShowImage( "src", src );
}
}
int main()
{
src=cvLoadImage("lena.jpg",1);//读入图像
tmp=cvCloneImage(src);//复制图像到临时图像上
cvNamedWindow("src",1);//新建窗口
cvSetMouseCallback( "src", on_mouse, 0 );//注册鼠标响应回调函数
cvShowImage("src",src);//显示图像
cvWaitKey(0);//等待按键按下
cvDestroyAllWindows();//销毁所有窗口
cvReleaseImage(&src);//释放图像
cvReleaseImage(&tmp);//释放图像
return 0;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。