2016-09-16 10 views
1

몇 개의 꼭지점이 있는지 찾아서 꼭지점 수로 모양을 결정하고 싶습니다. 지금까지 'HoughCircle'을 사용하여 원이 있는지 여부를 감지 할 수 있었지만 직사각형, 정사각형 또는 삼각형을 처리 할 수 ​​없습니다.웹캠에서 도형을 감지 할 수 없습니다

여기 코드는 있지만 제대로 처리 할 수 ​​없습니다.

// declerations 
vector<vector<Point>> contoursPoly(contours.size()); 
vector<Rect> boundRect(contours.size()); 
vector<vector<Point>> contours; 
vector<Vec4i> hierarchy; 
Mat contourimg; 
Mat frame, out; 

// for webcam 
VideoCapture vcap(0); 

for (;;){ 

    vcap.read(frame); // display 
    cvtColor(frame, out, CV_BGR2GRAY); // convert BGR to GRAY scale 

    // fix noises 
    GaussianBlur(out, out, Size(9, 9), 2); 
    threshold(out,out,200,255,CV_THRESH_BINARY_INV | CV_THRESH_OTSU); 

    findContours(out, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); // find contours 

    for (int i = 0; i < contours.size(); i++) 
    { 
     approxPolyDP(Mat(contours[i]), contoursPoly[i], 3, true); // find the number of vertecies 

     if (contoursPoly.size() == 4) // it should be a quadrilateral 
     { 
      boundRect[i] = boundingRect(contoursPoly[i]); 
      drawContours(frame, contours, i, Scalar(0, 0, 255), CV_FILLED, 8, hierarchy, 0, Point()); 
     } 

     if (contoursPoly.size() == 3) // it should be triange 
     { 
      drawContours(frame, contours, i, Scalar(0, 0, 255), CV_FILLED, 8, hierarchy, 0, Point()); 
     } 

    imshow("test", frame); 
    if (waitKey(30) == 27) break; 

EDIT : 일부 변경 한

..

  1. 벡터 contoursPoly (contours.size());
  2. approxPolyDP (매트 (윤곽선 [i]), contours 폴리, arcLength (매트 (윤곽선 [i]) * 0.01, 참));

여기가

enter image description here

.. 2 편집 프로그램에서 이미지입니다 : 매트 contourOutput : 코멘트에

덕분에, 나는 내 코드에이 부분을 추가 한

= threshimg.clone();

나는 .. 최근

enter image description here

를이있어하지만 문제는 여전히 존재한다. 그것은 윤곽선을 그리지 않습니다, 왜 모양을 감지 할 수없는 것입니다.

편집 3 : 나는 또한 하프 라인에서 내가 원하는 일을하고 여기에 결과가 있습니다

int main(){ 

    VideoCapture vcap(0); 
    Mat gray, frame, mask, out, trackbarimg; 

    vector<vector<Point>> contours; 
    vector<Point> contPoly; 
    vector<Vec4i> hierarchy; 
    RotatedRect rrect; 
    Point2f vertices[4]; 

    for (;;){ 

     vcap.read(frame); 
     cvtColor(frame, gray, CV_BGR2GRAY); 
     GaussianBlur(gray,gray,Size(9,9), 1); 
     threshold(gray, mask, 220, 255, CV_THRESH_BINARY); 

     Mat contoursOut = mask.clone(); 
     findContours(contoursOut, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); 

     out = Mat::zeros(contoursOut.size(), CV_8UC3); 

     for (int i = 0; i < contours.size(); i++){ 
      drawContours(out, contours, i, Scalar(0, 255, 0), CV_FILLED, 8, hierarchy, 1, Point()); 
      rrect = minAreaRect(contours[i]); 

      rrect.points(vertices); 
      line(out, vertices[0], vertices[1], cv::Scalar(0, 0, 255)); 
      line(out, vertices[1], vertices[2], cv::Scalar(0, 0, 255)); 
      line(out, vertices[2], vertices[3], cv::Scalar(0, 0, 255)); 
      line(out, vertices[3], vertices[0], cv::Scalar(0, 0, 255)); 
     } 

     imshow("THRESHOLD", mask); 
     imshow("BLUR", gray); 
     imshow("ORIGINAL", frame); 
     imshow("LINES", out); 

     if (waitKey(30) == 27) break; 

    } 
    return 0; 
} 

어떤 비논리적 일이나 있습니까 : enter image description here

코드입니다 두 번째 코드의 실수? 실제로, approxPolyDP를 사용하여 내 목적을 달성하고 거기에있는 정점의 수를 결정하고 싶습니다.

+0

'approxPolyDP (Mat (contours [i]), contoursPoly [i], 0.01 * arcLength (Matte (contours [i]), true), true); – PSchn

+0

답장을 보내 주셔서 감사 합니다만 작동하지 않았습니다. – PIC16F84A

+0

그런 다음 몇 가지 샘플 이미지를 제공해주십시오. RHE 소스, 임계 후 이진이나 게 검출 된 윤곽 – PSchn

답변

0

윤곽선으로 도형을 찾는 것은 무언가에 의해 손상되지 않는 윤곽선을 사용합니다 (예에서 엄지 손가락처럼).

더 강력한 대안은 선 (또는 선분)을 찾고 선이 교차하여 사각형/직사각형을 형성하는지 확인하기 위해 허프 (hough) 변환을 사용하는 것입니다.

등고선을 사용하려면 루프에서 approxPolyDP를 실행하여 4 포인트로 윤곽을 얻을 때까지 근사 요소를 늘리거나 실패합니다.

+0

EDIT 3을 확인할 수 있습니까? – PIC16F84A

0

나는 잘 모르겠지만, 내 생각에는 임계 값을 후 (그리고 findContour는()하기 전에), 당신은 검소한 에지 검출기 사용 예를 들어, 에지 검출을 수행해야합니다

void cv::Canny (InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize = 3, bool L2gradient = false) 

canny 문서는 here입니다.

+0

여전히 윤곽선을 그릴 수 없습니다. – PIC16F84A

+0

내가 루프에 대한 생각, 당신은) (contoursPoly [I] 크기는()하지 contoursPoly.size에 대한 테스트해야합니까? – centralcmd