Reputation: 351
I am working on an image processing project in which I have flood filled the original image.
Now
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.
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.
This is a follow-up of this question. The image is obtained with the code in this answer.
Upvotes: 1
Views: 1300
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);
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);
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