How can I improve the counting of sticks in a bundle from looking at the end of the bundle?

74 Views Asked by At

I'm using OpenCV with Python to determine how many circular objects are on an image. I already wrote a small program to do that, but it's not precise enough. It seems to detect most of the objects, but I want a higher accuracy than this.

Any idea how to improve it's precision? Maybe a better method to detect all the (circular) objects in the images?

Changing the values don't seem to help much except the C parameter of the adaptiveThreshold function, but it is not enough.

All the images are similar or exactly like these: image1, image2

Code:

import cv2

img = cv2.imread("sticks01.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

threshold = cv2.adaptiveThreshold(gray, 355, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 8)
gauss = cv2.GaussianBlur(threshold, (3, 3), sigmaX=2.0, sigmaY=2.0)
circles = cv2.HoughCircles(gauss, cv2.HOUGH_GRADIENT, minDist=50, dp=1, param1=150, param2=15, minRadius=25, maxRadius=40)

# draw detected circles on image
for (x, y, r) in circles[0, :, :]:
    cv2.circle(img, (int(x), int(y)), int(r), (0, 255, 0), 4)

print(len(circles[0]))

cv2.imshow('image', img)

cv2.waitKey(0)
cv2.destroyAllWindows()
0

There are 0 best solutions below