Search code examples
opencvopencv3.0image-segmentationsemantic-segmentationdeeplab

Refine segmentation mask based on contours of image


I have an image for which I have the green border as the segmentation mask's outline. I'm looking to refine this outline based on contours found on the original image, to get a mask like the in 2nd image - where the edges of the hair are more refined.

segmentation mask outline enter image description here

I've tried combinations of dilation & erosion of the segmentation mask, but it didn't feel like a generic solution - since it involves manually tuning the kernel size.

Are there better approaches?


Solution

  • There is no generic solution. Parameter tuning is always required to get the desired output. For getting more refined fine edges of the hairs, you can apply thresholding as below:

    import cv2 as cv
    from matplotlib import pyplot as plt
    
    im = cv2.imread("model.jpg",0)
    plt.imshow(im)
    thresh = cv2.adaptiveThreshold(im,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
                cv2.THRESH_BINARY_INV,31,3)
    plt.imshow(thresh)
    

    input: enter image description here

    output: enter image description here

    Note: The color changes are due to 'matplotlib'.