Type conversion
Using opencv-python to add object-detection rectangle for the image:
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread("./birds/Martens's Warbler/martens's_warbler.638x520+0+0.999.200x200.0.o_0014.jpg")
cv2.rectangle(img, (21, 34), (185, 158), (255, 0, 0), 1)
plt.imshow(img)
plt.show()
The result looks like this
But in a more complicated program, I processed a image from float32 type. Therefore the code looks like:
...
img = img.astype('int')
cv2.rectangle(img, (21, 34), (185, 158), (255, 0, 0), 1)
...
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
...
img = img.astype('uint8')
...
Check source of image
img = somefunc()
cv2.resize(img, (100, 200))
This code snippet reported error:
TypeError: Expected cv::UMat for argument 'src'
Seems the argument ‘img’ is not a correcttype. So I blindly changed the code to convert ‘img’ to ‘UMat’.
img = somefunc()
cv2.resize(cv2.UMat(img), (100, 200))
It also reported another more inexplicable error:
TypeError: Required argument 'ranges' (pos 2) not found
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.