Junaid Sultan
Junaid Sultan

Reputation: 351

Image Processing on flood fill image

I am working on an image processing project in which I have flood filled the original image.

Now

  1. I need to remove the noise in this picture which is the white lines around the image of the hand. I want to remove these white lines by merging them into the background color which is black.

  2. I need to to change the gray color (the value is 127) of the flood filled area to white color. Note that the background colour should remain black.

enter image description here


This is a follow-up of this question. The image is obtained with the code in this answer.

Upvotes: 1

Views: 1300

Answers (1)

Miki
Miki

Reputation: 41765

The code to produce the image in your question can be found in your previous question.

So we know that the flood-filled region has value 127.


Starting from this image, you can easily obtain the mask of the flood-filled region as:

Mat1b mask = (img == 127);

enter image description here

The single channel mask will have values either black 0 or white 255.

If you want to have a color image, you need to create a black initialized image of the same size as img, and set pixels according to the mask to your favourite color (green here):

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

Code for reference:

#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;
}

Upvotes: 3

Related Questions