2011-09-29 3 views
1

나는 cvFindContours()를 사용하여 imageContours에 결과를 저장 한 이미지가 있습니다. 나는 그 때 2 개의 가장 큰 윤곽을 찾아 내고 그 (것)들을 표시하는 것을 시도하고있다. 그러나 2 가지 이상의 윤곽을 보여주는 이유가 있습니다. 내 알고리즘에 무슨 문제가 있습니까? 감사합니다OpenCV가 가장 큰 윤곽을 찾으십니까?

EDIT : 오, 또한, 콘솔에서, 나는 "오류"메시지를 받고 있는데, 그 중 하나라도 옵션이 사실이 아니라면 아래에 프로그램되어 있습니다. 왜 그런지 알아?

cvFindContours (binMask, imageContoursMem, &imageContours, sizeof (CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); //find the contours in binMask, and store results in ImageContours 

    //following conditions used to set f_handContour to the bigger contour for the first time, and s_handContour to the smaller contour for the first time 
    if ((cvContourArea (imageContours, CV_WHOLE_SEQ)) > (cvContourArea((imageContours -> h_next), CV_WHOLE_SEQ))) 
    { //begin first contour bigger condition 

     f_handContour = imageContours; 
     s_handContour = (imageContours -> h_next); 

    } //end second contour bigger condition 

    else if ((cvContourArea (imageContours, CV_WHOLE_SEQ)) < (cvContourArea((imageContours -> h_next), CV_WHOLE_SEQ))) 
    { //begin second contour bigger condition 

     f_handContour = (imageContours -> h_next); 
     s_handContour = imageContours; 

    } //begin second contour biggger condition 

    else if ((cvContourArea (imageContours, CV_WHOLE_SEQ)) == (cvContourArea((imageContours -> h_next), CV_WHOLE_SEQ))) 
    { //begin contours equal condition 

     f_handContour = imageContours; //if contours equal assignment of contours doesn't matter 
     s_handContour = (imageContours -> h_next); 

    } //end contours equal condition 

    else 
    { //begin error condition 

     cout<<"Error"; 

    } //end error condition 

    while ((imageContours -> h_next) != NULL) 
    { //start of biggest/second biggest contour recognition loop 

     imageContours = (imageContours -> h_next); 

     if (cvContourArea (f_handContour, CV_WHOLE_SEQ) < cvContourArea (imageContours, CV_WHOLE_SEQ)) 
     { //start of next contour bigger than f_handContour condition 

      s_handContour = f_handContour; 
      f_handContour = imageContours; 

     } //end start of next contour bigger than f_handContour condition 

     else if (cvContourArea (f_handContour, CV_WHOLE_SEQ) > cvContourArea (imageContours, CV_WHOLE_SEQ)) 
     { //start of next contour smaller than f_handContour condition 

      if (cvContourArea (s_handContour, CV_WHOLE_SEQ) < cvContourArea (imageContours, CV_WHOLE_SEQ)) 
      { //startof next contour bigger than s_handContour condition 

       s_handContour = imageContours; 

      } //end of next contour bigger than s_handContour condition 

      else if (cvContourArea (s_handContour, CV_WHOLE_SEQ) > cvContourArea (imageContours, CV_WHOLE_SEQ)) 
      { //start of next contour smaller than s_handContour condition 

       //just pass on contour 

      } //end of next contour smaller than s_handContour condition 

      else if (cvContourArea (s_handContour, CV_WHOLE_SEQ) == cvContourArea (imageContours, CV_WHOLE_SEQ)) 
      { //start of next contour equal to s_handContour condition 

       //just pass on contour 

      } // end of next contour equal to s_handContour condition 

      else 
      { //start of error condition 

       cout<<"Error"; 

      } //end of error condition 

     } //end of next contour smaller than f_handContour Condition 

     else if (cvContourArea (f_handContour, CV_WHOLE_SEQ) == cvContourArea (imageContours, CV_WHOLE_SEQ)) 
     { //start of next contour equal to f_handContour condition 

      if (cvContourArea (s_handContour, CV_WHOLE_SEQ) < cvContourArea (imageContours, CV_WHOLE_SEQ)) 
      { //startof next contour bigger than s_handContour condition 

       s_handContour = imageContours; 

      } //end of next contour bigger than s_handContour condition 

      else 
      { //start of error condition 

       cout<<"Error"; 

      } //end of error condition 

     } //end of next contour equal to f_handContour condition 

     else 
     { //start of error condition 

      cout<<"Error"; 

     } //end of error condition 

    } //end of biggest/second biggest contour recognition loop 

    cvDrawContours (output, f_handContour, cvScalar (0,255,0), cvScalar (0,255,255), 1, 3,8, cvPoint (0,0)); //draws the first hand contour derived from binMask on ouput 
    cvDrawContours (output, s_handContour, cvScalar (0,255,0), cvScalar (0,255,255), 1, 3,8, cvPoint (0,0)); //draws the second hand contour derived from binMask on ouput 

답변

1

CV_RETR_EXTERNAL은 극단 외곽선 만 검색합니다. 알고리즘에 CV_RETR_LIST를 사용해야합니다. cvDrawContours 함수의 max_level로 값 0을 사용하십시오. 값 1은 현재 윤곽선과 동일한 레벨에있는 다른 모든 윤곽선을 그립니다. 또한 조건을 많이 최적화 할 수 있습니다 (엉망입니다). 그리고 cvContourArea를 너무 많이 호출하지 않도록 일부 지역 변수에 영역을 저장하십시오.

+0

완벽하게 작동 해 주셔서 감사합니다. :) – fdh

관련 문제