gpt4 book ai didi

c++ - 从图像中减去区域并保留边界

转载 作者:搜寻专家 更新时间:2023-10-31 02:18:48 24 4
gpt4 key购买 nike

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

Image

整个正方形是 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 个不同大小的图像?

最佳答案

尝试使用子图像或使用 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);

关于c++ - 从图像中减去区域并保留边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34184319/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com