Python OpenCV TypeError: integer argument expected, got float

Abnormal interpretation

When using OpenCV to write code, there will be typeerror: integer argument expected, get float error

The error is a type error. For example, the following code will report an error

img = cv.imread('10.jpg', 1)
rows, cols, channels = img.shape
M = np.float32([[1, 0, 100], [0, 1, 50]])
res = cv.warpAffine(img, M, (cols*0.5, rows*0.5))
cv.imshow("img", res)
cv.waitKey()

The error location is shown in the figure below, where the value of int type is required, but after cols * 0.5 operation, the float type appears

Exception resolution

Type cast

res = cv.warpAffine(img, M, (int(cols*0.5), int(rows*0.5)))

Appendix

This series of articles is only for recording the bugs encountered in the process of Python daily development and providing them to students as reference data and solutions. It belongs to the recording blog, and readers who are destined to see it hope to solve your problems

Error prompt template can be compared with your error

Traceback (most recent call last):
  File "e:/crawl100/opencv_ppt/10day.py", line 7, in <module>
    res = cv.warpAffine(img, M, (cols*0.5, rows*0.5))
TypeError: integer argument expected, got float

This article shares synchronously on the blog “dream eraser” (CSDN).

Similar Posts: