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

发布于 2021-01-29 14:59:07

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

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

如何使其更快?如果使用的是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
关注者
0
被浏览
64
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    简单尝试一下:

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看