ROI,感兴趣区域(region of interest),截取图像
方法1:使用Rect
- cv::Mat src=cv::imread("*.bmp");//读取原图
- cv::Mat matROI = src(cv::Rect(100,200,50,100));//截取原图,矩形区域
方法2:使用Range
- cv::Mat src=cv::imread("*.bmp");//读取原图
- cv::Mat matROI = src(cv::Range(100,150),cv::Range(200,300));//截取原图,矩形区域
注:Mat类型中copyTo函数和clone函数和赋值操作符操作。
- stringstream ss;
- cv::Mat src = cv::imread("BK5.jpg");//3通道彩图
- cv::Mat* srcPointer = &src;
- cv::Mat srcROI = src(cv::Rect(src.rows / 2 - 100, src.cols / 2 - 100, 100, 100));
- ss << "src.isContinuous=" << src.isContinuous() <
- ss << "src.type=" << src.type() << endl;
- ss << "srcROI - Memery Address:" << &srcROI << endl;
- cv::Mat srvROI_End = cv::Mat(srcROI.rows, srcROI.cols, srcROI.type(), cv::Scalar(0));
- srvROI_End.copyTo(srcROI);
- ss << "srcROI - Memery Address:" << &srcROI << endl;
- cv::imshow("srcROI", srcROI);
- cv::imshow("src-Result", src);
- qDebug() << ss.str().c_str();
指针版
- stringstream ss;
- cv::Mat src = cv::imread("BK5.jpg");//3通道彩图
- cv::Mat srcROI = src(cv::Rect(src.rows / 2 - 100, src.cols / 2 - 100, 100, 100));
- cv::Mat* srcROIPointer = &srcROI;
- ss << "src.isContinuous=" << src.isContinuous() <
- ss << "src.type=" << src.type() << endl;
- ss << "srcROI - Memery Address:" << &srcROI << endl;
- cv::Mat srcROI_End = cv::Mat(srcROIPointer->rows, srcROIPointer->cols, srcROIPointer->type(), cv::Scalar(0));
- srcROI_End.copyTo(*srcROIPointer);
- ss << "srcROI - Memery Address:" << &srcROI << endl;
- cv::imshow("srcROI", *srcROIPointer);
- cv::imshow("src-Result", src);
- qDebug() << ss.str().c_str();
3.1.2,打印结果
src.isContinuous=1
src.type=16
srcROI - Memery Address:000000EF810FF980
srcROI - Memery Address:000000EF810FF980
3.1.3,图片显示结果
原图的ROI区域,图片被处理。
3.1.4,结论
类型/尺寸相同,copyTo时,ROI图元修改会生效到原图中。
3.2.1,代码2(copyTo,类型不同,尺寸相同)
- stringstream ss;
- cv::Mat src = cv::imread("BK5.jpg");//3通道彩图
- cv::Mat* srcPointer = &src;
- cv::Mat srcROI = src(cv::Rect(src.rows / 2 - 100, src.cols / 2 - 100, 100, 100));
- ss << "src.isContinuous=" << src.isContinuous() <
- ss << "src.type=" << src.type() << endl;
- ss << "srcROI - Memery Address:" << &srcROI << endl;
- cv::Mat srvROI_End = cv::Mat(srcROI.rows, srcROI.cols, CV_8UC1, cv::Scalar(0));
- srvROI_End.copyTo(srcROI);
- ss << "srcROI - Memery Address:" << &srcROI << endl;
- cv::imshow("srcROI", srcROI);
- cv::imshow("src-Result", src);
- qDebug() << ss.str().c_str();
3.2.2,打印结果
src.isContinuous=1
src.type=16
srcROI - Memery Address:000000EF810FF980
srcROI - Memery Address:000000EF810FF980
3.2.3,图片显示结果
原图的ROI区域,无处理效果。
3.2.4,结论
类型不同,尺寸相同,copyTo时,ROI图元不会生效到原图中。
3.3.1,代码2(copyTo,类型不同,尺寸相同)
- stringstream ss;
- cv::Mat src = cv::imread("BK5.jpg");//3通道彩图
- cv::Mat* srcPointer = &src;
- cv::Mat srcROI = src(cv::Rect(src.rows / 2 - 100, src.cols / 2 - 100, 100, 100));
- ss << "src.isContinuous=" << src.isContinuous() <
- ss << "src.type=" << src.type() << endl;
- ss << "srcROI - Memery Address:" << &srcROI << endl;
- cv::Mat srvROI_End = cv::Mat(srcROI.rows+100, srcROI.cols+100, srcROI.type(), cv::Scalar(0));
- srvROI_End.copyTo(srcROI);
- ss << "srcROI - Memery Address:" << &srcROI << endl;
- cv::imshow("srcROI", srcROI);
- cv::imshow("src-Result", src);
- qDebug() << ss.str().c_str();
3.3.2,打印结果
src.isContinuous=1
src.type=16
srcROI - Memery Address:000000FC6E10F8E0
srcROI - Memery Address:000000FC6E10F8E0
3.3.3,图片显示结果
原图的ROI区域,无处理效果。
3.3.4,结论
类型相同,尺寸不同,copyTo时,ROI图元不会生效到原图中。
3.4.1,代码2(clone,类型+尺寸相同)
- stringstream ss;
- cv::Mat src = cv::imread("BK5.jpg");//3通道彩图
- cv::Mat* srcPointer = &src;
- cv::Mat srcROI = src(cv::Rect(src.rows / 2 - 100, src.cols / 2 - 100, 100, 100));
- ss << "src.isContinuous=" << src.isContinuous() <
- ss << "src.type=" << src.type() << endl;
- ss << "srcROI - Memery Address:" << &srcROI << endl;
- cv::Mat srcROI_End = cv::Mat(srcROI.rows, srcROI.cols, srcROI.type(), cv::Scalar(0));
- srcROI = srcROI_End.clone();
- ss << "srcROI - Memery Address:" << &srcROI << endl;
- cv::imshow("srcROI", srcROI);
- cv::imshow("src-Result", src);
- qDebug() << ss.str().c_str();
3.4.2,打印结果
src.isContinuous=1
src.type=16
srcROI - Memery Address:00000006268FF560
srcROI - Memery Address:00000006268FF560
3.4.3,图片显示结果
原图的ROI区域,无处理效果。
3.4.4,结论
clone时,ROI图元不会生效到原图中。
4,制作掩膜
4.1.1,代码1(copyTo,mask)
绿色掩膜
- stringstream ss;
- cv::Mat src = cv::imread("BK5.jpg");//3通道彩图
- cv::Mat srcROI = src(cv::Rect(src.rows / 2 - 100, src.cols / 2 - 100, 100, 100));
- cv::Mat* srcROIPointer = &srcROI;
- ss << "src.isContinuous=" << src.isContinuous() << endl;
- ss << "src.type=" << src.type() << endl;
- ss << "srcROI - Memery Address:" << &srcROI << endl;
- cv::Mat srcROI_End = cv::Mat(srcROIPointer->rows, srcROIPointer->cols, srcROIPointer->type(), cv::Scalar(0, 255, 0));
- cv::Mat srcROI_Mask = cv::Mat(srcROIPointer->rows, srcROIPointer->cols, srcROIPointer->type(), cv::Scalar(0, 255, 0));
- srcROI_End.copyTo(*srcROIPointer, srcROI_Mask);
- ss << "srcROI - Memery Address:" << &srcROI << endl;
- cv::imshow("srcROI", *srcROIPointer);
- cv::imshow("src-Result", src);
- qDebug() << ss.str().c_str();
-
相关阅读:
postgres字符串转数字
什么是 PWA
cannot import name ‘container_abcs’ from ‘torch._six’
微信支付——微信退款实战教程(Java版)
【Python百日进阶-WEB开发】Day174 - Django案例:06用户注册后端逻辑
ArrayList详解
.NET宝藏API之:OutputFormatter,格式化输出对象
VSCode远程调试Python代码环境配置
DeepStream--测试代码
Jetson Orin平台4-16路 GMSL2/GSML1相机采集套件推荐
-
原文地址:https://blog.csdn.net/FairyTailQ/article/details/134216474