Integer argument expected int got float in opencv

你离开我真会死。 提交于 2021-01-03 07:01:13

问题


I have displayed the code below, but when I try to execute it, get

Traceback (most recent call last):
  File "/home/decentmakeover2/Code/cv.py", line 22, in <module>
    img = cv2.circle(img,center, radius, (0,255, 0), 2)
TypeError: integer argument expected, got float

Im not exactly sure what the problem is, in the minEnclosingCircle the values have been converted to int, but i still get the same error, any ideas on what might be the problem?

import numpy as np
import cv2
import os
from scipy import ndimage

img = cv2.pyrDown(cv2.imread('img.jpeg'))
ret, thresh  = cv2.threshold(cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY), 127, 255, cv2.THRESH_BINARY)
image, contours, heir = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
    x, y , w, h = cv2.boundingRect(c)
    cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)

    rect  = cv2.minAreaRect(c)
    box = cv2.boxPoints(rect)
    box  = np.int0(box)
    cv2.drawContours(img, [box], 0 , (0, 0, 255), 3)

    (x,y), radius = cv2.minEnclosingCircle(c)
    center = (int(x), int(y))
    radius = int(radius)
    img = cv2.circle(img, center, radius, (0,255, 0), 2)

cv2.drawContours(img, contours, -1, (255, 0, 0), 1)   
cv2.imshow('contours',img)
cv2.waitKey(0)
cv2.destroyAllWindows()`

回答1:


Modify your code as follows you do not need to use return value of cv2.circle.

cv2.circle(img,center, radius, (0,255, 0), 2)



回答2:


This answer is probably too late, but I found that cv2.circle only can accept centre coordinates precision up to float32. If the coordinates are in float64, it will throw this error. Simple solution is always converting centre coordinates to numpy.float32.




回答3:


I have done small changes to your code for conversion of float numbers to integers. It is running without errors now. Check this:

import numpy as np
import cv2
import os
from scipy import ndimage

img = cv2.pyrDown(cv2.imread('img.jpeg'))
ret, thresh  = cv2.threshold(cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY), 127, 255, cv2.THRESH_BINARY)
image, contours, heir = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
    x, y ,w ,h = cv2.boundingRect(c)
    cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)

    rect  = cv2.minAreaRect(c)
    box = cv2.boxPoints(rect)
    box  = np.int0(box)
    cv2.drawContours(img, [box], 0 , (0, 0, 255), 3)

    (x,y), radius = cv2.minEnclosingCircle(c)
    x = np.round(x).astype("int")
    y = np.round(y).astype("int")
    center = (x,y)
    radius = np.round(radius).astype("int")
    cv2.circle(img, center, radius, (0,255, 0), 2)

cv2.drawContours(img, contours, -1, (255, 0, 0), 1)   
cv2.imshow('contours',img)
cv2.waitKey(0)
cv2.destroyAllWindows()


来源:https://stackoverflow.com/questions/46764711/integer-argument-expected-int-got-float-in-opencv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!