2013-11-14 27 views
1

工作在平方檢測。問題出在輻射的地板上。因爲你可以看到圖片。 解決這個問題的任何想法?opencv平方檢測輻射樓板問題

謝謝。

源圖像:

輸出:

源代碼:

空隙邊緣檢測:: find_squares(常量CV ::墊&圖像, 矢量> &正方形, cv :: Mat & outputFrame){

unsigned long imageSize = (long) (image.rows * image.cols)/1000; 
if (imageSize > 1200) RESIZE = 9; 
else if (imageSize > 600) RESIZE = 5; 
else if (imageSize > 300) RESIZE = 3; 
else RESIZE = 1; 


Mat src(Size(image.cols/RESIZE, image.rows/RESIZE),CV_YUV420sp2BGR); 
// Resize src to img size 
resize(image, src, src.size() ,0.5, 0.5, INTER_LINEAR); 

Mat imgeorj = image; const int N = 10; // 11; Mat pyr,timg,gray0(src.size(),CV_8U),灰色;

// down-scale and upscale the image to filter out the noise 
pyrDown(src, pyr, Size(src.cols/2, src.rows/2)); 
pyrUp(pyr, timg, src.size()); 

的#ifdef所迷離

Mat blurred(src); 
medianBlur(src, blurred, 15); 

#ENDIF

vector<vector<Point> > contours; 

// find squares in every color plane of the image 
for (int c = 0; c < 3; ++c) { 
    int ch[] = {c, 0}; 
    mixChannels(&timg, 1, &gray0, 1, ch, 1); 

    // try several threshold levels 
    for (int l = 0; l < N; ++l) { 
     // hack: use Canny instead of zero threshold level. 
     // Canny helps to catch squares with gradient shading 
     if (l == 0) { 
      // apply Canny. Take the upper threshold from slider 
      // and set the lower to 0 (which forces edges merging) 
      // Canny(gray0, gray, 0, thresh, 5); 
      // Canny(gray0, gray, (10+l), (10+l)*3, 3); 
      Canny(gray0, gray,50, 200, 3); 
      // dilate canny output to remove potential 
      // holes between edge segments 
      dilate(gray, gray, Mat(), Point(-1, -1)); 
      //erode(gray, gray, Mat(), Point(-1, -1), 1); 
     } else { 
      // apply threshold if l!=0: 
      // tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0 
      gray = gray0 >= (l + 1) * 255/N; 
     } 


     // find contours and store them all as a list 
     findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); 

     vector<Point> approx; 

     // test each contour 
     for (size_t i = 0; i < contours.size(); ++i) { 
      // approximate contour with accuracy proportional 
      // to the contour perimeter 

      approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true) * 0.02, true); 


      if (approx.size() == 4 && 
       fabs(contourArea(Mat(approx))) > 5000 && 
       isContourConvex(Mat(approx))) { 
       float maxCosine = 0; 

       for (register int j = 2; j < 5; ++j) { 
        // find the maximum cosine of the angle between joint edges 
        float cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1])); 
        maxCosine = MAX(maxCosine, cosine); 
       } 

       // if cosines of all angles are small 
       // (all angles are ~90 degree) then write quandrange 
       // vertices to resultant sequence 
       if (maxCosine < 0.3) { 
        squares.push_back(approx); 
       } 

      } 
     } 

    } 
} 

debugSquares(squares, imgeorj,outputFrame); 
} 
+0

[OpenCV C++/Obj-C:檢測紙張/方形檢測]的可能副本(http://stackoverflow.com/questions/8667818/opencv-c-obj-c-detecting-a-sheet-的紙平方檢測) – baci

回答

0

您可以嘗試使用Hough變換檢測直邊,並使用這些構建方。