2017-10-16 78 views
-1

我已經顯示下面的代碼,但是當我嘗試執行它,得到整數參數預期INT了漂浮在OpenCV的

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 

我不是很確定是什麼問題,在minEnclosingCircle的價值已轉換爲int,但我仍然得到相同的錯誤,有什麼想法可能是什麼問題?

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)) 
    raduis = 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()` 
+0

請更正您的Python代碼格式! – Silencer

回答

0

修改你的代碼如下,你不需要使用cv2.circle的返回值。

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

這不會消除錯誤,因爲浮點輸入參數應該是整數。所以,我認爲不是你的錯誤的確切解決方案。 – Jazz

1

我已經對浮點數轉換爲整數的代碼做了小的修改。它現在運行沒有錯誤。選中此項:

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() 
相關問題