OpenCvSharp 是一个 .NET 包装库,允许开发者在 C# 中使用 OpenCV 的功能。霍夫变换是 OpenCV 中一种强大且常用的图像处理方法,用于检测几何形状。本文将介绍霍夫变换的特点、应用场景,并通过示例代码展示如何在 C# 中使用 OpenCvSharp 实现霍夫变换。
霍夫变换是一种特征提取技术,主要用于识别图像中的直线、圆以及其他简单的几何形状。其特点包括:
霍夫变换在众多领域都能派上用场,包括但不限于:
以下是使用 OpenCvSharp 在一幅图像中进行霍夫直线检测的示例代码。
C#static void Main()
{
// 加载灰度图像
using var src = Cv2.ImRead("example.png", ImreadModes.Grayscale);
// 边缘检测(使用Canny检测器)
using var cannyOutput = new Mat();
Cv2.Canny(src, cannyOutput, 50, 150);
// 霍夫线变换
LineSegmentPolar[] lines = Cv2.HoughLines(cannyOutput, 1, Math.PI / 180, 100);
// 显示检测到的线条
using var colorDst = new Mat();
Cv2.CvtColor(cannyOutput, colorDst, ColorConversionCodes.GRAY2BGR);
double thetaThreshold = 1 * Math.PI / 180; // 1 degree tolerance for near horizontal
List<LineSegmentPolar> filteredLines = new List<LineSegmentPolar>();
foreach (var line in lines)
{
double thetaInDegrees = line.Theta * 180 / Math.PI;
if (thetaInDegrees >= 90 - thetaThreshold && thetaInDegrees <= 90 + thetaThreshold)
{
// 将每条线与已经选出的线对比,避免选择非常相似的线条
bool isSimilar = false;
foreach (var existingLine in filteredLines)
{
if (Math.Abs(line.Rho - existingLine.Rho) < 10 && Math.Abs(line.Theta - existingLine.Theta) < thetaThreshold)
{
isSimilar = true;
break;
}
}
// 如果找不到相似的线条,则添加到结果集中
if (!isSimilar)
{
filteredLines.Add(line);
}
}
}
// 绘制过滤后线条
foreach (var line in filteredLines)
{
double rho = line.Rho;
double theta = line.Theta;
double a = Math.Cos(theta);
double b = Math.Sin(theta);
double x0 = a * rho;
double y0 = b * rho;
Point pt1 = new Point(x0 + 1000 * (-b), y0 + 1000 * (a));
Point pt2 = new Point(x0 - 1000 * (-b), y0 - 1000 * (a));
// 在图像上绘制线
Cv2.Line(colorDst, pt1, pt2, Scalar.Red, 3, LineTypes.AntiAlias);
// 在图像上标记该线的水平值(Theta)
string label = $"Theta: {theta * 180 / Math.PI:F2} deg";
Point textPosition = new Point(x0, y0);
Cv2.PutText(colorDst, label, textPosition, HersheyFonts.HersheySimplex, 0.5, Scalar.Blue, 1, LineTypes.AntiAlias);
}
// 显示结果
Cv2.ImShow("Detected Lines with Angles", colorDst);
Cv2.WaitKey();
}
filteredLines
列表来保存不重复的线。在每次添加新线之前,检查它是否与已选择的线条过于相似(基于 rho
和 theta
参数的差异)。thetaThreshold
),从而减少引入不接近水平的线条。rho
位置偏移小于一定值的线视为重复,提高精度调整的自由度。以下示例展示了如何检测图像中的圆。
C#using OpenCvSharp;
class HoughCircleTransformExample
{
static void Main()
{
// 加载灰度图像
using var src = Cv2.ImRead("input.jpg", ImreadModes.Grayscale);
// 降噪
using var blurred = new Mat();
Cv2.MedianBlur(src, blurred, 5);
// 霍夫圆变换
CircleSegment[] circles = Cv2.HoughCircles(
blurred,
HoughMethods.Gradient,
1,
src.Rows / 8,
100,
30,
1,
30);
// 显示检测到的圆
using var colorSrc = new Mat();
Cv2.CvtColor(src, colorSrc, ColorConversionCodes.GRAY2BGR);
foreach (var circle in circles)
{
Cv2.Circle(colorSrc, (int)circle.Center.X, (int)circle.Center.Y, (int)circle.Radius, Scalar.Green, 2);
Cv2.Circle(colorSrc, (int)circle.Center.X, (int)circle.Center.Y, 2, Scalar.Red, 3);
}
// 显示结果
Cv2.ImShow("Detected Circles", colorSrc);
Cv2.WaitKey();
}
}
霍夫变换是图像处理中不可或缺的工具,尤其适用于识别图像中的简单几何形状。在 OpenCvSharp 中,我们可以方便地使用霍夫变换进行线条和圆的检测。通过这些特点和应用场景,开发者可以开发出能够自动识别和分析复杂场景的强大程序。这个工具在图像处理、计算机视觉的项目中,将为您带来无限的便捷和可能性。
本文作者:技术老小子
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!