Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在numpy中优化此图像迭代?_Python_Opencv_Numpy_Image Processing_Opencv3.0 - Fatal编程技术网

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

Python 如何在numpy中优化此图像迭代?,python,opencv,numpy,image-processing,opencv3.0,Python,Opencv,Numpy,Image Processing,Opencv3.0,我用这个代码来检测图像中的绿色 问题是这个迭代非常慢 如何使它更快?如果它正在使用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,wid

我用这个代码来检测图像中的绿色

问题是这个迭代非常慢

如何使它更快?如果它正在使用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)
试着简单地说:

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

我只是作为一个业余爱好者涉猎过numpy,但我相信您可以利用fromfunction从现有的np数组创建一个新的np数组

以下是我认为在这种情况下可能会起作用的内容-这将利用numpy的速度:

def手柄颜色(img、x、y):
蓝色=模拟项目(x,y,0)
绿色=模拟项目(x,y,1)
红色=模拟项目(x、y、2)
exg=2*绿-红-蓝
如果exg>50:
返回(exg、绿色、红色)
返回蓝色、绿色、红色
def转换器绿色(rawimg):
processedimg=np.fromfonment(lambda i,j:handle_colors(rawimg,i,j),rawimg.shape)
返回进程

我只是作为一个业余爱好者涉猎过numpy,但我相信您可以利用fromfunction从现有的np数组创建一个新的np数组

以下是我认为在这种情况下可能会起作用的内容-这将利用numpy的速度:

def手柄颜色(img、x、y):
蓝色=模拟项目(x,y,0)
绿色=模拟项目(x,y,1)
红色=模拟项目(x、y、2)
exg=2*绿-红-蓝
如果exg>50:
返回(exg、绿色、红色)
返回蓝色、绿色、红色
def转换器绿色(rawimg):
processedimg=np.fromfonment(lambda i,j:handle_colors(rawimg,i,j),rawimg.shape)
返回进程

Great您完全避免了使用
for
循环!!我犯了这个错误。exg=2*绿色-红色-蓝色ueError:操作数无法与形状(480640)(480,2,3)一起广播!请参见编辑(红色
定义中缺少最后一个逗号)太好了,您完全避免了使用
for
循环!!我犯了这个错误。exg=2*绿色-红色-蓝色ueError:操作数无法与形状(480640)(480,2,3)一起广播!请参见编辑(在
红色
定义中缺少最后一个逗号)