2016-06-23 4 views
0

Я получаю эту ошибку всякий раз, когда я использую функцию contourArea в OpenCV ErrorAssertion Ошибка при использовании контура Area-OpenCV

Вот мой код

while (true) 
{ 
    Mat imgOriginal; 

    bool bSuccess = cap.read(imgOriginal); // read a new frame from video 



    if (!bSuccess) //if not success, break loop 
    { 
     cout << "Cannot read a frame from video stream" << endl; 
     break; 
    } 

    Mat imgHSV; 

    cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV 

    Mat imgThresholded; 

    inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image 

                            //morphological opening (removes small objects from the foreground) 
    erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 
    dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 

    //morphological closing (removes small holes from the foreground) 
    dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 
    erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 


    int thresh = 100; 
    Mat canny_output; 
    vector<Vec4i> hierarchy; 
    vector<vector<Point> > contours; 
    RNG rng(12345); 

    Canny(imgThresholded, canny_output, thresh, thresh * 2, 3); 
    /// Find contours 
    findContours(canny_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 

    /// Draw contours 
    Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3); 
    for (int i = 0; i< contours.size(); i++) 
    { 
     Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); 
     drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point()); 
    } 


    double area = contourArea(contours); 
    std::cout << area; 

    imshow("contours", drawing); 

Мой код работает в основном нормально до этого, но очень важно, чтобы я захватил область контура, но он продолжает давать мне эту ошибку. Любая помощь очень ценится

+0

положить его в для петли 'двойной области = contourArea (контуры [я]);' – sturkmen

ответ

1

cv::contourArea() принимает один контур и возвращает площадь этого контура, Поскольку contours в вашем случае это список контуров, которые могут содержать 0 или более контуров, так что вам явно нужно доступ к контуру, используя индекс как:

if (contours.size() > 0) { 
    for (int i=0; i<contours.size(); i++) { 
     std::cout << "Contour id : " << i << ", area = " << cv::contourArea(contours[i]) << std::endl; 
    } 
} else { 
    std::cout << "No contours found !" << std::endl; 
} 
+0

Вы можете спокойно избежать 'if', так как проверка уже в' для ' – Miki

+0

Да, именно так, но было просто напечатать какую-то ошибку в' '' '' '' '' '' '', я бы отредактировал его, чтобы сделать его более понятным. – ZdaR

+0

уверен, уже + 1'ed; D – Miki

Смежные вопросы