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

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

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

10-06 06:29