0

opencv,我正在使用霍夫变换来寻找圆形这里是代码

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++ 异常:内存位置 0x002df08c 处的 cv::Exception。

如果我尝试cout中心坐标。

4

1 回答 1

0

嗯,这很有趣。我运行了你的代码并填写了之前和之后,没有任何问题。老实说,我只在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;
}
于 2013-02-22T21:52:52.943 回答