can't run HoughCircles on image
I am trying to count objects in an image. To do that this is the approach I am using;
- Open an image file
- Convert the image file to HSV
- Extract S channel
- Run Gaussian Filter
- Otsu Thresholding
- Sobel Edge detection
- Hough Circle transform
- 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,
param1=50,param2=30,minRadius=0,maxRadius=-1)
Any help, on how can I fix this error.
Post original image.
This is the original image.