Abhishek V. Pai
Abhishek V. Pai

Reputation: 351

Filling shapes in an image with a particular color opencv

I have this image

enter image description here

I want to fill the polygons with white color. I tried fillpoly but couldn't get it to work. Any ideas? I'm using opencv 3.0 in c++.

Upvotes: 2

Views: 3882

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207465

This is a fun question, I thought I'd try it with ImageMagick at the command line but you can adapt the technique to OpenCV readily enough. I suspect it may be similar to what @Arjun's code does, but I can't read that easily...

I'll do it in three steps, each one building on the last, but you actually only need the final, one-line command at the end.

First, flood-fill the image with yellow starting at the top-left corner:

convert shapes.png -fill yellow -draw 'color 0,0 floodfill' result.png

enter image description here

Now set the fill-colour to white and overpaint all black areas with the white fill-colour:

convert shapes.png -fill yellow -draw 'color 0,0 floodfill' -fill white -opaque black result.png

enter image description here

Now set the fill-colour to black, and overpaint the yellow areas with the black fill-colour:

convert shapes.png -fill yellow -draw 'color 0,0 floodfill' -fill white -opaque black -fill black -opaque yellow result.png

enter image description here

Upvotes: 0

Try the below piece of code to fill the closed objects with whit color.

 cv::Mat edgesIn; 
 cv::Mat edgesNeg =temp.clone();
 //  imshow( "edgesNeg", edgesNeg );
 cv::floodFill(edgesNeg, cv::Point(0,0), CV_RGB(255,255,255));
 imshow( "edgesNeg", edgesNeg );
 bitwise_not(edgesNeg, edgesNeg);
 filledEdgesOut = (edgesNeg | temp);
 imshow("Filled region",filledEdgesOut);

Upvotes: 1

Related Questions