OpenCV霍夫線變換

可以通過使用Imgproc類的HoughLines()方法應用霍夫變換技術來檢測給定圖像的形狀。以下是此方法的語法。

HoughLines(image, lines, rho, theta, threshold)

該方法接受以下參數 -

  • image - 表示此操作的源(輸入圖像)的Mat對象。
  • lines - Mat類的一個對象,用於存儲存儲線的參數(r,Φ)的向量。
  • rho - 類型爲double的變量,以像素爲單位表示參數r的分辨率。
  • theta - 類型爲double的變量,表示以弧度表示的參數Φ的分辨率。
  • threshold - 一個整數類型的變量,表示「檢測」一條直線的最小交點數。

示例

下面的程序演示如何檢測給定圖像中的霍夫線。

package com.yiibai.miscellaneous;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;

import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class HoughlinesTest {
   public static void main(String args[]) throws Exception {
      // Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

      // Reading the Image from the file and storing it in to a Matrix object
      String file = "F:/worksp/opencv/images/hough_input.jpg";

      // Reading the image
      Mat src = Imgcodecs.imread(file,0);

      // Detecting edges of it
      Mat canny = new Mat();
      Imgproc.Canny(src, canny, 50, 200, 3, false);

      // Changing the color of the canny
      Mat cannyColor = new Mat();
      Imgproc.cvtColor(canny, cannyColor, Imgproc.COLOR_GRAY2BGR);

      // Detecting the hough lines from (canny)
      Mat lines = new Mat();
      Imgproc.HoughLines(canny, lines, 1, Math.PI/180, 100);

      System.out.println(lines.rows());
      System.out.println(lines.cols());

      // Drawing lines on the image
      double[] data;
      double rho, theta;
      Point pt1 = new Point();
      Point pt2 = new Point();
      double a, b;
      double x0, y0;

      for (int i = 0; i < lines.cols(); i++) {
         data = lines.get(0, i);
         rho = data[0];
         theta = data[1];

         a = Math.cos(theta);
         b = Math.sin(theta);
         x0 = a*rho;
         y0 = b*rho;

         pt1.x = Math.round(x0 + 1000*(-b));
         pt1.y = Math.round(y0 + 1000*(a));
         pt2.x = Math.round(x0 - 1000*(-b));
         pt2.y = Math.round(y0 - 1000 *(a));
         Imgproc.line(cannyColor, pt1, pt2, new Scalar(0, 0, 255), 6);
      }
      // Writing the image
      Imgcodecs.imwrite("F:/worksp/opencv/images/hough_output.jpg", cannyColor);

      System.out.println("Image Processed");
   }
}

假定以下是上述程序中指定的輸入圖像:sample4.jpg
OpenCV霍夫線變換

執行上面示例代碼,得到以下結果 -
OpenCV霍夫線變換