Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 760
Abdul Fatir Avatar asked May 24 '16 09:05

Abdul Fatir


People also ask

How do you smooth the edge of a binary image in Python?

You can do that in Python/OpenCV with the help of Skimage by blurring the binary image. Then apply a one-sided clip. You will have to adjust the amount of blur for the degree of aliasing in the image.

How do you smooth an image in Python?

To smoothen an image with a custom-made kernel we are going to use a function called filter2D() which basically helps us to convolve a custom-made kernel with an image to achieve different image filters like sharpening and blurring and more.


1 Answers

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

like image 54
Sunreef Avatar answered Sep 27 '22 22:09

Sunreef