Circle detection with OpenCV











up vote
2
down vote

favorite
3












How can I improve the performance of the following circle-detection code



from matplotlib.pyplot import imshow, scatter, show
import cv2

image = cv2.imread('points.png', 0)
_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY)
image = cv2.Canny(image, 1, 1)
imshow(image, cmap='gray')

circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 2, 32)
x = circles[0, :, 0]
y = circles[0, :, 1]

scatter(x, y)
show()


with the following source image:



enter image description here



I have tried adjusting the parameters of the HoughCircles function but they result in either too many false positives or too many false negatives. In particular, I am having trouble with spurious circles being detected in the gap between the two blobs:



enter image description here










share|improve this question






















  • Consider accepting the answer if you think it was helpful.
    – m3h0w
    Mar 10 '17 at 11:25















up vote
2
down vote

favorite
3












How can I improve the performance of the following circle-detection code



from matplotlib.pyplot import imshow, scatter, show
import cv2

image = cv2.imread('points.png', 0)
_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY)
image = cv2.Canny(image, 1, 1)
imshow(image, cmap='gray')

circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 2, 32)
x = circles[0, :, 0]
y = circles[0, :, 1]

scatter(x, y)
show()


with the following source image:



enter image description here



I have tried adjusting the parameters of the HoughCircles function but they result in either too many false positives or too many false negatives. In particular, I am having trouble with spurious circles being detected in the gap between the two blobs:



enter image description here










share|improve this question






















  • Consider accepting the answer if you think it was helpful.
    – m3h0w
    Mar 10 '17 at 11:25













up vote
2
down vote

favorite
3









up vote
2
down vote

favorite
3






3





How can I improve the performance of the following circle-detection code



from matplotlib.pyplot import imshow, scatter, show
import cv2

image = cv2.imread('points.png', 0)
_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY)
image = cv2.Canny(image, 1, 1)
imshow(image, cmap='gray')

circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 2, 32)
x = circles[0, :, 0]
y = circles[0, :, 1]

scatter(x, y)
show()


with the following source image:



enter image description here



I have tried adjusting the parameters of the HoughCircles function but they result in either too many false positives or too many false negatives. In particular, I am having trouble with spurious circles being detected in the gap between the two blobs:



enter image description here










share|improve this question













How can I improve the performance of the following circle-detection code



from matplotlib.pyplot import imshow, scatter, show
import cv2

image = cv2.imread('points.png', 0)
_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY)
image = cv2.Canny(image, 1, 1)
imshow(image, cmap='gray')

circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 2, 32)
x = circles[0, :, 0]
y = circles[0, :, 1]

scatter(x, y)
show()


with the following source image:



enter image description here



I have tried adjusting the parameters of the HoughCircles function but they result in either too many false positives or too many false negatives. In particular, I am having trouble with spurious circles being detected in the gap between the two blobs:



enter image description here







python opencv geometry computer-vision feature-detection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 '17 at 21:26









user76284

401515




401515












  • Consider accepting the answer if you think it was helpful.
    – m3h0w
    Mar 10 '17 at 11:25


















  • Consider accepting the answer if you think it was helpful.
    – m3h0w
    Mar 10 '17 at 11:25
















Consider accepting the answer if you think it was helpful.
– m3h0w
Mar 10 '17 at 11:25




Consider accepting the answer if you think it was helpful.
– m3h0w
Mar 10 '17 at 11:25












1 Answer
1






active

oldest

votes

















up vote
7
down vote



accepted










@Carlos, I'm not really a big fan of Hough Circles in situations like the one you've described. To be honest, I find this algorithm very unintuitive. What I would recommend in your case is using findContour() function and then calculating mass centers. Thus said, I tuned the Hough's parameters a bit to get reasonable results. I also used a different method for preprocessing before Canny, since I don't see how that thresholding would work in any other case than that particular one.



Hough method:
enter image description here



Finding mass centers:
enter image description here



And the code:



from matplotlib.pyplot import imshow, scatter, show, savefig
import cv2

image = cv2.imread('circles.png', 0)
#_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY)
image = cv2.GaussianBlur(image.copy(), (27, 27), 0)
image = cv2.Canny(image, 0, 130)
cv2.imshow("canny", image)
cv2.waitKey(0)
imshow(image, cmap='gray')

circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 22, minDist=1, maxRadius=50)
x = circles[0, :, 0]
y = circles[0, :, 1]

scatter(x, y)
show()
savefig('result1.png')
cv2.waitKey(0)

_, cnts, _ = cv2.findContours(image.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)

# loop over the contours
for c in cnts:
# compute the center of the contour
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

#draw the contour and center of the shape on the image
cv2.drawContours(image, [c], -1, (125, 125, 125), 2)
cv2.circle(image, (cX, cY), 3, (255, 255, 255), -1)

cv2.imshow("Image", image)
cv2.imwrite("result2.png", image)
cv2.waitKey(0)


Both methods require some more fine tuning but I hope that gives you something more to work with.



Sources: this answer and pyimagesearch.






share|improve this answer



















  • 1




    Hough transform gets unreliable when there are many circles close together, because parts of different circles combine to give the likelihood of a new circle. As @m3h0w has shown, you can tun the Hough parameters and also use more advanced features like contours and moments. The key thing is to either preprocess images before Hough transform, or post-process the results after the transform, or do both.
    – Totoro
    Mar 8 '17 at 2:13












  • @m3h0w Aha another master stroke. Wish there was an option to favorite the answer as well.
    – Jeru Luke
    Mar 10 '17 at 13:35













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42658653%2fcircle-detection-with-opencv%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
7
down vote



accepted










@Carlos, I'm not really a big fan of Hough Circles in situations like the one you've described. To be honest, I find this algorithm very unintuitive. What I would recommend in your case is using findContour() function and then calculating mass centers. Thus said, I tuned the Hough's parameters a bit to get reasonable results. I also used a different method for preprocessing before Canny, since I don't see how that thresholding would work in any other case than that particular one.



Hough method:
enter image description here



Finding mass centers:
enter image description here



And the code:



from matplotlib.pyplot import imshow, scatter, show, savefig
import cv2

image = cv2.imread('circles.png', 0)
#_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY)
image = cv2.GaussianBlur(image.copy(), (27, 27), 0)
image = cv2.Canny(image, 0, 130)
cv2.imshow("canny", image)
cv2.waitKey(0)
imshow(image, cmap='gray')

circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 22, minDist=1, maxRadius=50)
x = circles[0, :, 0]
y = circles[0, :, 1]

scatter(x, y)
show()
savefig('result1.png')
cv2.waitKey(0)

_, cnts, _ = cv2.findContours(image.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)

# loop over the contours
for c in cnts:
# compute the center of the contour
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

#draw the contour and center of the shape on the image
cv2.drawContours(image, [c], -1, (125, 125, 125), 2)
cv2.circle(image, (cX, cY), 3, (255, 255, 255), -1)

cv2.imshow("Image", image)
cv2.imwrite("result2.png", image)
cv2.waitKey(0)


Both methods require some more fine tuning but I hope that gives you something more to work with.



Sources: this answer and pyimagesearch.






share|improve this answer



















  • 1




    Hough transform gets unreliable when there are many circles close together, because parts of different circles combine to give the likelihood of a new circle. As @m3h0w has shown, you can tun the Hough parameters and also use more advanced features like contours and moments. The key thing is to either preprocess images before Hough transform, or post-process the results after the transform, or do both.
    – Totoro
    Mar 8 '17 at 2:13












  • @m3h0w Aha another master stroke. Wish there was an option to favorite the answer as well.
    – Jeru Luke
    Mar 10 '17 at 13:35

















up vote
7
down vote



accepted










@Carlos, I'm not really a big fan of Hough Circles in situations like the one you've described. To be honest, I find this algorithm very unintuitive. What I would recommend in your case is using findContour() function and then calculating mass centers. Thus said, I tuned the Hough's parameters a bit to get reasonable results. I also used a different method for preprocessing before Canny, since I don't see how that thresholding would work in any other case than that particular one.



Hough method:
enter image description here



Finding mass centers:
enter image description here



And the code:



from matplotlib.pyplot import imshow, scatter, show, savefig
import cv2

image = cv2.imread('circles.png', 0)
#_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY)
image = cv2.GaussianBlur(image.copy(), (27, 27), 0)
image = cv2.Canny(image, 0, 130)
cv2.imshow("canny", image)
cv2.waitKey(0)
imshow(image, cmap='gray')

circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 22, minDist=1, maxRadius=50)
x = circles[0, :, 0]
y = circles[0, :, 1]

scatter(x, y)
show()
savefig('result1.png')
cv2.waitKey(0)

_, cnts, _ = cv2.findContours(image.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)

# loop over the contours
for c in cnts:
# compute the center of the contour
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

#draw the contour and center of the shape on the image
cv2.drawContours(image, [c], -1, (125, 125, 125), 2)
cv2.circle(image, (cX, cY), 3, (255, 255, 255), -1)

cv2.imshow("Image", image)
cv2.imwrite("result2.png", image)
cv2.waitKey(0)


Both methods require some more fine tuning but I hope that gives you something more to work with.



Sources: this answer and pyimagesearch.






share|improve this answer



















  • 1




    Hough transform gets unreliable when there are many circles close together, because parts of different circles combine to give the likelihood of a new circle. As @m3h0w has shown, you can tun the Hough parameters and also use more advanced features like contours and moments. The key thing is to either preprocess images before Hough transform, or post-process the results after the transform, or do both.
    – Totoro
    Mar 8 '17 at 2:13












  • @m3h0w Aha another master stroke. Wish there was an option to favorite the answer as well.
    – Jeru Luke
    Mar 10 '17 at 13:35















up vote
7
down vote



accepted







up vote
7
down vote



accepted






@Carlos, I'm not really a big fan of Hough Circles in situations like the one you've described. To be honest, I find this algorithm very unintuitive. What I would recommend in your case is using findContour() function and then calculating mass centers. Thus said, I tuned the Hough's parameters a bit to get reasonable results. I also used a different method for preprocessing before Canny, since I don't see how that thresholding would work in any other case than that particular one.



Hough method:
enter image description here



Finding mass centers:
enter image description here



And the code:



from matplotlib.pyplot import imshow, scatter, show, savefig
import cv2

image = cv2.imread('circles.png', 0)
#_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY)
image = cv2.GaussianBlur(image.copy(), (27, 27), 0)
image = cv2.Canny(image, 0, 130)
cv2.imshow("canny", image)
cv2.waitKey(0)
imshow(image, cmap='gray')

circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 22, minDist=1, maxRadius=50)
x = circles[0, :, 0]
y = circles[0, :, 1]

scatter(x, y)
show()
savefig('result1.png')
cv2.waitKey(0)

_, cnts, _ = cv2.findContours(image.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)

# loop over the contours
for c in cnts:
# compute the center of the contour
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

#draw the contour and center of the shape on the image
cv2.drawContours(image, [c], -1, (125, 125, 125), 2)
cv2.circle(image, (cX, cY), 3, (255, 255, 255), -1)

cv2.imshow("Image", image)
cv2.imwrite("result2.png", image)
cv2.waitKey(0)


Both methods require some more fine tuning but I hope that gives you something more to work with.



Sources: this answer and pyimagesearch.






share|improve this answer














@Carlos, I'm not really a big fan of Hough Circles in situations like the one you've described. To be honest, I find this algorithm very unintuitive. What I would recommend in your case is using findContour() function and then calculating mass centers. Thus said, I tuned the Hough's parameters a bit to get reasonable results. I also used a different method for preprocessing before Canny, since I don't see how that thresholding would work in any other case than that particular one.



Hough method:
enter image description here



Finding mass centers:
enter image description here



And the code:



from matplotlib.pyplot import imshow, scatter, show, savefig
import cv2

image = cv2.imread('circles.png', 0)
#_, image = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY)
image = cv2.GaussianBlur(image.copy(), (27, 27), 0)
image = cv2.Canny(image, 0, 130)
cv2.imshow("canny", image)
cv2.waitKey(0)
imshow(image, cmap='gray')

circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 22, minDist=1, maxRadius=50)
x = circles[0, :, 0]
y = circles[0, :, 1]

scatter(x, y)
show()
savefig('result1.png')
cv2.waitKey(0)

_, cnts, _ = cv2.findContours(image.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)

# loop over the contours
for c in cnts:
# compute the center of the contour
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

#draw the contour and center of the shape on the image
cv2.drawContours(image, [c], -1, (125, 125, 125), 2)
cv2.circle(image, (cX, cY), 3, (255, 255, 255), -1)

cv2.imshow("Image", image)
cv2.imwrite("result2.png", image)
cv2.waitKey(0)


Both methods require some more fine tuning but I hope that gives you something more to work with.



Sources: this answer and pyimagesearch.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 22:11

























answered Mar 8 '17 at 0:24









m3h0w

1,02011027




1,02011027








  • 1




    Hough transform gets unreliable when there are many circles close together, because parts of different circles combine to give the likelihood of a new circle. As @m3h0w has shown, you can tun the Hough parameters and also use more advanced features like contours and moments. The key thing is to either preprocess images before Hough transform, or post-process the results after the transform, or do both.
    – Totoro
    Mar 8 '17 at 2:13












  • @m3h0w Aha another master stroke. Wish there was an option to favorite the answer as well.
    – Jeru Luke
    Mar 10 '17 at 13:35
















  • 1




    Hough transform gets unreliable when there are many circles close together, because parts of different circles combine to give the likelihood of a new circle. As @m3h0w has shown, you can tun the Hough parameters and also use more advanced features like contours and moments. The key thing is to either preprocess images before Hough transform, or post-process the results after the transform, or do both.
    – Totoro
    Mar 8 '17 at 2:13












  • @m3h0w Aha another master stroke. Wish there was an option to favorite the answer as well.
    – Jeru Luke
    Mar 10 '17 at 13:35










1




1




Hough transform gets unreliable when there are many circles close together, because parts of different circles combine to give the likelihood of a new circle. As @m3h0w has shown, you can tun the Hough parameters and also use more advanced features like contours and moments. The key thing is to either preprocess images before Hough transform, or post-process the results after the transform, or do both.
– Totoro
Mar 8 '17 at 2:13






Hough transform gets unreliable when there are many circles close together, because parts of different circles combine to give the likelihood of a new circle. As @m3h0w has shown, you can tun the Hough parameters and also use more advanced features like contours and moments. The key thing is to either preprocess images before Hough transform, or post-process the results after the transform, or do both.
– Totoro
Mar 8 '17 at 2:13














@m3h0w Aha another master stroke. Wish there was an option to favorite the answer as well.
– Jeru Luke
Mar 10 '17 at 13:35






@m3h0w Aha another master stroke. Wish there was an option to favorite the answer as well.
– Jeru Luke
Mar 10 '17 at 13:35




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42658653%2fcircle-detection-with-opencv%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues