简体   繁体   中英

Smooth binary segmentation mask image in Python

I would like to smooth out the outline of my binary image segmentation mask to improve upon the output from my convolutional neural network segmentation algorithm.

I tried PIL:

# Smooth the outline
from PIL import Image, ImageFilter
pil_img = Image.fromarray(orignal_mask)
smoothed_image = pil_img.filter(ImageFilter.SMOOTH)
smoothed_image = smoothed_image.filter(ImageFilter.SMOOTH_MORE)
smoothed_image_arr = np.array(smoothed_image)
smoothed_image_arr[smoothed_image_arr>=127] = 255
smoothed_image_arr[smoothed_image_arr<127] = 0
cv2.imwrite(data_dir + 'test_masks/smooth_{}'.format(jpg_filename), smoothed_image_arr)

After thresholding, the resulting image looks essentially identical to the original mask. Am I doing something wrong? Is there any other way to smooth out the binary mask? I am open to any Python solution.

Original Jagged Binary Image Mask

您可以尝试使用 OpenCV 应用形态学操作,开口可以稍微平滑边缘:

cv2.morphologyEx(mask, cv2.MORPH_OPEN,cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM