有没有更简单、更快捷的方法?
maxr, maxc = im_out.shape[:2]
for col in range(maxc):
for row in range(maxr):
if im_gray[row,col,0] != 255 and im_gray[row,col,1] != 255 and im_gray[row,col,2] != 255:
im_out[row, col] = im_gray[row, col]
类似这样的代码应该可以达到这个效果:
# I think it's axis 2, might have to play around there
mask = (im_gray != 255).all(axis=2)
im_out[mask] = im_gray[mask]
可以使用遮罩:
mask = (im_gray[..., 0] != 255) & (im_gray[..., 1] != 255) & (im_gray[..., 2] != 255)
im_out[mask] = im_gray[mask]
上面的矢量化找到一个掩码,其中所有通道的条件都满足。