Python中的平滑二值分割掩模图像
我想平滑我的二值图像分割掩模的轮廓,以改进我的卷积神经网络分割算法的输出 我试过PIL:Python中的平滑二值分割掩模图像,python,opencv,image-processing,python-imaging-library,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
# 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)))
你会发现这很有用!如果你只是简单地放大/腐蚀图像,这不是很有帮助吗?@Headcrab,必要时重复。你可以得到轮廓,然后使用approxPolyDP减少顶点的数量。然后将轮廓重新绘制为黑色背景上的白色填充。这是否回答了您的问题?请不要发帖。如果两个问题能以某种方式得到回答,那么这些问题是重复的。将一个标记为另一个的副本。