c++ opencv roi
【摘要】
最新的: cv::Mat img; a= cv::Mat m0 = img(cv::Rect(i0, i1, w, h))
以前的:
//opencv#include "opencv2/opencv.hpp"#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/im...
最新的:
cv::Mat img;
a=
cv::Mat m0 = img(cv::Rect(i0, i1, w, h))
以前的:
-
-
-
//opencv
-
#include "opencv2/opencv.hpp"
-
#include "opencv2/highgui/highgui.hpp"
-
#include "opencv2/imgproc/imgproc.hpp"
-
-
/******************************************************************************************
-
Function: Screenshot
-
Description: 矩形截图
-
Input: src:原图片 rect:截图范围
-
Output: dst:截图后的图片
-
Return: 截图成功返回true,失败返回false
-
*******************************************************************************************/
-
bool Screenshot(IplImage* src, IplImage* dst, CvRect rect)
-
{
-
try {
-
cvSetImageROI(src, rect);
-
cvCopy(src, dst, 0);
-
cvResetImageROI(src);
-
return true;
-
}
-
-
catch (cv::Exception e)
-
{
-
}
-
}
-
-
/******************************************************************************************
-
Function: SafeResetSizeOfRect
-
Description: 安全重置矩形大小
-
Input: src:原图片 rect:截图范围
-
Return: 无
-
*******************************************************************************************/
-
void SafeResetSizeOfRect(IplImage* src, CvRect& rect)
-
{
-
try
-
{
-
rect.x = rect.x < 0 ? 0 : rect.x;
-
rect.y = rect.y < 0 ? 0 : rect.y;
-
rect.width = rect.width < 0 ? 0 : rect.width;
-
rect.height = rect.height < 0 ? 0 : rect.height;
-
-
if (rect.x > src->width || rect.y > src->height)
-
{
-
rect = cvRect(0, 0, src->width, src->height);
-
}
-
rect.width = std::min(rect.width, src->width - rect.x);
-
rect.height = std::min(rect.height, src->height - rect.y);
-
}
-
-
catch (cv::Exception e)
-
{
-
}
-
}
-
//调用举例
-
IplImage *src = 0;
-
IplImage *dst = 0;
-
src = cvLoadImage("D:\\1.jpg", CV_LOAD_IMAGE_COLOR);
-
//定义截图范围
-
CvRect rect = cvRect(720, 610, 300, 320);
-
//截图
-
SafeResetSizeOfRect(src, rect);
-
dst = cvCreateImage(cvSize(rect.width, rect.height), src->depth, src->nChannels);
-
Screenshot(src, dst, rect);
-
//保存图片
-
cvSaveImage("D:\\rect.jpg", dst);
-
//释放内存
-
cvReleaseImage(&src);
-
cvReleaseImage(&dst);
文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/106267174
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)