如何在numpy中优化这个图像迭代? [英] How to optimize this image iteration in numpy?

查看:91
本文介绍了如何在numpy中优化这个图像迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using this code to detect green color in the image.

问题是此迭代非常慢。

The problem is this iteration is really slow.

如何让它更快?如果它正在使用numpy,如何以numpy方式进行?

How to make it faster? If it is using numpy, How to do it in numpy way?

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)

这篇关于如何在numpy中优化这个图像迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆