代码之家  ›  专栏  ›  技术社区  ›  Ajinkya

将特定范围内的所有BGR值转换为白色(255255)

  •  0
  • Ajinkya  · 技术社区  · 7 年前

    我已经输入了png图像,我想把从(2,2,2)开始到(255255)结束的特定范围内的所有像素转换成白色(255255)

    im = cv2.imread('3.png')  # I am reading the image
    
    lower_range = np.array([2,2,2]). # I specific the lower range
    upper_range = np.array([255,255,255]) # I specify the upper range
    
    im[np.where((im == [0,0,255]).all(axis = 2))] = [255,255,255]  # converts all red pixels to white
    cv2.imwrite('out.png', im)
    

    我的问题是如何修改 im[np.where((im == [0,0,255]).all(axis = 2))] = [255,255,255] ,使其覆盖第2行和第3行中提到的颜色范围,并将其全部转换为白色。

    0 回复  |  直到 7 年前
        1
  •  1
  •   Quang Hoang    7 年前

    cv2.inRange 这就产生了一个面具,可以用来改变你想要的颜色。

    mask1 = cv2.inRange(im, lower_range, upper_range)
    im[np.where(mask)] = [255,255,255]
    

    另一方面,你的颜色范围相当大(几乎涵盖了所有颜色)。