【问题标题】:substract region from image and keep the borders从图像中减去区域并保留边界
【发布时间】:2015-12-10 01:31:42
【问题描述】:

我有一个 200x200 像素的图像,我想只保留其中某个区域的数据。 检查下图:

整个正方形是 200x200 像素。我想从中删除较小的正方形(白色)。所以,只保留蓝色区域中包含的信息。但是,我想保留 200x200 尺寸。

我试过了:

    Mat whiteArea;
    whiteArea = ImageInitial( Range(50,200) , Range(50,200) );

    Size size(200,200);
    Mat dst;
    resize(whiteArea,dst,size);

    Mat FinalImage;
    subtract(ImageInitial,dst,FinalImage); 

我正在调整白色区域的大小,因为我想从初始图像中减去它。 我的问题是它给了我初始图像。

也许调整大小是问题。但是如何减去 2 个不同大小的图像?

【问题讨论】:

  • 所以您只想从自身中减去图像的内部部分?并且那个内部从像素位置(50,50)到(250,250)?

标签: c++ opencv


【解决方案1】:

尝试使用子图像或使用蒙版:

// use a roi (nice if your target area is rectangular and you know the position)
Rect whiteArea = Rect(50,50, 200,200); // creates a roi of the inner rect

Mat FinalImage = ImageInitial.clone();
// now set the roi area to zero:
FinalImage (whiteArea).setTo(Scalar(0,0,0));
// or FinalImage(whiteArea) = FinalImage(whiteArea) - FinalImage(whiteArea);

imshow("version 1 with subimage", FinalImage);
waitkey(0);


// or use a mask (nice if that region can has arbitrary shape etc and you have to extract it first):
Scalar lowerColorBound = Scalar(x,y,z); //some BGR values to find the color you want to eliminate
Scalar upperColorBound = Scalar(a,b,c); //some BGR values to find the color you want to eliminate
Mat mask;
inRange(ImageInitial, lowerColorBound, upperColorBound  mask)
// use the mask for subtraction:
subtract(ImageInitial, ImageInitial, FinalImage , mask);

imshow("version 2 with mask", FinalImage);
waitkey(0);

【讨论】:

  • :太好了,谢谢!(第二个版本我无法运行它,因为它给了我Mat’ has no member named ‘inRange’
  • :好的,它现在可以工作了,谢谢。我想问一下,在第一个版本中,我们可以用垃圾值填充 whiteArea setTo(Scalar(0,0,0)); 吗?Nan 值或其他东西?还有一个图像在哪里我可以做计算吗?(我现在才问!:))
  • nan 只为浮点值定义。整数类型不知道某事。像那样。因此,要么转换为 float/double 并设置为 nan,要么记住您知道垃圾的位置(​​例如掩码)并将该信息提供给后续处理步骤(那么掩码不是垃圾而是“好”像素可能更方便)
猜你喜欢
  • 2019-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-08
  • 1970-01-01
  • 2016-10-08
  • 1970-01-01
相关资源
最近更新 更多