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

时间:2017-08-09 03:15:17

标签: python opencv image-processing python-imaging-library

我想平滑我的二进制图像分割掩码的轮廓,以改进我的卷积神经网络分割算法的输出。

我试过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)

在阈值处理后,生成的图像看起来与原始蒙版基本相同。难道我做错了什么?有没有其他方法来平滑二进制掩码?我对任何Python解决方案都持开放态度。

Original Jagged Binary Image Mask

1 个答案:

答案 0 :(得分:0)

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

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