//自己写的代码,拖动鼠标截取一部分图像
#include <cvh>
#include <highguih>
bool drawing = false;
CvRect rect;
CvPoint origin;
IplImage src;
IplImage dst;
void onMouse(int event,int x,int y,int flags,void param = NULL);
int main(int argc, char argv[])
{
src = cvLoadImage("D:\\Lenajpg", -1);
cvNamedWindow("源",1);
cvSetMouseCallback("源",onMouse,NULL);//捕捉鼠标
cvShowImage("源",src);
cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&src);
cvReleaseImage(&dst);
return 0;
}
//鼠标事件处理函数
void onMouse(int event,int x,int y,int flags,void param)
{
if(event == CV_EVENT_LBUTTONDOWN && !CV_EVENT_MOUSEMOVE)
{
drawing = true;
origin=cvPoint(x,y);
rect=cvRect(x,y,0,0);
}
else if(event==CV_EVENT_MOUSEMOVE)
{
if(drawing)
{
rectx=MIN(originx, x);
recty=MIN(originy,y);
rectheight = abs(originx-x);
rectwidth = abs(originy-y);
}
}
else if(event == CV_EVENT_LBUTTONUP )
{
drawing = false;
if(rectheight==0 || rectwidth==0)
{
cvDestroyWindow("截图");
return;
}
dst=cvCreateImage(cvSize(rectwidth,rectheight),src->depth,src->nChannels);
cvSetImageROI(src,rect);
cvCopy(src,dst);
cvResetImageROI(src);
cvNamedWindow("截图",1);
cvShowImage("截图",dst);
cvSaveImage("截图jpg",dst);
cvWaitKey(0);
cvDestroyWindow("截图");
}
}
以上就是关于opencv如何在显示的窗口中用鼠标提取一块区域全部的内容,包括:opencv如何在显示的窗口中用鼠标提取一块区域、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)