1

我試圖提取矩形掃描圖像,像這樣:enter image description here
我使用的OpenCV及以下this tutorial,我來到這個代碼選擇了霍夫變換參數

private static Mat detectLinesHough(Mat img) { 
    Mat lines = new Mat(); 
    // Mat edges = new Mat(); 
    // Imgproc.Canny(img, edges, 20, 60); 
    int threshold = 80; 
    int minLineLength = 10; 
    int maxLineGap = 5; 
    double rho = 0.4; 
    Imgproc.HoughLinesP(img, lines, rho, Math.PI/180, threshold, minLineLength, maxLineGap); 
    Imgproc.cvtColor(img, img, Imgproc.COLOR_GRAY2RGB); 
    System.out.println(lines.cols()); 
    for (int x = 0; x < lines.cols(); x++) { 
     double[] vec = lines.get(0, x); 
     double x1 = vec[0], y1 = vec[1], x2 = vec[2], y2 = vec[3]; 
     Point start = new Point(x1, y1); 
     Point end = new Point(x2, y2); 
     Core.line(lines, start, end, new Scalar(0, 255, 0), 3); 
    } 
    return img; 
} 

例如,如上面的代碼中的參數,我得到以下結果: enter image description here
結果不令人滿意,所以問題是 - 我應該如何選擇參數來選擇所有矩形而不是許多字母?

回答

0

我有三個建議,以提高結果:

  1. 你應該讓「minLineLength」儘可能高和「maxLineGap」儘可能低,讓你搜索線仍然被找到。

  2. 處理線條並檢查角度,因爲您的矩形線與x軸平行或垂直。您可以放棄不在特定角度範圍內的線條。

  3. 尋找角落並丟棄沒有連接到角落的每條線。