gpt4 book ai didi

python - 如何在numpy中优化此图像迭代?

转载 作者:行者123 更新时间:2023-12-02 16:47:07 27 4
gpt4 key购买 nike

我正在使用此代码来检测图像中的绿色。

问题在于此迭代确实很慢。

如何使其更快?如果使用的是numpy,该如何以numpy的方式进行?

def convertGreen(rawimg):
width, height, channels = rawimg.shape
size = (w, h, channels) = (width, height, 1)
processedimg = np.zeros(size, np.uint8)
for wimg in range(0,width):
for himg in range(0,height):
blue = rawimg.item(wimg,himg,0)
green = rawimg.item(wimg,himg,1)
red = rawimg.item(wimg,himg,2)
exg = 2*green-red-blue
if(exg > 50):
processedimg.itemset((wimg,himg,0),exg)

return processedimg

最佳答案

简单尝试一下:

blue = rawimg[:,:,0]
green = rawimg[:,:,1]
red = rawimg[:,:,2]
exg = 2*green-red-blue
processedimg = np.where(exg > 50, exg, 0)

关于python - 如何在numpy中优化此图像迭代?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42991524/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com