簡體   English   中英

Python中的平滑二進制分割掩碼圖像

[英]Smooth binary segmentation mask image in Python

我想平滑我的二值圖像分割掩碼的輪廓,以改進我的卷積神經網絡分割算法的輸出。

我試過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 解決方案持開放態度。

原始鋸齒狀二進制圖像蒙版

您可以嘗試使用 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