【问题标题】: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】:

您可以使用numpyscipy 应用膨胀:

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)

【讨论】:

    【解决方案2】:

    此操作称为dilation。 Python 有这方面的功能。

    【讨论】:

      【解决方案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)
      

      【讨论】:

        猜你喜欢
        • 2014-05-02
        • 2020-07-25
        • 1970-01-01
        • 2015-05-05
        • 2021-01-23
        • 1970-01-01
        • 2018-09-28
        • 1970-01-01
        • 2023-03-26
        相关资源
        最近更新 更多