0

I have this image: enter image description here

And, I want to detect all circles in it, I am using this tutorial.

Here is the code :

import argparse
import cv2 as cv
import numpy as np
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())
# load the image, clone it for output, and then convert it to grayscale
image = cv.imread(args["image"])

output = image.copy()
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# detect circles in the image
circles = cv.HoughCircles(gray,cv.HOUGH_GRADIENT, 1.2, 75)

# ensure at least some circles were found
if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")
    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        cv.circle(output, (x, y), r, (0, 255, 0), 4)
        cv.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)

    cv.imshow("output", np.hstack([image, output]))
    cv.waitKey(0)

Here is the output image after running code: enter image description here

The result is weird , why it is like this ? How can I detect all circles in it? What parameters should I change to achieve it?

After using:

circles = cv.HoughCircles(gray,cv.HOUGH_GRADIENT, 1.5, 75)

I got this : enter image description here

Anubhav Singh
  • 8,321
  • 4
  • 25
  • 43
Nasim
  • 161
  • 1
  • 3
  • 12
  • 1
    `circles = cv.HoughCircles(gray,cv.HOUGH_GRADIENT, 1.5, 75)` seems to find all. – Jeppe May 31 '19 at 09:28
  • i tried it, it seems its adding yello circle, but why is it repeating the image in the right side of the image? do you get result same as me? i mean it repeats in right side? – Nasim May 31 '19 at 09:32
  • 1
    That is because of `np.hstack([image, output])` when calling `cv.imshow("output", ...`. Replace with `cv.imshow("output", output)` to only show output frame. – Jeppe May 31 '19 at 09:42
  • Can you tell me how i should work with these parameters? how u could guess changing it to 1.5 will make it work? @Jeppe – Nasim May 31 '19 at 09:48
  • 1
    I don't know exactly how it works, which is why I added it as a comment and not an answer. But try reading [this](https://stackoverflow.com/questions/34706092/how-exactly-does-dp-parameter-in-cvhoughcircles-work) - essentially, by increasing the [`dp` parameter](https://docs.opencv.org/2.4/modules/imgproc/doc/feature_detection.html?highlight=houghcircles#houghcircles), I think we decrease the resolution of the voting-bins and thus allow less precision or more noise. I think this is needed here because the edges between the yellow, green and blue circles are overlapping. – Jeppe May 31 '19 at 10:22
  • thanks . i appreciate it if anyone could explain more about it – Nasim May 31 '19 at 11:04
  • You might get better results by thresholding before using HoughCircles. Check the threshold image to be sure it looks good and has found good circles. You probably can use Otsu thresholding to do that automatically. – fmw42 Sep 05 '20 at 17:17

1 Answers1

2

cv.imshow("output", np.hstack([image, output])) because of this code..you are stacking original image and output image together by np.hstack.

simply print cv.imshow("output",output).

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107