How to smooth the edges of this binary image of blood vessels obtained after thresholding.
I tried a method somewhat similar to this method but did not quite get the result I expected.
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?
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.
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.
Here is the result I obtained with your image:
My method is mostly based on several cv::medianBlur
applied 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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With