2013-02-21 125 views
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個圓圈它給這個錯誤

未處理的異常在0x75ebc41f在xyz.exe:微軟C++異常: CV ::異常在存儲器位置0x002df08c ..

如果我試圖以cout爲中心座標。

回答

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