HOUGHCIRCLE generates strange numbers
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 (!src.data) {
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);
cv::waitKey(0);
return 0;
}
Here is the resuls;
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 resultsbecause 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.
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
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
I wrote for (size_t i = 0; i < circles.size(); i++) but I got so big points again. What is the solution ?
Can you please update your question and show drawn circles? Thank you
I updated the question and I get this result.
The code runs succesfully on my Win7 x64, VS 2013 and OpenCV 3.0. Can you please provide your original input image?
I updated and put the original input image there.
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);