[英]Shorter way of this NumPy list comprehension
Is there a simpler and faster way of doing this? 有没有更简单,更快捷的方法?
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]
Something along the lines of this should do the trick: 遵循以下方法应该可以解决问题:
# 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]
You can use a mask: 您可以使用遮罩:
mask = (im_gray[..., 0] != 255) & (im_gray[..., 1] != 255) & (im_gray[..., 2] != 255)
im_out[mask] = im_gray[mask]
The above vectorization finds a mask where the condition is satisfied for all the channels. 上面的矢量化找到一个掩码,其中所有通道都满足条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.