用鼠标截矩形图像并保存

来源:互联网 发布:淘宝实名认证的截图 编辑:程序博客网 时间:2024/06/07 13:05


  摘要: 接着上一个实验,此次实验想把鼠标截取(框住)的图片分开显示出来,但是没有达到运行效果。

关键字:cvSetImageRol()

1.void cvSetImageROI(IplImage* image,cvRect rect); 

说明:基于给定的矩形设置图像的 ROI(感兴趣区域,region of interesting)

CvRect结构:包含4个数据成员,x,y,width,height,通过定义矩形左上角坐标和矩形的宽和高来确定一个矩形。 

typedef struct CvRect 

{

   int x; /* 方形的左上角的x-坐标 */ 

   int y; /* 方形的左上角的y-坐标*/

 int width; /* 宽 */ 

 int height; /* 高 */ 

  } 
x,y用来确定区域左上角的坐标 
width和height为矩形区域的宽和高

            在上一次的代码当中添加了如下部分;

int width=0, height=0;
dst = cvCreateImage(cvSize(width, height), src->depth, src->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(src, rect);
cvCopy(src, dst);
cvResetImageROI(src);
cvDestroyWindow("dst");
cvNamedWindow("dst", 1);
cvShowImage("dst", dst);
cvSaveImage("dst.jpg", dst);
}



运行结果如下:


       在网上搜索的这个问题有如下几个说法,首先可能是这两个图或者矩阵初始化了,如果初始化了,初始化的大小必须一样,否则无法运用,还有两者的深度也要一致,即通道数要相同。

       


阅读全文
0 0
原创粉丝点击