Create trimap from approximated mask

asked 2017-09-03 09:50:35 -0600

trax72 gravatar image

updated 2017-09-06 07:54:05 -0600

I would like to create a trimap to be used for matting, given an approximated (black and white) mask in which every pixel within 10 pixels of the edge would be gray.

Of course, the naive solution would be to go through the image, find all pixels that have an opposite color neighbor and paint a gray circle around it (mark every pixel within 10 pixels of each such pixel as gray).

I'm wondering if there is an easier/faster way to code it.

What I mean is - given this:

image description

Create this:

image description

edit retag flag offensive close merge delete

Comments

How about first adding an extra black border around the image (closes the contour), findcontours on the binary image, retrieve all points of the single contour, so no cuts on straight parts, loop over those points and if they are not on the original border draw your circle?

StevenPuttemans gravatar imageStevenPuttemans ( 2017-09-06 08:57:05 -0600 )edit

You can try using the distance tranform for this:

Perform the distance transform on the mask, also use it on the inverse of the mask, and color grey all pixels which have a distance less than half your expected grey band width in both images.

In C++ documentation, distance transform call is:

void cv::distanceTransform  (   InputArray  src,
OutputArray     dst,
OutputArray     labels,
int     distanceType,
int     maskSize,
int     labelType = DIST_LABEL_CCOMP 
)
MikeTronix gravatar imageMikeTronix ( 2018-08-27 11:43:23 -0600 )edit