OpenCV Python binds incredibly slow iterations through image data

后端 未结 1 1305
萌比男神i
萌比男神i 2021-01-05 20:03

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

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-05 20:51

    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!

    0 讨论(0)
提交回复
热议问题