洪水填充图像的图像处理

时间:2016-03-15 13:00:04

标签: c++ opencv image-processing

我正在开展一个图像处理项目,在这个项目中,我已经填充了原始图像。

现在

  1. 我需要去除此图片中的噪点,即手的图像周围的白线。我想通过将它们合并为黑色的背景颜色来删除这些白线。

  2. 我需要将泛滥区域的灰色(值127)更改为白色。请注意,背景颜色应保持黑色。

  3. enter image description here

    这是this question的后续行动。使用this answer中的代码获取图像。

1 个答案:

答案 0 :(得分:3)

可以在your previous question中找到生成问题中图片的代码。

所以我们知道充满洪水的地区有价值127

从此图片开始,您可以轻松获取充满洪水的区域的掩码:

Mat1b mask = (img == 127);

enter image description here

单通道蒙版的值为黑色0或白色255

如果你想要一个彩色图像,你需要创建一个与img大小相同的黑色初始化图像,并根据掩码将像素设置为你喜欢的颜色(这里是绿色):

// Black initialized image, same size as img
Mat3b out(img.rows, img.cols, Vec3b(0,0,0)); 

Scalar some_color(0,255,0);
out.setTo(some_color, mask);

enter image description here

参考代码:

#include <opencv2/opencv.hpp>
using namespace cv;

int main()
{
    Mat1b img = imread("path_to_floodfilled_image", IMREAD_GRAYSCALE);

    // 127 is the color of the floodfilled region
    Mat1b mask = (img == 127); 

    // Black initialized image, same size as img
    Mat3b out(img.rows, img.cols, Vec3b(0,0,0)); 

    Scalar some_color(0,255,0);
    out.setTo(some_color, mask);

    // Show results
    imshow("Flood filled image", img);
    imshow("Mask", mask);
    imshow("Colored mask", out);
    waitKey();

    return 0;
}
相关问题