Ask Your Question

HOUGHCIRCLE generates strange numbers

asked 2015-11-24 05:49:34 -0500

WH gravatar image

updated 2015-11-24 14:49:23 -0500

I am working on detecting circles on an image and I am using the houghcircle function but the function generates so interesting numbers. I use VS-2013 for c++ ide and use 3.0 version opencv. Why why ???? Here is my code ;

#include "stdafx.h"
#include <cstdio>
#include <iostream> 
#include <stdio.h>
#include <opencv2\opencv.hpp>
#include <opencv\cv.h>
#include <opencv2\core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {

cv::Mat src = cv::imread("asd.jpg");

if (! {
    std::cout << "ERROR:\topening image" << std::endl;
    return -1;
cv::namedWindow("image", CV_WINDOW_AUTOSIZE);
cv::imshow("image", src);

Mat src_gray2;
cvtColor(src, src_gray2, CV_BGR2GRAY);
GaussianBlur(src_gray2, src_gray2, cv::Size(9, 9), 2, 2);
Canny(src_gray2, src_gray2, 25, 35, 3, true);
std::vector<Vec3f> circles; 
cout << circles.size() << std::endl;
HoughCircles(src_gray2, circles, CV_HOUGH_GRADIENT, 2, 100, 35, 30, 50, 70);

for (size_t i = 0; i < 20; i++)
    Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
    int radius = cvRound(circles[i][2]);
    // circle center
    //circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);
    // circle outline
    //circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);
    cout << i << "----> " << center << endl;

cv::namedWindow("image", CV_WINDOW_AUTOSIZE);
cv::imshow("image", src_gray2);
return 0;

Here is the resuls; image description

edit retag flag offensive close merge delete


Why are you using for (size_t i = 0; i < 20; i++)? You may end up in accesing items out of the array bounds. Also, drawing your found circles should shed some light on the results

LorenaGdL gravatar imageLorenaGdL ( 2015-11-24 06:08:20 -0500 )edit

because of when i use circles.size() instead of < 20 the program gives a break. T he only reason that i use < 20 is just trying and seeing what happens in background. The points are so big and negative. I mean the points are not coordinates there is something worng with opencv. There is no any wrong with this code.

WH gravatar imageWH ( 2015-11-24 06:52:02 -0500 )edit

I'm telling you there is something wrong with your code. If you don't want to fix that in the first place, then obviously you'll run into problems

LorenaGdL gravatar imageLorenaGdL ( 2015-11-24 07:08:11 -0500 )edit

Can you please tell me where I should fix ? The problem is points are so so big but the image is 500*500. Can you just write that line which will fix the problem please

WH gravatar imageWH ( 2015-11-24 07:25:37 -0500 )edit

I wrote for (size_t i = 0; i < circles.size(); i++) but I got so big points again. What is the solution ?

WH gravatar imageWH ( 2015-11-24 07:28:23 -0500 )edit

Can you please update your question and show drawn circles? Thank you

LorenaGdL gravatar imageLorenaGdL ( 2015-11-24 07:34:36 -0500 )edit

I updated the question and I get this result.

WH gravatar imageWH ( 2015-11-24 07:45:38 -0500 )edit

The code runs succesfully on my Win7 x64, VS 2013 and OpenCV 3.0. Can you please provide your original input image?

LorenaGdL gravatar imageLorenaGdL ( 2015-11-24 07:57:01 -0500 )edit

I updated and put the original input image there.

WH gravatar imageWH ( 2015-11-24 08:01:53 -0500 )edit

No error here, though with your params no circle is obtained. I get 3 acceptable circles with HoughCircles(src_gray2, circles, CV_HOUGH_GRADIENT, 2, 20, 100, 80, 80, 0);

LorenaGdL gravatar imageLorenaGdL ( 2015-11-24 08:12:18 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-11-24 12:50:32 -0500

jmbapps gravatar image

updated 2015-11-24 13:55:41 -0500

This is exactly what my app does in iOS. Your circle is HUGE, easier to start with smaller coins. First off, check your header files for something like #import <opencv2/opencv.hpp>, this includes all the imports or "includes" in your case.. The only thing in my code that is not in yours is I run the Canny Edge Detectorcv::Canny(gray, edges, t1, t2, aS, l2); //25, 35, 3, true (sub numbers for my int parameters) after the Gaussian Blur. Also...if your parameters for Hough Circles are in the same order as mine then I would use something like this in the parameters after the Gradient. 1 or 2 for the AIR. 200, 35, 30, 300, 600. The first number is the Accumulated Inverse Ratio which determines the amount of pixels used from the image....either all or half. The second is the Minimum Distance Between Centers amount...and 20 is a bit small, since the ball is most of the image. Third parameter should be the same as the Threshold 2 setting in your Canny Edge Detector, which you should add, imo. Fourth is the Detection Filter and you have it maxed....drop that to 20 or 30. Fifth it is Minimum Distance, smallest circle. Sixth is Maximum Distance, largest circle. 0, 0 for the last two are common to scan the pic for every size possible, it is better to enter parameters so you will become familiar on how HC works. The numbers I gave you are for detecting the circle in your image. If you use some coins (easier) then try 2, 100, 35, 40, 50, 70. Hope this helps.

edit flag offensive delete link more


Thank you for your perfect answer but I did everthing I tried everthing but the result is like above. I updated my Question. The numbers are so huge and negative. What the hell is that ?

WH gravatar imageWH ( 2015-11-24 14:47:44 -0500 )edit

By the way, can you please tell me where you set up opencv on your vs-2013. Just please give me link.

WH gravatar imageWH ( 2015-11-24 14:56:03 -0500 )edit

I cant help you there, I use Xcode for iOS,.. drag and drop it in project...make sure to check 'Copy Files'. That probably wont work for you. Sorry. I do have a link for Android Studio....probably wont help either. How many circles are you registering? Switch your imshow to src. The circles and centers are being drawn on that image. Uncomment those two lines of code in your 'for' loop, also set for(size_t i = 0; i < circles.size(); i++) like you had it before. I have no idea about the numbers..I would guess 'center' and 'radius' numbers since that is all your for loop is generating? I use version 2.4.7. 3.0 doesnt work.

jmbapps gravatar imagejmbapps ( 2015-11-24 16:18:25 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-11-24 05:49:34 -0500

Seen: 182 times

Last updated: Nov 24 '15