pythonopencvimage-processingblursmoothing

Smoothing Edges of a Binary Image


How to smooth the edges of this binary image of blood vessels obtained after thresholding.

enter image description here

I tried a method somewhat similar to this method but did not quite get the result I expected.

enter image description here

Here's the code:

import cv2
import numpy as np

INPUT = cv2.imread('so-br-in.png',0)
MASK = np.array(INPUT/255.0, dtype='float32')

MASK = cv2.GaussianBlur(MASK, (5,5), 11)
BG = np.ones([INPUT.shape[0], INPUT.shape[1], 1], dtype='uint8')*255

OUT_F = np.ones([INPUT.shape[0], INPUT.shape[1], 1],dtype='uint8')

for r in range(INPUT.shape[0]):
    for c in range(INPUT.shape[1]):
        OUT_F[r][c]  = int(BG[r][c]*(MASK[r][c]) + INPUT[r][c]*(1-MASK[r][c]))

cv2.imwrite('brain-out.png', OUT_F)  

What can be done to improve the smoothing of these harsh edges?

EDIT

I'd like to smoothen the edges something like http://pscs5.tumblr.com/post/60284570543. How to do this in OpenCV?


Solution

  • Here is the result I obtained with your image: enter image description here

    My method is mostly based on several cv::medianBlurapplied on a scaled-up image.

    Here is the code:

    cv::Mat vesselImage = cv::imread(filename); //the original image
    cv::threshold(vesselImage, vesselImage, 125, 255, THRESH_BINARY);
    cv::Mat blurredImage; //output of the algorithm
    cv::pyrUp(vesselImage, blurredImage);
    
    for (int i = 0; i < 15; i++)
        cv::medianBlur(blurredImage, blurredImage, 7);
    
    cv::pyrDown(blurredImage, blurredImage);
    cv::threshold(blurredImage, blurredImage, 200, 255, THRESH_BINARY);
    

    The jagged edges are due to the thresholding. If you are comfortable with an output image that is non-binary (i.e. with 256 shades of grAy), you can just remove it and you get this image: enter image description here