2013-03-26 1 views
0

나는 OpenCV 프로젝트를 개발 중이다.C++ OpenCV 더 작은 윤곽을 없앤다

저는 현재 특정 ROI (Regoin Of Interest) 윤곽선을 감지하고 있습니다. 내가 원하는 것은 작은 모든 윤곽선을 제거하는 것입니다. 다시 말해,이 작은 윤곽선이 전혀 빠져 나오기를 원하지 않습니다.

지금까지 나는이 일을하기 위해이 알고리즘을 코딩 한 경우 :

CODE :

vector<vector<Point> > contours; 
    vector<Vec4i> hierarchy; 
    findContours(mBlur, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 
    //-----------------------------------------------------------------------------> 
    //Contours Vectors 
    vector<vector<Point> > contours_poly(contours.size()); 
    vector<Rect> boundRect (contours.size()); 
    vector<Point2f> ContArea(contours.size()); 
    Mat drawing = Mat::zeros(range_out.size(), CV_8UC3); 
    //-----------------------------------------------------------------------------> 
    //Detecting Contours 
    for(int i = 0; i < contours.size(); i++) 
    { 

     ContArea.clear(); 
     ContArea.push_back(Point2f(boundRect[i].x, boundRect[i].y)); 
     ContArea.push_back(Point2f(boundRect[i].x + boundRect[i].width, boundRect[i].y)); 
     ContArea.push_back(Point2f(boundRect[i].x + boundRect[i].width, boundRect[i].y + boundRect[i].height)); 
     ContArea.push_back(Point2f(boundRect[i].x, boundRect[i].y + boundRect[i].height)); 

     double area = contourArea(ContArea); 

     if(area > 2000) 
     { 
      approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true); 
      boundRect[i] = boundingRect(Mat(contours_poly[i])); 


      cout<<"The area of Contour: "<<i<< " is: " <<area<<endl; 

     } 
    } 



    /// Draw polygonal contour + bonding rects 


    ////////////////////////////////////////////////////////////////////////////////// 

    for(int i = 0; i< contours.size(); i++) 
    { 

     Scalar color = Scalar(255,255,255); 
     drawContours(drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point()); 
     fillPoly(drawing, contours, Scalar(255,0,0)); 

    } 

여기서 문제는 경우 (영역> 2000) 문조차 실행되지 것처럼 보이는 것입니다 반죽 나는 이미지의 일부 영역이 이것보다 훨씬 큽니다.

나는 많은 다른 해결책을 시도해 왔지만 이것이 나에게 가장 적합한 것으로 보인다.

주요 국가 질문 :

가 .... 내가 주어진 코드로 원하는 것을 달성 할 수 있습니까? 그렇다면 누구나 내가이 문제로 어디로 가고 있는지 알 수 있습니다. Else 누군가가 해결책이나 좋은 온라인 자료를 제안 할 수 있습니까? 당신은 로아의 크기에 따라 제거 할 경우, 다음과 같이 할 수

+0

Ok 약간의 업데이트 if 문을 제거했는데 감지 된 각 윤곽선이 영역 0이므로 해당 영역 계산과 관련이 있어야합니다. – Tomazi

+0

'boundRect'가 비어 있습니다. 당신은'contours.size()'를 사용하여 용량을 설정하기 때문에, 항상 빈 사각형 인 것으로 생각됩니다. 한 가지 더, 작은 윤곽선이나 작은 ROI를 제거 하시겠습니까? – Safir

답변

1

이 (bounding box of opencv의 예에 따라) :

vector< vector<Point> > contours_poly(contours.size()); 
vector<Rect> boundRect (contours.size()); 
vector<Point2f> centeres (contours.size()); 
vector<float> radiuses (contours.size()); 

// finding the approximate rectangle and circle of contours 
for(int i = 0; i < contours.size(); i++) 
    { 
    approxPolyDP(Mat (contours[i]), contours_poly[i], 3, true); 
    boundRect[i] = boundingRect(Mat (contours_poly[i])); 
    minEnclosingCircle((Mat) contours_poly[i], centeres[i], radiuses[i]); 
    } 

위의 코드는 당신이 윤곽의 대략적인 사각형을 제공합니다 (boundRect) 및 근사 원 (centers, radiuses)이 포함됩니다. 이 후에는 contourArea();으로 전화 할 수 있어야하며 특정 임계 값보다 작 으면이를 제거 할 수 있습니다. 당신은 단지 특정 길이의 작은 윤곽을 제거하려면


, 당신은 길이 look at the answer to similar question이다 계산할 수 있습니다, 그리고 당신이 arcLength() 기능을 사용할 수 있습니다 보인다. 나는 다음과 같이 생각한다 : double perimeter = arcLength (Mat (contours[i]), true);.

관련 문제