Skip to content
Advertisement

shrink and enlarge contour image with Python OpenCV

I have an image with an object like below:

enter image description here

I can detect the contour and get a mask with only ball region, but my ROI is the edge region, that means I need a bigger and a smaller mask which combine to get this:

enter image description here

so my question is: how can I shrink/enlarge the mask of contour around contour’s center?

Advertisement

Answer

Here is one way to do that in Python/OpenCV.

JavaScript

Input:

enter image description here

JavaScript

Mask:

enter image description here

Erode mask:

enter image description here

Dilate mask:

enter image description here

Result:

enter image description here

Note: If you dilate too much, you reach the edges of the image and then the shape changes. To avoid that, pad the input with background color enough to contain the dilated size.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement