1
votes

This is my first attempt at image processing so please bear with me. I am trying to run the following python example to find all Hough Circles in the image. However, when I get the following error:

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/array.cpp, line 2482 Traceback (most recent call last): File "hough_circles.py", line 10, in param1=50,param2=30,minRadius=0,maxRadius=0) cv2.error: /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat

Below is my code:

import cv2
import cv2.cv as cv # here
import numpy as np

img = cv2.imread('opencv-logo.png',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,20,
                            param1=50,param2=30,minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()

This is the opencv-logo.jpg image I am trying to use. Can you please help me run this example and guide me through the process of looping through the radius of each found circle?

Thank you

1

1 Answers

2
votes

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type)

HoughCircles is expecting a numpy array as its first argument. The above error means that it didn't get it. The problem is that your input file is a jpg and the code is asking for a png:

img = cv2.imread('opencv-logo.png',0)

When a file does not exist, cv2.imread quietly returns a None. Consequently, img is set to None. When cv2.HoughCircles receives that value as its first argument, it raises the error.

To solve the problem, replace the above line with:

img = cv2.imread('opencv-logo.jpg',0)

With that change, your code runs, finds many many potential circles, and produces the image:

enter image description here

You can control the number of circles found by changing the various parameters. Increasing the canny parameters to 70 and 50, for example, will reduce the number of circles found to seven.