Alex z
Alex z

Reputation: 33

What are the basics to OpenCv Circle reshape() and syntax?

I just wrote this simple code to track circles, it is not tunned at all and I want to fix my error before I go on. It says:

ValueError: cannot reshape array of size 6 into shape (3,)

I tried looking online, but I'm in over my head. Can someone point me in the right direction?

import cv2
import numpy as np


cap = cv2.VideoCapture(0)
def circle_image(image, circles):
    image_circles = np.zeros_like(image)
    if circles is not None:
        for circle in circles:
            rad, x1, y1 = circle.reshape(3)
            cv2.circle(image_circles,(x1,y1),rad,(255,0,0), 10)
    return  image_circles

while cap.isOpened():
    ret, frame = cap.read()
    if ret == True:
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        blur_frame = cv2.GaussianBlur(gray_frame, (5, 5), 0)
        canny_frame = cv2.Canny(blur_frame, 100, 100)
        tires = cv2.HoughCircles(canny_frame,cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)

        circles_to_blend = circle_image(canny_frame, tires)
        combo_image = cv2.addWeighted(canny_frame, 0.8, circles_to_blend, 1, 1)

        cv2.imshow('frame', combo_image)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Upvotes: 0

Views: 132

Answers (1)

api55
api55

Reputation: 11420

I actually had to test the HoughCircles in python to understand the problem. I don not know why, but the HoughCircles function returns in python a list of list of circles. So you want to do

for circle in circles[0,:]:

for it to work. However, in the documentation you can clearly see that this is wrong:

rad, x1, y1 = circle.reshape(3)

The circle should be x,y, rad and also you do not need to reshape. In some cases it may be 4 values (the votes) and this line will fail, since 4 values cannot be reshaped as 3, but you can do the following:

x1, y1, rad = circle[:3] # this takes the first 3 numbers

I think after these changes your code should work.

Upvotes: 3

Related Questions