Ask Your Question

Hand mask has holes in it - Best way to deal with?

asked 2017-03-01 05:43:39 -0500

ginister gravatar image

updated 2017-03-02 10:10:25 -0500

Hi everyone, I have a thresholded binary mask from H-S back projection (applied back to the original image) as displayed below. I was wondering what the best image technique would be to solve the big holes. The holes are where the colour of the skin vary more. e.g. something like a convex hull may help, but I'm not exactly sure how to apply these. Any suggestions welcome.


Here is a photo of the in and output of the erosion and dilation suggestion: image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-03-01 19:52:21 -0500

Tetragramm gravatar image

updated 2017-03-02 18:37:42 -0500

Take a look at Erosion and Dilation.


As kbarni pointed out, I liked to just the tutorial on erosion and dilation. The next tutorial is on all the morphology operations, including the one you want, close.

Secondly, that code makes me sad. There is almost no time you want to new a cv::Mat. Especially not like this when it will cause a memory leak. Instead either just pass it (make sure to include the cv::Scalar parameter to initialize it to 255) or use the getStructuringElement method to create it.

Mat kernel = getStructuringElement( MORPH_ELLIPSE, Size( 15, 15 ) );
morphologyEx( image, image, MORPH_CLOSE, kernel );

creates the result

image description

The result you got is likely because you didn't initialize the value of your kernel to anything.

edit flag offensive delete link more



...and the closing operation.

kbarni gravatar imagekbarni ( 2017-03-02 02:50:54 -0500 )edit

Thank you for the hints everyone. I would have thought that erosion & dilation would expand the hand evenly on all sides, so I would have some extra data on the outline of the hand. Previously I was doing

erode(lastPreprocessedROI, lastPreprocessedROI, Mat());
    dilate(lastPreprocessedROI, lastPreprocessedROI, Mat());
however this resulted in a pretty similar image. Do I need to modify the final argument as well to make it effective? And in response to @kbarni, is the closing just the erosion and dilation being applied in that order? The docs seem to suggest so

ginister gravatar imageginister ( 2017-03-02 09:36:14 -0500 )edit

Just in addition to the above, I have tested out the above proposed solutions with the following code

  erode(in, out, *new Mat(cv::Size(6,6), CV_8U));
  dilate(out, out, *new Mat(cv::Size(6,6), CV_8U));
however this produces some problematic results. The fingers blur quite drastically and the holes do not get much better. I worry in particular about losing the intentional gaps between the fingers however in this case this was not problematic. Can you recommend any ways to improve this approach? I have updated my original answer to show the in and out when using this code. Many thanks.

ginister gravatar imageginister ( 2017-03-02 10:08:51 -0500 )edit

Question Tools

1 follower


Asked: 2017-03-01 05:43:39 -0500

Seen: 219 times

Last updated: Mar 02 '17