【问题标题】:open cv, rectangle region of interest referencingopencv,矩形感兴趣区域参考
【发布时间】:2013-02-28 09:39:25
【问题描述】:

我想从图像中创建一个具有特定坐标宽度和高度的矩形。

然后拆分通道,最后对该区域中的给定通道进行阈值处理。

我的问题是我想引用图像上的一个区域(矩形)。

这是我的代码: cv::矩形 myROI(10, 20, 50, 50);
cv::Mat croppedImage = org_image(myROI);

在我对这个croppedImage 进行操作后,我还想要该操作对org_image 的影响。

我想引用该区域而不是复制该区域(引用它)。

我该怎么做?


编辑 1:

首先感谢您的回答。当我使用上面的代码时,我还需要一个小解决方案
cv::threshold(croppedImage,croppedImage, thresh, 255, type);

矩形区域被阈值化(但所有通道),但不是我想要的特定通道,例如我只想对蓝色通道进行阈值化,我尝试了所有方法,它不起作用。

我尝试分割矩形,但它不起作用。

顺便说一句,合并分割图像的解决方案也可以吗?这可以帮助我 如果我可以通过仅引用croppedImage 进行拆分,我认为它会解决我的问题。

所以我只需要在一个投资回报率中设置一个通道,请给我一个完整的解决方案。而且我必须使用拆分。

谢谢


编辑 2

所以它不起作用,请在此处查看我的代码... 对分割通道的操作对裁剪后的图像和原始图像也没有任何影响。

  Mat org_image = image.clone();
  cv::Rect myROI(0, 0, 5, 5);
  cv::Mat croppedImage = org_image(myROI);
  std::vector<cv::Mat> img_split(3);
  split(croppedImage, img_split);

  cv::threshold(img_split[1], img_split[1], thresh, 255, 0);

  for(int i=0; i < 3; i++)
  printf("img_split[%d].data[0] ==> adress: %p, ==> data: %d\n", i, &img_split[i].data[0], img_split[i].data[0]);
  printf("+++++++++++++++++\n");        
  for(int i=0; i < 3; i++)
  printf("     croppedImage[%d] ==> adress: %p, ==> data: %d\n", i, &croppedImage.data[i], croppedImage.data[i]);      
  printf("+++++++++++++++++\n");    
  for(int i=0; i < 3; i++)
  printf("org_image[%d] ==> adress: %p, data ->  %d\n", i, &org_image.data[i], org_image.data[i]);

输出:

img_split[0].data[0] ==> adress: 0x846e8a0, ==> data: 212
img_split[1].data[0] ==> adress: 0x846e8d0, ==> data: 255
img_split[2].data[0] ==> adress: 0x846e910, ==> data: 220
+++++++++++++++++
     croppedImage[0] ==> adress: 0xb468d010, ==> data: 212
     croppedImage[1] ==> adress: 0xb468d011, ==> data: 220
     croppedImage[2] ==> adress: 0xb468d012, ==> data: 220
+++++++++++++++++
org_image[0] ==> adress: 0xb468d010, data ->  212
org_image[1] ==> adress: 0xb468d011, data ->  220
org_image[2] ==> adress: 0xb468d012, data ->  220

我没有更多的想法...... :-(

【问题讨论】:

  • 除非你明确地clone() a cv::Mat,否则内存是共享的。对 ROI 执行操作应该会影响原始图像。你是说你的代码还没有这样做吗?

标签: opencv reference area threshold roi


【解决方案1】:

实际上,在你对这个croppedImage 进行操作之后,该操作会对org_image 产生影响。您可以为此使用特殊的构造函数:

Mat::Mat(const Mat& m, const Rect& roi)

示例如下:

uchar vals[] = {1,1,1,1,
                1,1,1,1,
                1,1,1,1,
                1,1,1,1};
cv::Mat_<uchar> A(4,4,vals);
std::cout << "before:\n" << A << std::endl;

cv::Rect roi(1,1,2,2);
cv::Mat_<uchar> B( A, roi );
B.setTo(2);

std::cout << "after:\n" << A << std::endl;

输出:

before:
 [1, 1, 1, 1;
  1, 1, 1, 1;
  1, 1, 1, 1;
  1, 1, 1, 1]
after:
 [1, 1, 1, 1;
  1, 2, 2, 1;
  1, 2, 2, 1;
  1, 1, 1, 1]

【讨论】:

    猜你喜欢
    • 2018-12-02
    • 2013-03-03
    • 1970-01-01
    • 2012-04-25
    • 2013-10-19
    • 2017-09-28
    • 2014-12-23
    • 2011-08-10
    相关资源
    最近更新 更多