是否有一种聪明的方法可以将更改特定颜色的脏代码转换为另一种颜色?

时间:2020-06-06 06:50:46

标签: python numpy cv2

#convert pixels's color from 0,255,255 to 0,0,0
h = mask.shape[0] 
w = mask.shape[1]
for py in range(0,h):
    for px in range(0,w):
        if mask[py][px][0] ==0 and mask[py][px][1] == 255 and mask[py][px][2] == 255:
            for i in range(3):
                mask[py][px][i] = 0
# masking
masked = mask.copy()
for py in range(0,h):
    for px in range(0,w):
        if masked[py][px][0] != 0:
            masked[py][px] = im[py][px]
masked = cv2.cvtColor(masked, cv2.COLOR_RGB2BGR)

是否可以通过cv2或numpy函数将脏代码转换为简单代码?

1 个答案:

答案 0 :(得分:3)

您可以轻松地做到:

color_old = (0,255,255)
color_new = (0,0,0)

img[(img==color_old).all(-1)] = color_new