OpenCV Python Binds Incredibly Slow Iterations Using Image Data

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

The overall results and method were the same minus syntax. But when I execute the code below on each frame of the video, it takes almost 2-3 seconds to complete, where, like the C ++ option, also below, it instantly compares, and I can iterate between frames as fast as my finger can press the 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;
        }
    }

thank

+5
source share
1 answer

numpy , . C- , numpy.

, numpy...

opencv, , python numpy, , :

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)

( , ...) , opencv, - , ,...

, , !

+6

All Articles