圆形霍夫中心坐标

时间:2013-02-21 16:42:57

标签: c++ opencv

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中心坐标。

1 个答案:

答案 0 :(得分:0)

嗯,这很有意思。我运行你的代码并填写前后,并没有任何问题。说实话,我只在linux和mac机器上测试过。这是我的全长代码,尝试一下,看看会发生什么。另外,请检查此解决方案here

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;
}