我想使用OpenCV和C++检测图像中的圆圈。我可以通过参考official documentation并调整由OpenCV团队编写的代码段的参数来做到这一点。OpenCV - HoughCircles导致程序崩溃
所以,我正在使用的代码如下:(参数已调整)
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace cv;
int main(int, char** argv)
{
Mat src, src_gray;
/// Read the image
src = imread(argv[1], 1);
if(!src.data)
{ return -1; }
/// Convert it to gray
cvtColor(src, src_gray, CV_BGR2GRAY);
/// Reduce the noise so we avoid false circle detection
GaussianBlur(src_gray, src_gray, Size(9, 9), 2, 2);
vector<Vec3f> circles;
/// Apply the Hough Transform to find the circles
HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT, 6.0, 5, 110, 70, 3, 20);
/// Draw the circles detected
for(size_t i = 0; i < circles.size(); i++)
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][2]));
int radius = cvRound(circles[i][3]);
// 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);
}
/// Show your results
namedWindow("Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE);
imshow("Hough Circle Transform Demo", src);
waitKey(0);
src.release();
src_gray.release();
return 0;
}
我要检测其圆图像如下:Test image
这些其实都是我使用cvBlobsLib获得的两个斑点的轮廓并重新绘制为新图像。
该算法能够识别每圈的中心,但是,当我按任意键关闭程序,它崩溃... :(我不得不强行将其关闭。
我需要适应该算法在相机上运行,所以当它崩溃一样,我不能着手实施。
因此,没有人知道这可能是造成这个问题呢? 我正在做的事情的Visual Studio 2012开发和OpenCV version 2.4.2。
如果有人可以给我一个它可能是mayb或mayb的建议Ë尝试运行算法,我将非常感激!
谢谢您的回答! 我纠正了你指出的索引,并且拿出了我放在那里释放内存的行...并且没有任何变化:( 然后,我尝试评论代码的某些部分,让我的代码崩溃是对'HoughCircles'的调用...... 有一个有趣的是它在调用后不会导致崩溃 - 崩溃只发生在'return 0;'行... 在对'HoughCircles'的调用之后,我把一个'std :: cin.get();'和程序实际上等待输入... – Saph
是的,这是你可能看到的堆栈或堆损坏时的行为。通常可以继续工作,直到资源清理完毕,在这种情况下,从'main'返回,所以看起来好像是'HoughCircles'或'GaussianBlu r'或'cvtColor'正在做一些坏事。 – paddy