简体   繁体   English

Python中的平滑二进制分割掩码图像

[英]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:我试过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.我对任何 Python 解决方案持开放态度。

Original Jagged Binary Image Mask原始锯齿状二进制图像蒙版

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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