2013-11-02 4 views
0

라이브 웹캠 피드에서 손으로 윤곽을 그리고 싶습니다. OpenCV는 다음 코드를 사용할 때 노이즈, 그림자 등으로 인해 다른 많은 윤곽선을 감지합니다.더 작은 영역을 제거하면서 윤곽 그리기

cv::findContours(fgmask,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE); 
    cv::drawContours(fgimg,contours,-1,cv::Scalar(0,0,255),2); 

작은 중요하지 않은 부분을 제거하기 위해 온라인에서 다음 코드를 발견했습니다.

cv::findContours(fgmask,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE); 
    for(int i=0;i<contours.size();i++) 
    { 

        if(contours[i].size() < 10000 && contours[i].size() > 0) 
      { 

      int size=cv::contourArea(contours[i]); 
      if(size>5000) 
      { 
          //Draw contour 
          vector<vector<Point> > tcontours; 
          tcontours.push_back(contours[i]); 
          cv::drawContours(fgimg,tcontours,-1,cv::Scalar(0,0,255),2); 
          } 
          } 
      } 

그러나 윤곽선이 전혀 그려지지 않습니다. (크기> 5000) 진술이 진실인지 알기 위해 진술서를 넣었고, 나는 Cout 출력을 얻었다. 그렇다면 윤곽선이없는 이유는 무엇입니까? push_back 함수에 문제가 있습니까?

답변

0
  1. CV_RETR_EXTERNAL을 정말로 사용 하시겠습니까?
  2. if(contours[i].size() < 10000 && contours[i].size() > 0) 여기에 쓸모가있는 이해가되지 않습니다 vector<vector<Point> > tcontours;
  3. 임시 벡터 중 하나

시도 : 나는 당신의 코드를 사용하는 경우

vector<vector<Point> > contours; 
vector<Vec4i> hierarchy; 
findContours(fgmask, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE); 

// draw contours: 
Mat newImg = Mat::zeros(fgmask.size(), CV_8UC3); 
for (unsigned int i = 0; i < contours.size(); i++) 
{ 
    if (contourArea(contours[i]) > 5000) 
     drawContours(newImg, contours, i, Scalar(0,0,255), 1, 8, hierarchy, 0); 
} 
+0

, 내가 가진 drawContours이 세번째 인수가 제공 될 때 처리되지 않은 예외. 오류는 다음과 같습니다 : 알 수없는 함수, 파일 contours.cpp, 라인 1810에서 어설 션 실패 (0 <= contourIdx && contourIdx <(int) last) – MollieVX

+0

@vatsalasharma :이 코드가 어떻게 되겠습니까? '0; contours.size()'간격. – LihO

관련 문제