Ask Your Question

can't run HoughCircles on image

asked 2020-07-06 03:47:41 -0500

Ali001 gravatar image

updated 2020-11-24 02:23:12 -0500

I am trying to count objects in an image. To do that this is the approach I am using;

  1. Open an image file
  2. Convert the image file to HSV
  3. Extract S channel
  4. Run Gaussian Filter
  5. Otsu Thresholding
  6. Sobel Edge detection
  7. Hough Circle transform
  8. Detect and count

I have managed to complete the task upto Soble Edge detection. When I run Hough Circle transform, I get following error:

circles = cv.HoughCircles(sobely, cv.HOUGH_GRADIENT,1,20,
cv2.error: OpenCV(4.2.0) /io/opencv/modules/imgproc/src/hough.cpp:1728: error: (-215:Assertion failed) !_image.empty() && _image.type() == CV_8UC1 && (_image.isMat() || _image.isUMat()) in function 'HoughCircles'

This is the code I wrote so far. It is working fine until I try to run HoughCircles on the result of Sobel method.

import cv2 as cv
import numpy as np

src = cv.imread("both.png", cv.IMREAD_COLOR)

src = cv.cvtColor(src, cv.COLOR_RGB2HSV)

h, s, v = cv.split(src)

v = cv.GaussianBlur(v, (3,3),0,0)

ret2, otsu_src = cv.threshold(v,0,250, cv.THRESH_BINARY+cv.THRESH_OTSU)

sobely = cv.Sobel(otsu_src, cv.CV_64F, 0, 1, ksize=1)

circles = cv.HoughCircles(sobely, cv.HOUGH_GRADIENT,1,20,

Any help, on how can I fix this error.

image description

edit retag flag offensive close merge delete


Post original image.

supra56 gravatar imagesupra56 ( 2020-07-06 09:02:17 -0500 )edit

This is the original image.

Ali001 gravatar imageAli001 ( 2020-07-06 09:17:38 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2020-07-06 04:04:02 -0500

berak gravatar image

your Sobel outputs CV_64F, while HoughCircles expects CV_8U as input

since HoughCircles works on gradients, you probably should remove both threshold() and Sobel() ops (binarizing will be harmful)

again, have a look at the tutorial

edit flag offensive delete link more



there is new HOUGH_GRADIENT_ALT method. you also try it if available in your OpenCV version

sturkmen gravatar imagesturkmen ( 2020-07-06 04:11:34 -0500 )edit

How can I implement Multilevel Otsu threshold in OpenCV and Python?

Ali001 gravatar imageAli001 ( 2020-07-06 07:41:58 -0500 )edit

I'm a little complaint about something. in my opinion the correct answer should be @berak's answer. and @supra56 should hold down solve all problems with changing parameters. I'm not worried about offending anyone but quality answers are decreasing

sturkmen gravatar imagesturkmen ( 2020-07-07 03:51:34 -0500 )edit

I understood. Sorry about that.

supra56 gravatar imagesupra56 ( 2020-07-07 04:35:50 -0500 )edit

answered 2020-07-06 11:18:45 -0500

supra56 gravatar image

updated 2020-07-06 11:25:12 -0500

I solved problem. I play around with HoughCircles value 79, 23. And medianBlur to 11(odd number not even numbers)

#OpenCV 4.3.0, Raspberry pi3B/+, 4b/4g/8g, Thonny 3.7.3
#Date: 6th July, 2020

import numpy as np
import cv2 as cv
import sys

def main():
    fn = 'bottle_eggs.png'

    src = cv.imread(fn)
    img = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    img = cv.medianBlur(img, 11)
    cimg = src.copy() # numpy function

    circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 10, np.array([]), 79, 23, 9, 26)

    counter = 0
    if circles is not None: # Check if circles have been found and only then iterate over these and add them to the image
        _a, b, _c = circles.shape
        for i in range(b):
  , (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv.LINE_AA)  # draw center of circle
            counter += 1

        print('total both bottle and eggs {}'.format(counter))
        cv.imwrite('bottles_eggs.jpg', cimg)
        cv.imshow('detected circles', cimg)

    cv.imshow("source", src)

if __name__ == '__main__':

Output 50 circles:

image description

edit flag offensive delete link more


There are 20 bottles and 30 eggs for total of 50. You need to separated images not both.

supra56 gravatar imagesupra56 ( 2020-07-06 11:22:00 -0500 )edit

You don't needed step 5 and 6.

supra56 gravatar imagesupra56 ( 2020-07-07 04:32:34 -0500 )edit

Question Tools

1 follower


Asked: 2020-07-06 03:47:41 -0500

Seen: 553 times

Last updated: Jul 07 '20