Fix mask visualization to exclude holes from polygon drawing#5509
Open
Mr-Neutr0n wants to merge 1 commit into
Open
Fix mask visualization to exclude holes from polygon drawing#5509Mr-Neutr0n wants to merge 1 commit into
Mr-Neutr0n wants to merge 1 commit into
Conversation
Fixes facebookresearch#1771 When drawing segmentation masks with holes, the GenericMask.mask_to_polygons() method was returning all contours including internal hole contours. When these polygons were drawn, the holes were incorrectly filled in. The fix filters out hole contours by checking the hierarchy returned from cv2.findContours. With cv2.RETR_CCOMP: - External contours have hierarchy[i][3] == -1 (no parent) - Hole contours have hierarchy[i][3] >= 0 (have a parent) Now only external contours (boundaries) are returned as polygons, while holes are correctly left empty.
Author
|
hi, wanted to follow up on this. the polygon drawing bug causes holes to incorrectly show up as filled regions in mask visualizations. the fix is minimal — just filters out inner contours. let me know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1771
When drawing segmentation masks with holes, the
GenericMask.mask_to_polygons()method was returning all contours including internal hole contours. When these polygons were drawn, the holes were incorrectly filled in.Problem
As reported in #1771:
Visualizer.draw_instance_predictions()createsGenericMaskand callsoverlay_instancesmask_to_polygonsusescv2.findContourswithcv2.RETR_CCOMPwhich returns both external and internal (hole) contoursBefore fix:

Solution
Filter out hole contours by checking the hierarchy returned from
cv2.findContours. Withcv2.RETR_CCOMP:hierarchy[i][3] == -1(no parent)hierarchy[i][3] >= 0(have a parent)Now only external contours (boundaries) are returned as polygons, while holes are correctly left empty.
Changes
Modified
GenericMask.mask_to_polygons()to filter contours based on hierarchy:Test plan