【问题标题】:Replacing specific colors with black using slicing?使用切片用黑色替换特定颜色?
【发布时间】:2020-03-17 04:59:24
【问题描述】:

第一次在stackoverflow上提问! 我正在尝试使用切片替换特定的像素颜色。我有兴趣用黑色代替粉红色。

 colors = [(0, 0, 0), (255, 0, 255)]
 img = cv2.imread('Untitled.png')  # Random image containing some pink pixels
 pink = img[:, :, :] == np.array(colors[1])  # Boolean array with TRUE @ all pink indices

当我尝试使用此功能进行替换时

img[pink, :] = np.array(colors[0])  # Replace with black

我收到以下错误

img[pink, :] = np.array(colors[0])
IndexError: too many indices for array

img 和粉色是相同的尺寸和大小。我做错了什么?

【问题讨论】:

    标签: python replace colors slice pixel


    【解决方案1】:

    这应该适合你:

    import cv2
    
    image = cv2.imread('test.png')
    image[np.where((image==[255,0,255]).all(axis=2))] = [0,0,0]
    cv2.imwrite('test_out.png', image)
    

    【讨论】:

    • 谢谢,确实如此! (image==[255, 0, 255]).all(axis=2) 是否意味着它查看 x 和 y 轴?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    相关资源
    最近更新 更多