1,简介
ROI,感兴趣区域(region of interest),截取图像
2,获取方法
方法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));//截取原图,矩形区域
3,图像叠加
注:Mat类型中copyTo函数和clone函数和赋值操作符操作。
3.1.1,代码1(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() <<endl;
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() <<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));
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() <<endl;
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() <<endl;
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() <<endl;
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();