2013-01-17 22 views
1

我正在處理我的項目,並且遇到問題是我不知道如何僅檢測方形區域的關鍵點之前已經被檢測到。下面是我的演示和到目前爲止,我的代碼將檢測到的外部和內部的方形要點:https://www.youtube.com/watch?feature=player_embedded&v=3U8V6PhMnZ8opencv/C++我如何只檢測在之前檢測到的方形區域內的關鍵點

這是我的代碼找到方:

 const int threshold_level = 2; 
    for (int l = 0; l < threshold_level; l++) 
    { 

     gray = gray0 >= (l+1) * 255/threshold_level; 

     // Find contours and store them in a list 
     findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); 

     // Test contours 
     vector<Point> approx; 
     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); 

       // Note: absolute value of an area is used because 
       // area may be positive or negative - in accordance with the 
       // contour orientation 
       if (approx.size() == 4 && 
         fabs(contourArea(Mat(approx))) > 3000 && 
         isContourConvex(Mat(approx))) 
       { 
         double maxCosine = 0; 

         for (int j = 2; j < 5; j++) 
         { 
           double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1])); 
           maxCosine = MAX(maxCosine, cosine); 
         } 

         if (maxCosine < 0.3) 
           squares.push_back(approx); 
       } 
     } 
    } 

這是我的代碼繪製正方形和角點:

const Point* p = &squares[i][0]; 
     int n = (int)squares[i].size();  
     Point p1 = squares[i][0]; 
     Point p2 = squares[i][1]; 
     Point p3 = squares[i][2]; 
     Point p4 = squares[i][3]; 
     cout<<"p1 is "<<p1<<" p2 is "<<p2<<" p3 is "<<p3<<" p4 is "<<p4<<endl; 
     circle(image, squares[i][0], 3, Scalar(0,0,255), 5, 8, 0); 
     circle(image, squares[i][1], 3, Scalar(0,255,255), 5, 8, 0); 
     circle(image, squares[i][2], 3, Scalar(255,0,255), 5, 8, 0); 
     circle(image, squares[i][3], 3, Scalar(255,255,0), 5, 8, 0); 

     polylines(image, &p, &n, 1, true, Scalar(0,255,0), 3, CV_AA); 

這是我的代碼來檢測關鍵點:

Mat gray_image; 
    vector<KeyPoint> keyPoints; 
    cvtColor(image, gray_image, CV_BGR2GRAY); 
    FastFeatureDetector fast(60); 
    fast.detect(gray_image,keyPoints); 
    drawKeypoints(image, keyPoints,image, Scalar::all(255), DrawMatchesFlags::DRAW_OVER_OUTIMG); 

回答

1

您可以剪裁使用圖像

矩形R(左,上,寬度,高度); //我們感興趣的部分圖片

Mat roi(fullImage,r); //將創建對原始圖像的矩形r的引用。請注意,它不是副本。

1

你有兩種可能的解決方案:

  1. 檢測所有的關鍵點,然後檢查它們是否在廣場內。
  2. 從圖像中裁剪出正方形以生成新圖像,然後在那裏檢測關鍵點。

乾杯,