鼠标绘制矩形
2015-08-27 17:25
274 查看
程序之一,在OpenCV中利用鼠标绘制矩形
效果图如下
程序之二,在OpenCV中利用鼠标绘制矩形并截取该矩形区域的图像
[c-sharp] view
plaincopy
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#pragma comment( lib, "cv.lib" )
#pragma comment( lib, "cxcore.lib" )
#pragma comment( lib, "highgui.lib" )
IplImage* org = 0;
IplImage* img = 0;
IplImage* tmp = 0;
IplImage* dst = 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 )
{
cvCopy(org,img);
sprintf(temp,"(%d,%d)",x,y);
pre_pt = cvPoint(x,y);
cvPutText(img,temp, pre_pt, &font, cvScalar(0,0, 0, 255));
cvCircle( img, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
cvShowImage( "img", img );
cvCopy(img,tmp);
}
else if( event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
{
cvCopy(tmp,img);
sprintf(temp,"(%d,%d)",x,y);
cur_pt = cvPoint(x,y);
cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
cvShowImage( "img", img );
}
else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
{
cvCopy(tmp,img);
sprintf(temp,"(%d,%d)",x,y);
cur_pt = cvPoint(x,y);
cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
cvRectangle(img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
cvShowImage( "img", img );
}
else if( event == CV_EVENT_LBUTTONUP )
{
cvCopy(tmp,img);
sprintf(temp,"(%d,%d)",x,y);
cur_pt = cvPoint(x,y);
cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
cvCircle( img, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
cvRectangle( img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
cvShowImage( "img", img );
cvCopy(img,tmp);
int width=abs(pre_pt.x-cur_pt.x);
int height=abs(pre_pt.y-cur_pt.y);
if(width==0 || height==0)
{
cvDestroyWindow("dst");
return;
}
dst=cvCreateImage(cvSize(width,height),org->depth,org->nChannels);
CvRect rect;
if(pre_pt.x<cur_pt.x && pre_pt.y<cur_pt.y)
{
rect=cvRect(pre_pt.x,pre_pt.y,width,height);
}
else if(pre_pt.x>cur_pt.x && pre_pt.y<cur_pt.y)
{
rect=cvRect(cur_pt.x,pre_pt.y,width,height);
}
else if(pre_pt.x>cur_pt.x && pre_pt.y>cur_pt.y)
{
rect=cvRect(cur_pt.x,cur_pt.y,width,height);
}
else if(pre_pt.x<cur_pt.x && pre_pt.y>cur_pt.y)
{
rect=cvRect(pre_pt.x,cur_pt.y,width,height);
}
cvSetImageROI(org,rect);
cvCopy(org,dst);
cvResetImageROI(org);
cvDestroyWindow("dst");
cvNamedWindow("dst",1);
cvShowImage("dst",dst);
cvSaveImage("dst.jpg",dst);
}
}
int main()
{
org=cvLoadImage("lena.jpg",1);
img=cvCloneImage(org);
tmp=cvCloneImage(org);
cvNamedWindow("img",1);
cvSetMouseCallback( "img", on_mouse, 0 );
cvShowImage("img",img);
cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&org);
cvReleaseImage(&img);
cvReleaseImage(&tmp);
cvReleaseImage(&dst);
return 0;
}
效果图如下
#include <cv.h> #include <highgui.h> #include <stdio.h> IplImage* org = 0; IplImage* img = 0; IplImage* tmp = 0; IplImage* dst = 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) //当鼠标按下时,输出当前的坐标位置,用圆点表示 { cvCopy(org, img); sprintf(temp, "(%d,%d)", x, y); pre_pt = cvPoint(x, y); cvPutText(img, temp, pre_pt, &font, cvScalar(0, 0, 0, 255)); cvCircle(img, pre_pt, 3, cvScalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0); cvShowImage("img", img); cvCopy(img, tmp); } else if (event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON)) //当鼠标移动,并且向左边拖拽的时候,输出鼠标点的位置信息 { cvCopy(tmp, img); sprintf(temp, "(%d,%d)", x, y); cur_pt = cvPoint(x, y); cvPutText(img, temp, cur_pt, &font, cvScalar(0, 0, 0, 255)); cvShowImage("img", img); } else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON)) { cvCopy(tmp, img); sprintf(temp, "(%d,%d)", x, y); cur_pt = cvPoint(x, y); cvPutText(img, temp, cur_pt, &font, cvScalar(0, 0, 0, 255)); cvRectangle(img, pre_pt, cur_pt, cvScalar(0, 255, 0, 0), 1, 8, 0); cvShowImage("img", img); } //当鼠标移动,并且向左边拖拽的时候,画出拖拽的矩形 else if (event == CV_EVENT_LBUTTONUP) { cvCopy(tmp, img); sprintf(temp, "(%d,%d)", x, y); cur_pt = cvPoint(x, y); cvPutText(img, temp, cur_pt, &font, cvScalar(0, 0, 0, 255)); cvCircle(img, cur_pt, 3, cvScalar(255, 0, 0, 0), CV_FILLED, CV_AA, 0); cvRectangle(img, pre_pt, cur_pt, cvScalar(0, 255, 0, 0), 1, 8, 0); cvShowImage("img", img); //当鼠标按键放开时,画出鼠标点的坐标,并且画出鼠标画出的矩形 cvCopy(img, tmp); int width = abs(pre_pt.x - cur_pt.x); int height = abs(pre_pt.y - cur_pt.y); if (width == 0 || height == 0) { cvDestroyWindow("dst"); return; } //查看鼠标画出的矩形的大小,并用改大小表示这一区域的图像 dst = cvCreateImage(cvSize(width, height), org->depth, org->nChannels); CvRect rect; if (pre_pt.x<cur_pt.x && pre_pt.y<cur_pt.y) { rect = cvRect(pre_pt.x, pre_pt.y, width, height); } else if (pre_pt.x>cur_pt.x && pre_pt.y<cur_pt.y) { rect = cvRect(cur_pt.x, pre_pt.y, width, height); } else if (pre_pt.x>cur_pt.x && pre_pt.y>cur_pt.y) { rect = cvRect(cur_pt.x, cur_pt.y, width, height); } else if (pre_pt.x<cur_pt.x && pre_pt.y>cur_pt.y) { rect = cvRect(pre_pt.x, cur_pt.y, width, height); } cvSetImageROI(org, rect); //取图像感兴趣区域的图像,rect里的图像 cvCopy(org, dst); cvResetImageROI(org); cvDestroyWindow("dst"); cvNamedWindow("dst", 1); cvShowImage("dst", dst); cvSaveImage("dst.jpg", dst); } } int main() { org = cvLoadImage("D:6.jpg", 1); img = cvCloneImage(org); tmp = cvCloneImage(org); cvNamedWindow("img", 1); cvSetMouseCallback("img", on_mouse, 0); cvShowImage("img", img); cvWaitKey(0); cvDestroyAllWindows(); cvReleaseImage(&org); cvReleaseImage(&img); cvReleaseImage(&tmp); cvReleaseImage(&dst); return 0; }
效果图如下
程序之二,在OpenCV中利用鼠标绘制矩形并截取该矩形区域的图像
[c-sharp] view
plaincopy
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#pragma comment( lib, "cv.lib" )
#pragma comment( lib, "cxcore.lib" )
#pragma comment( lib, "highgui.lib" )
IplImage* org = 0;
IplImage* img = 0;
IplImage* tmp = 0;
IplImage* dst = 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 )
{
cvCopy(org,img);
sprintf(temp,"(%d,%d)",x,y);
pre_pt = cvPoint(x,y);
cvPutText(img,temp, pre_pt, &font, cvScalar(0,0, 0, 255));
cvCircle( img, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
cvShowImage( "img", img );
cvCopy(img,tmp);
}
else if( event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
{
cvCopy(tmp,img);
sprintf(temp,"(%d,%d)",x,y);
cur_pt = cvPoint(x,y);
cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
cvShowImage( "img", img );
}
else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
{
cvCopy(tmp,img);
sprintf(temp,"(%d,%d)",x,y);
cur_pt = cvPoint(x,y);
cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
cvRectangle(img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
cvShowImage( "img", img );
}
else if( event == CV_EVENT_LBUTTONUP )
{
cvCopy(tmp,img);
sprintf(temp,"(%d,%d)",x,y);
cur_pt = cvPoint(x,y);
cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
cvCircle( img, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
cvRectangle( img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
cvShowImage( "img", img );
cvCopy(img,tmp);
int width=abs(pre_pt.x-cur_pt.x);
int height=abs(pre_pt.y-cur_pt.y);
if(width==0 || height==0)
{
cvDestroyWindow("dst");
return;
}
dst=cvCreateImage(cvSize(width,height),org->depth,org->nChannels);
CvRect rect;
if(pre_pt.x<cur_pt.x && pre_pt.y<cur_pt.y)
{
rect=cvRect(pre_pt.x,pre_pt.y,width,height);
}
else if(pre_pt.x>cur_pt.x && pre_pt.y<cur_pt.y)
{
rect=cvRect(cur_pt.x,pre_pt.y,width,height);
}
else if(pre_pt.x>cur_pt.x && pre_pt.y>cur_pt.y)
{
rect=cvRect(cur_pt.x,cur_pt.y,width,height);
}
else if(pre_pt.x<cur_pt.x && pre_pt.y>cur_pt.y)
{
rect=cvRect(pre_pt.x,cur_pt.y,width,height);
}
cvSetImageROI(org,rect);
cvCopy(org,dst);
cvResetImageROI(org);
cvDestroyWindow("dst");
cvNamedWindow("dst",1);
cvShowImage("dst",dst);
cvSaveImage("dst.jpg",dst);
}
}
int main()
{
org=cvLoadImage("lena.jpg",1);
img=cvCloneImage(org);
tmp=cvCloneImage(org);
cvNamedWindow("img",1);
cvSetMouseCallback( "img", on_mouse, 0 );
cvShowImage("img",img);
cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&org);
cvReleaseImage(&img);
cvReleaseImage(&tmp);
cvReleaseImage(&dst);
return 0;
}
效果图如下
相关文章推荐
- c++对象模型-对象模型
- 给JFinal添加Shiro插件功能,支持Shiro所有注解-使用篇
- c++对象模型-对象模型
- 工作这些年对技术学习过程的一些 总结 与 感悟
- SSIS 目录
- Nginx高级数据结构源码分析(四)-----内存池
- WebSphere MQ&&Active MQ
- 关于Android Canvas.drawText方法中的坐标参数的正确解释
- iOS 7 8 升级问题总结
- JS获取被点击对象id
- JS 合并数组之concat
- socket(AF_INET, SOCK_STREAM…
- error LNK2019: 无法解析的外部符…
- 无法打开预编译头文件:“Release\xx…
- Java: Simulation of printStackTr…
- Java: Exception Trace Kick off
- Java: Snack from Constructor
- Java: A Puzzle about Constructor…
- Java: Instance Initialization
- Java: Keyword final