在opencv
中,我使用Hough变换来查找圆,这里是代码
HoughCircles (diff, circles, CV_HOUGH_GRADIENT, 2, src.cols / 5, 200, 80, 20, 62);
for (size_t i = 0; i < circles.size(); i++ )
{
//if(circles[i][2]<62)
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
// draw the green circle center
circle( src, center, 3, Scalar(0,255,255), -1, 8, 0 );
// draw the blue circle outline
circle(src, center, radius, Scalar(0,255,0), 3, 8, 0 );
}
}
我面临的问题是,有时在它找到3个圆的情况下,第三个圆心坐标是分数,而不是整数。如果它找到4个圆,它会给出这个错误
xyz.exe中0x75ebc41f处未处理的异常:Microsoft C++异常:cv::内存位置0x002df08c出现异常。。
如果我尝试cout
中心坐标。
嗯,这很有趣。我运行了你的代码,并在之前和之后填写了,没有任何问题。老实说,我只在linux和mac机器上测试过。这是我的全长代码,试试看会发生什么。此外,请在此处查看此解决方案。
int main(int argc, char* argv[]) {
VideoCapture capture(0);
if (!capture.isOpened()) {
LOG(FATAL) << "COULD NOT OPEN CAPTURE";
}
Mat frame;
capture >> frame;
if (frame.empty()) {
LOG(FATAL) << "FRAME IS EMPTY!";
}
char key;
while ((int)key != 27) {
capture >> frame;
Mat gray;
cvtColor(frame, gray, CV_BGR2GRAY);
GaussianBlur(gray, gray, Size(9, 9), 2, 2);
vector<Vec3f> circles;
HoughCircles(gray, circles, CV_HOUGH_GRADIENT, 2, gray.cols/5,200,80,20,62);
for (size_t i = 0; i < circles.size(); ++i) {
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
// draw the green circle center
circle(frame, center, 3, Scalar(0,255,255), -1, 8, 0 );
// draw the blue circle outline
circle(frame, center, radius, Scalar(0,255,0), 3, 8, 0 );
}
imshow("frame", frame);
key = waitKey(1);
}
return 0;
}