- 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
如果您打开指定的路径,您可以观察到输出图像如下:
广告