Some tips for opencv-python

      No Comments on Some tips for opencv-python

Type conversion
Using opencv-python to add object-detection rectangle for the image:

The result looks like this




But in a more complicated program, I processed a image from float32 type. Therefore the code looks like:

But this time, the rectangle disappeared.




The reason is opencv-python use numpy array for image in type of ‘uint8’, not ‘int’! The correct code should be

Check source of image

This code snippet reported error:

Seems the argument ‘img’ is not a correcttype. So I blindly changed the code to convert ‘img’ to ‘UMat’.

It also reported another more inexplicable error:

After long time searching, I finally get the cause: the function ‘somefunc()’ returned a tuple ‘(img, target)’ instead of only ‘img’…
I should look more closely into argument ‘img’ before changing the code.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.