C# opencvsharp 多点直线拟合
时间: 2023-10-05 09:11:02 浏览: 160
C# OpenCvSharp 通道分离.rar
5星 · 资源好评率100%
在C#中使用OpencvSharp进行多点直线拟合,可以通过以下步骤实现:
1. 加载图片并转为灰度图:
```csharp
Mat srcImg = new Mat("img.jpg", ImreadModes.Grayscale);
```
2. 进行边缘检测:
```csharp
Mat edgeImg = new Mat();
Cv2.Canny(srcImg, edgeImg, 50, 200, 3);
```
3. 进行霍夫直线变换:
```csharp
LineSegmentPoint[] lines = Cv2.HoughLinesP(edgeImg, 1, Math.PI / 180, 50, 30, 10);
```
4. 绘制直线:
```csharp
foreach (LineSegmentPoint line in lines)
{
Cv2.Line(srcImg, line.P1, line.P2, Scalar.Red, 2);
}
```
完整代码如下:
```csharp
Mat srcImg = new Mat("img.jpg", ImreadModes.Grayscale);
Mat edgeImg = new Mat();
Cv2.Canny(srcImg, edgeImg, 50, 200, 3);
LineSegmentPoint[] lines = Cv2.HoughLinesP(edgeImg, 1, Math.PI / 180, 50, 30, 10);
foreach (LineSegmentPoint line in lines)
{
Cv2.Line(srcImg, line.P1, line.P2, Scalar.Red, 2);
}
Cv2.ImShow("result", srcImg);
Cv2.WaitKey();
```
阅读全文