• 叁[3],感兴趣区域ROI


    1,简介


    ROI,感兴趣区域(region of interest),截取图像

    2,获取方法


    方法1:使用Rect

    1. cv::Mat src=cv::imread("*.bmp");//读取原图
    2. cv::Mat matROI = src(cv::Rect(100,200,50,100));//截取原图,矩形区域


    方法2:使用Range

    1. cv::Mat src=cv::imread("*.bmp");//读取原图
    2. cv::Mat matROI = src(cv::Range(100,150),cv::Range(200,300));//截取原图,矩形区域

    3,图像叠加


    注:Mat类型中copyTo函数和clone函数和赋值操作符操作。


    3.1.1,代码1(copyTo,类型+尺寸相同)

    1. stringstream ss;
    2. cv::Mat src = cv::imread("BK5.jpg");//3通道彩图
    3. cv::Mat* srcPointer = &src;
    4. cv::Mat srcROI = src(cv::Rect(src.rows / 2 - 100, src.cols / 2 - 100, 100, 100));
    5. ss << "src.isContinuous=" << src.isContinuous() <
    6. ss << "src.type=" << src.type() << endl;
    7. ss << "srcROI - Memery Address:" << &srcROI << endl;
    8. cv::Mat srvROI_End = cv::Mat(srcROI.rows, srcROI.cols, srcROI.type(), cv::Scalar(0));
    9. srvROI_End.copyTo(srcROI);
    10. ss << "srcROI - Memery Address:" << &srcROI << endl;
    11. cv::imshow("srcROI", srcROI);
    12. cv::imshow("src-Result", src);
    13. qDebug() << ss.str().c_str();


    指针版

    1. stringstream ss;
    2. cv::Mat src = cv::imread("BK5.jpg");//3通道彩图
    3. cv::Mat srcROI = src(cv::Rect(src.rows / 2 - 100, src.cols / 2 - 100, 100, 100));
    4. cv::Mat* srcROIPointer = &srcROI;
    5. ss << "src.isContinuous=" << src.isContinuous() <
    6. ss << "src.type=" << src.type() << endl;
    7. ss << "srcROI - Memery Address:" << &srcROI << endl;
    8. cv::Mat srcROI_End = cv::Mat(srcROIPointer->rows, srcROIPointer->cols, srcROIPointer->type(), cv::Scalar(0));
    9. srcROI_End.copyTo(*srcROIPointer);
    10. ss << "srcROI - Memery Address:" << &srcROI << endl;
    11. cv::imshow("srcROI", *srcROIPointer);
    12. cv::imshow("src-Result", src);
    13. 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,类型不同,尺寸相同)

    1. stringstream ss;
    2. cv::Mat src = cv::imread("BK5.jpg");//3通道彩图
    3. cv::Mat* srcPointer = &src;
    4. cv::Mat srcROI = src(cv::Rect(src.rows / 2 - 100, src.cols / 2 - 100, 100, 100));
    5. ss << "src.isContinuous=" << src.isContinuous() <
    6. ss << "src.type=" << src.type() << endl;
    7. ss << "srcROI - Memery Address:" << &srcROI << endl;
    8. cv::Mat srvROI_End = cv::Mat(srcROI.rows, srcROI.cols, CV_8UC1, cv::Scalar(0));
    9. srvROI_End.copyTo(srcROI);
    10. ss << "srcROI - Memery Address:" << &srcROI << endl;
    11. cv::imshow("srcROI", srcROI);
    12. cv::imshow("src-Result", src);
    13. 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,类型不同,尺寸相同)

    1. stringstream ss;
    2. cv::Mat src = cv::imread("BK5.jpg");//3通道彩图
    3. cv::Mat* srcPointer = &src;
    4. cv::Mat srcROI = src(cv::Rect(src.rows / 2 - 100, src.cols / 2 - 100, 100, 100));
    5. ss << "src.isContinuous=" << src.isContinuous() <
    6. ss << "src.type=" << src.type() << endl;
    7. ss << "srcROI - Memery Address:" << &srcROI << endl;
    8. cv::Mat srvROI_End = cv::Mat(srcROI.rows+100, srcROI.cols+100, srcROI.type(), cv::Scalar(0));
    9. srvROI_End.copyTo(srcROI);
    10. ss << "srcROI - Memery Address:" << &srcROI << endl;
    11. cv::imshow("srcROI", srcROI);
    12. cv::imshow("src-Result", src);
    13. 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,类型+尺寸相同)

    1. stringstream ss;
    2. cv::Mat src = cv::imread("BK5.jpg");//3通道彩图
    3. cv::Mat* srcPointer = &src;
    4. cv::Mat srcROI = src(cv::Rect(src.rows / 2 - 100, src.cols / 2 - 100, 100, 100));
    5. ss << "src.isContinuous=" << src.isContinuous() <
    6. ss << "src.type=" << src.type() << endl;
    7. ss << "srcROI - Memery Address:" << &srcROI << endl;
    8. cv::Mat srcROI_End = cv::Mat(srcROI.rows, srcROI.cols, srcROI.type(), cv::Scalar(0));
    9. srcROI = srcROI_End.clone();
    10. ss << "srcROI - Memery Address:" << &srcROI << endl;
    11. cv::imshow("srcROI", srcROI);
    12. cv::imshow("src-Result", src);
    13. 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)


    绿色掩膜

    1. stringstream ss;
    2. cv::Mat src = cv::imread("BK5.jpg");//3通道彩图
    3.     cv::Mat srcROI = src(cv::Rect(src.rows / 2 - 100, src.cols / 2 - 100, 100, 100));
    4.     cv::Mat* srcROIPointer = &srcROI;
    5.     ss << "src.isContinuous=" << src.isContinuous() << endl;
    6.     ss << "src.type=" << src.type() << endl;
    7.     ss << "srcROI - Memery Address:" << &srcROI << endl;
    8.     cv::Mat srcROI_End = cv::Mat(srcROIPointer->rows, srcROIPointer->cols, srcROIPointer->type(), cv::Scalar(0, 255, 0));
    9.     cv::Mat srcROI_Mask = cv::Mat(srcROIPointer->rows, srcROIPointer->cols, srcROIPointer->type(), cv::Scalar(0, 255, 0));
    10.     srcROI_End.copyTo(*srcROIPointer, srcROI_Mask);
    11.     ss << "srcROI - Memery Address:" << &srcROI << endl;
    12.     cv::imshow("srcROI", *srcROIPointer);
    13.     cv::imshow("src-Result", src);
    14.     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