【问题标题】:Expand white pixels Image processing in python在python中展开白色像素图像处理
【发布时间】:2017-06-18 19:22:44
【问题描述】:
如果我打开如下图,是否可以扩大白色像素?
伪代码:
image = Image.open("test.png")
image = image.convert("RGBA")
my_data = image.getdata()
new_data = []
for i in my_data:
if i[0] == 255 and i[1] == 255 and i[2] == 255:
#append white to new_data + to the pixels around
else:
new_data.append((255, 255, 255, 0))
谢谢
【问题讨论】:
标签:
python
image
numpy
image-processing
pillow
【解决方案1】:
您可以使用numpy 和scipy 应用膨胀:
import numpy as np
from scipy.ndimage.morphology import binary_dilation
# Create and initialize image
image = np.zeros((21, 41), dtype=bool)
image[8:12, 18:22] = True
# Define structuring element and applying dilation
strel = np.ones((3, 3))
dilated = binary_dilation(image, structure=strel)
【解决方案3】:
您可以使用 cv2 中的 dilate 函数,以及一点 numpy:
import cv2
import numpy as np
img = cv2.imread('input.png',0)
kernel = np.ones((5,5), np.uint8)
dilation = cv2.dilate(img,kernel,iterations = 5)
cv2.imwrite('result.png', dilation)