一尘不染

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

python

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

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

如何使其更快?如果使用的是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

阅读 241

收藏
2021-01-20

共1个答案

一尘不染

简单尝试一下:

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