- OpenCV 教程
- OpenCV - 首頁
- OpenCV - 概述
- OpenCV - 環境配置
- OpenCV - 影像儲存
- OpenCV - 讀取影像
- OpenCV - 寫入影像
- OpenCV - 圖形使用者介面 (GUI)
- 繪圖函式
- OpenCV - 繪製圓形
- OpenCV - 繪製直線
- OpenCV - 繪製矩形
- OpenCV - 繪製橢圓
- OpenCV - 繪製多邊形
- OpenCV - 繪製凸多邊形
- OpenCV - 繪製帶箭頭的直線
- OpenCV - 新增文字
- 濾波
- OpenCV - 雙邊濾波
- OpenCV - 方框濾波
- OpenCV - 平方盒濾波
- OpenCV - Filter2D
- OpenCV - 膨脹
- OpenCV - 腐蝕
- OpenCV - 形態學操作
- OpenCV - 影像金字塔
- Sobel 導數
- OpenCV - Sobel 運算元
- OpenCV - Scharr 運算元
- 攝像頭和人臉檢測
- OpenCV - 使用攝像頭
- OpenCV - 圖片中的人臉檢測
- 使用攝像頭進行人臉檢測
- OpenCV 有用資源
- OpenCV - 快速指南
- OpenCV - 有用資源
- OpenCV - 討論
OpenCV - 霍夫線變換
您可以透過使用Imgproc類的HoughLines()方法應用霍夫變換技術來檢測給定影像的形狀。以下是此方法的語法。
HoughLines(image, lines, rho, theta, threshold)
此方法接受以下引數:
image − 表示源(輸入)影像的Mat類物件。
lines − 儲存儲存線的引數 (r, Φ) 的向量的Mat類物件。
rho − 表示引數 r 解析度(單位畫素)的雙精度型變數。
theta − 表示引數 Φ 解析度(單位弧度)的雙精度型變數。
threshold − 表示檢測一條線所需的最小交叉點數量的整型變數。
示例
下面的程式演示瞭如何在給定影像中檢測霍夫線。
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 = "E:/OpenCV/chap21/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("E:/OpenCV/chap21/hough_output.jpg", cannyColor);
System.out.println("Image Processed");
}
}
假設上面程式中指定的輸入影像是hough_input.jpg。
輸出
執行程式後,您將獲得以下輸出:
143 1 Image Processed
如果您開啟指定的路徑,您可以觀察到輸出影像如下:
廣告