OpenCV Python通过图像数据绑定令人难以置信的慢速迭代 [英] OpenCV Python binds incredibly slow iterations through image data

查看:157
本文介绍了OpenCV Python通过图像数据绑定令人难以置信的慢速迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用了一些基于 OpenCV c ++ 中的颜色跟踪对​​象的代码,并在python绑定中重写。



结果和方法都相同减去语法明显。但是,当我对视频的每个帧执行以下代码,需要几乎2-3秒来完成其中,作为c ++变体,也在下面,是即时比较,我可以在帧之间的快速,我的手指可以按



任何想法或意见?

  cv.PyrDown(img,dsimg)
for i in range(0,dsimg.height):$ b对于范围(0,dsimg.width)中的j,$ b:

如果dsimg [i,j] [1] (_RED_DIFF + dsimg [i,j] [2])和dsimg [i,j] [1] (_BLU_DIFF + dsimg [i,j] [0]):
res [i,j] = 255
else:
res [i,j] = 0






  for(int i = 0;对于(int j = 0; j <(width); j ++)
{
if(((data [i (data [i * step + j * channels + 1])(步骤+ j * channel + 1])>(RED_DIFF + data [i * step + j * channels + 2] ] +>(BLU_DIFF + data [i * step + j * channels])))
data_r [i * step_r + j * channels_r] = 255;
else
data_r [i * step_r + j * channels_r] = 0;
}
}

感谢

解决方案

尝试使用numpy来进行计算,而不是嵌套循环。



例如,你的嵌套for循环可以用几个numpy表达式来代替...



我不太熟悉opencv,但我认为python绑定现在有一个numpy数组接口,所以上面的例子应该是简单的:

  cv.PyrDown(img,dsimg)

data = np.asarray(dsimg)
blue, green,red = data.T

res =(green>(_RED_DIFF + red))& (绿色>(_BLU_DIFF +蓝色))
res = res.astype(np.uint8)* 255

res = cv.fromarray(res)

(完全未经测试,当然...)再次,我真的不是很熟悉opencv,但嵌套python for循环不是无论... ...



希望帮助一下,无论如何!


I recently took some code that tracked an object based on color in OpenCV c++ and rewrote it in the python bindings.

The overall results and method were the same minus syntax obviously. But, when I perform the below code on each frame of a video it takes almost 2-3 seconds to complete where as the c++ variant, also below, is instant in comparison and I can iterate between frames as fast as my finger can press a key.

Any ideas or comments?

    cv.PyrDown(img, dsimg)
    for i in range( 0, dsimg.height ):
        for j in range( 0, dsimg.width):

            if dsimg[i,j][1] > ( _RED_DIFF + dsimg[i,j][2] ) and dsimg[i,j][1] > ( _BLU_DIFF + dsimg[i,j][0] ):
                res[i,j] = 255
            else:
                res[i,j] = 0


    for( int i =0; i < (height); i++ ) 
    {
        for( int j = 0; j < (width); j++ )
        {
            if( ( (data[i * step + j * channels + 1]) > (RED_DIFF + data[i * step + j * channels + 2]) ) &&
                ( (data[i * step + j * channels + 1]) > (BLU_DIFF + data[i * step + j * channels]) ) )
                data_r[i *step_r + j * channels_r] = 255;
            else
                data_r[i * step_r + j * channels_r] = 0;
        }
    }

Thanks

解决方案

Try using numpy to do your calculation, rather than nested loops. You should get C-like performance for simple calculations like this from numpy.

For example, your nested for loops can be replaced with a couple of numpy expressions...

I'm not terribly familiar with opencv, but I think the python bindings now have a numpy array interface, so your example above should be as simple as:

cv.PyrDown(img, dsimg)

data = np.asarray(dsimg)
blue, green, red = data.T

res = (green > (_RED_DIFF + red)) & (green > (_BLU_DIFF + blue))
res = res.astype(np.uint8) * 255

res = cv.fromarray(res)

(Completely untested, of course...) Again, I'm really not terribly familar with opencv, but nested python for loops are not the way to go about modifying an image element-wise, regardless...

Hope that helps a bit, anyway!

这篇关于OpenCV Python通过图像数据绑定令人难以置信的慢速迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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