Python OpenCV TypeError: integer argument expected, got float
阿新 • • 發佈:2021-01-04
技術標籤:Python BUG記錄pythonopencvbug影象處理
異常解讀
在使用 OpenCV 進行程式碼編寫時,會出現 TypeError: integer argument expected, got float 錯誤。
該錯誤為型別錯誤,例如下述程式碼就會報錯。
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()
該錯誤位置如下圖所示,此處需要 int
型別的值,但是進行 cols * 0.5
操作之後,出現 float
型別。
異常解決方案
型別強制轉換即可。
res = cv.warpAffine(img, M, (int(cols*0.5), int(rows*0.5)))
附錄
本系列文章只供記錄 Python 日常開發過程中 偶遇 BUG,提供給學生作為參考資料與解決方案用,屬於記錄型部落格,有緣分看到的讀者希望可解決你的問題。
錯誤提示樣板,可以與你的錯誤進行比對。
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