Reputation: 351
I have this image
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
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
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
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
Upvotes: 0
Reputation: 394
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