我有以下代码:
for x in range(len(img)):
for y in range(len(img[0])):
if img[x][y]==255 :
imgblur[x][y]=imggray[x][y]
我对此进行了改进:
i, j = np.where(img == 255)
for x in range(i.shape[0]):
imgblur[i[x]][j[x]]=imggray[i[x]][j[x]]
有没有办法提高更多(更快循环)?
最佳答案
尝试np.where()
,其中说:if img==255 then imgblur = imgray else imgblur = imgblur
nx, ny = 5, 10
img = np.random.randint(255, size=(nx, ny))
imgblur = np.random.uniform(0,1.0,size=(nx, ny))
imggray = np.random.uniform(0,1.0,size=(nx, ny))
imgblur = np.where(img==255,imggray,imgblur)
imgblur