Error finding the center of an Opencv Python object

Asked

Viewed 185 times

1

Hello, I am studying opencv to use in a drone and while I was implementing a code of a tutorial that detected objects and drew a circle in the center of it, and for that you should take half the dimensions x+w and y+h only that the problem is that when I go to do x+w/2 and y+h/2appears the following error: integer argument expected, got float. HELP ME!!

Code:

import cv2
import numpy as np

cap = cv2.cv2.VideoCapture(0)
kernel = np.ones((5,5), np.uint8)

while True:
    ret, frame = cap.read()
    rangomax = np.array([50,255,50])
    rangomin = np.array([0, 51, 0])
    mascara = cv2.cv2.inRange(frame, rangomin, rangomax)
    opening = cv2.cv2.morphologyEx(mascara, cv2.cv2.MORPH_OPEN, kernel)
    x,y,w,h = cv2.cv2.boundingRect(opening)
    cv2.cv2.rectangle(frame, (x,y), (x+w, y+h), (0,255,0), 3)
    cv2.cv2.circle(frame,(x+w/2, y+h/2), 5,(0,0,255), -1)
    cv2.cv2.imshow('video', frame)
    k = cv2.cv2.waitKey(1) & 0xFF
    if k == 27:
        break
cap.release()

1 answer

1


It expects an int value try using the int() constructor to do what would be a cast

Python:

cv.Circle(img, center, radius, color, thickness=1, lineType=8, shift=0) → None

then it would be

cv2.cv2.circle(frame,(int(x+w/2), int(y+h/2)), 5,(0,0,255), -1)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.