2015-09-15 153 views
-1

我是Open-CV中的新手。我試圖檢測相當簡單的圖像中的90度拐角。我需要檢測圍繞對象的矩形拐角。我正在使用shi-Thomasi功能。以下是我的代碼:拐角檢測

for x in range(0, 50): 
    ret, frame = cap.read() 

# make image gray scale 
im = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 

#finding corners 
corners = cv2.goodFeaturesToTrack(im, 1, 0.01, 10) 
corners = np.int0(corners) 
for i in corners: 
    x, y = i.ravel() 
    cv2.circle(frame, (x, y), 3, 255,-1) 
cv2.imwrite("DetectedCorners.png", frame) 

問題:始終檢測到該對象中的某些角落。我需要一個方法,完全刪除該對象,然後檢測角落。 enter image description here

我不知道如何刪除該對象。 有什麼建議嗎?照片顯示了我的結果。有時檢測到周圍矩形的某些角落,有時是該複雜對象中的某些隨機點。 我在檢測拐角之前也使用了Canny,但結果卻差10倍。

+0

你不能檢測到清晰可見的黑線(例如,使用霍夫),然後計算該角,因爲這些線的交叉點? – Miki

+0

我試着用霍夫我,我無法讓它工作準確。大量的冗餘線路被檢測到。再加上一些線條不完整,它們沒有相互交叉。也許是因爲我在Open-CV中是全新的。我非常感謝您是否幫助我。 – Danial

回答

4

那麼,快速和骯髒的C++解決方案,只是爲了證明有關使用霍夫變換來檢測線條,然後計算它們的相交點。

如果需要,您最終可以將代碼移植到Python。

#include <opencv2\opencv.hpp> 
#include <iostream> 
using namespace cv; 
using namespace std; 

int main() 
{ 
    Mat3b img = imread("path_to_image"); 

    // Convert to grayscale 
    Mat1b gray; 
    cvtColor(img, gray, COLOR_BGR2GRAY); 

    // Compute edges 
    Mat1b edges; 
    Canny(gray, edges, 400, 100); 

    // Create the output result image 
    Mat3b res; 
    cvtColor(edges, res, COLOR_GRAY2BGR); 

    // Call hough 
    vector<Vec2f> lines; 
    HoughLines(edges, lines, 1, CV_PI/180, 200, 0, 0); 

    vector<pair<Point,Point>> pts; 
    vector<Point> intersections; 

    for (size_t i = 0; i < lines.size(); i++) 
    { 
     float rho = lines[i][0], theta = lines[i][1]; 

     // Get 2 points on each line 
     Point pt1, pt2; 
     double a = cos(theta), b = sin(theta); 
     double x0 = a*rho, y0 = b*rho; 

     pt1.x = cvRound(x0 + 1000 * (-b)); 
     pt1.y = cvRound(y0 + 1000 * (a)); 
     pt2.x = cvRound(x0 - 1000 * (-b)); 
     pt2.y = cvRound(y0 - 1000 * (a)); 

     // Save the pair of points 
     pts.push_back(make_pair(pt1, pt2)); 

     // Draw lines 
     line(res, pt1, pt2, Scalar(0, 0, 255), 3, CV_AA); 
    } 

    // for each couple of lines 
    for (int i = 0; i < pts.size() - 1; ++i) 
    { 
     // get the two points of the first line 
     const Point& p1 = pts[i].first; 
     const Point& p2 = pts[i].second; 

     for (int j = i + 1; j < pts.size(); ++j) 
     { 
      // Get the two points of the second line 
      const Point& p3 = pts[j].first; 
      const Point& p4 = pts[j].second; 

      // Compute intersection 

      Point p; 
      float den = (p1.x - p2.x) * (p3.y - p4.y) - (p1.y - p2.y) * (p3.x - p4.x); 
      if (den != 0) // if not parallel lines 
      { 
       p.x = ((p1.x*p2.y - p1.y*p2.x)*(p3.x - p4.x) - (p1.x - p2.x)*(p3.x*p4.y - p3.y*p4.x))/den; 
       p.y = ((p1.x*p2.y - p1.y*p2.x)*(p3.y - p4.y) - (p1.y - p2.y)*(p3.x*p4.y - p3.y*p4.x))/den; 

       // Draw intersection 
       circle(res, p, 7, Scalar(0, 255, 0), 2); 
      } 

      // Save intersections 
      intersections.push_back(p); 
     } 
    } 

    return 0; 
} 

結果:

enter image description here