从图像中减去区域并保留边框 [英] substract region from image and keep the borders

查看:96
本文介绍了从图像中减去区域并保留边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a 200x200 pixels image and I want to keep only the data for a certain region inside it. Check the following image:

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

The whole out square is 200x200 pixels.I want to remove from it the smaller square(white).So ,keep only the information that is included in the blue area.But , I want to keep the 200x200 dimensions.

我试过:

    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); 

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

I am resizing the white area because I want to substract it from the initial image. My problem is that it gives me the initial image.

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

Maybe the resize is the problem .but then how to substract 2 different sized images?

推荐答案

尝试使用子图像或使用掩码:

try to use subimages or use a mask:

// 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);

这篇关于从图像中减去区域并保留边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆