Houghcircles returns incorrect values

asked 2015-10-29 05:47:07 -0500

Ice_T02 gravatar image

updated 2015-10-29 06:52:41 -0500

Dear all,

I am pretty new to OpenCV. I decided myself to create a simple project finding circles in a picture following the offical OpenCV documentation about Hough transformation detecting circles (Link to the tutorial). The code works fine as long as I didn't try to draw the detected circles in the image. When reaching the drawing part i'll get following error:

OpenCV Error: Assertion failed (radius >= 0 && thickness <= MAX_THICKNESS && 0 <= shift && shift <= XY_SHIFT) in cv::circle, file C:\builds\master_PackSlave-win64-vc12-shared\opencv?modules\imgproc\src\drawing.cpp, line 1746

According to the error i checked the vector array which shall be filled by the houghcircles. I recognized that the function detects values but they exceed the picture size by a lot e.g.: picture size 640x480 --> x value 217685094. Moreover the radii is always 0 or revers meaning there are either only X/Y values or only a radii.

Here is the code i am using:

//Hough Transformation for a single frame
#include "stdafx.h"
#include <cstdio>

#include <opencv2\imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char **argv) 
Mat img = imread("circles.jpg", 0);
if (img.empty())
    cout << "Can not open" << endl;
    return -1;

Mat cimg;
medianBlur(img, img, 5);
cvtColor(img, cimg, COLOR_GRAY2BGR);

vector <Vec3f> circles;
HoughCircles(img, circles, HOUGH_GRADIENT, 1, 10, 100, 50, 1, 30);
for (size_t i = 0; i < circles.size(); i++)
    Vec3i c = circles[i];
    circle(cimg, Point(c[0], c[1]), c[2], Scalar(0, 0, 255), 3, LINE_AA);
    circle(cimg, Point(c[0], c[1]), c[2], Scalar(0, 255, 0), 3, LINE_AA);

imshow("detected circles", cimg);

return 0;

... and also the picture where I try to finde the circles:


I use Visual Studio 2015 with OpenCV 3.0 vc12 under C++.

My question is, if my set parameters are totally off so houghcircles can't work poperly or is it another issue i didn't recognized yet. Thank you very much for your help.

edit retag flag offensive close merge delete



no problem with the code. i think your problem is about your configuration with Visual Studio

sturkmen gravatar imagesturkmen ( 2015-10-29 07:04:08 -0500 )edit

Why are you using vc12 prebuilts (meant for Visual Studio 2013) in Visual Studio 2015?

LorenaGdL gravatar imageLorenaGdL ( 2015-10-29 07:09:00 -0500 )edit

Hey, thank you for your answers. Shall i use vc11 instead? I set up the project properties according to this tutroial: http://opencv-srf.blogspot.co.at/2013....

Ice_T02 gravatar imageIce_T02 ( 2015-10-29 07:32:09 -0500 )edit

as far as i know there is no prebuilt library for Visual Studio 2015. you must build yourself.

sturkmen gravatar imagesturkmen ( 2015-10-29 07:36:32 -0500 )edit

Ok, thank you very much for your help.

Ice_T02 gravatar imageIce_T02 ( 2015-10-29 07:42:23 -0500 )edit

@Ice_T02: you need to use the proper prebuilts binaries for your version of Visual Studio. vc11 ones are for Visual Studio 2012, so don't use them. I'm not really sure there are already prebuilts binaries for Visual Studio 2015, but you can build them using CMake. Full instructions can be found in this tutorial (though it is kind of old, steps are basically the same. Also, just focus on the CMake part, the rest of tools are totally optionally and I'm sure you don't want to mess around with them right now). You can get the last version of source files (you build libraries from them) in the official GitHub repository (you can download a .zip on the right bar)

LorenaGdL gravatar imageLorenaGdL ( 2015-10-29 07:48:25 -0500 )edit

@LorenaGdL thank you for your advise. I am currently installing VS2013 and will test my program with OpenCV cv12. Once finished, i will take a closer look at the tutorials you listed. Thank you very much to both of you, for your time and the detailed answers :D.

Ice_T02 gravatar imageIce_T02 ( 2015-10-29 08:34:04 -0500 )edit