C#OpenCV算子-轮廓
6.1 线拟合
函数原型:
Line2D FitLine(IEnumerable<Point> points, DistanceTypes distType, double param, double reps, double aeps) /* 参数: points:二维点的数组或vector. line:输出直线. distType:距离类型. param:距离参数. reps:径向的精度参数. aeps:角度精度参数. 功能: 根据输入的点拟合直线 */
例子:
static void Main(string[] args) { Mat src1 = new Mat(@"C:\Users\Administrator\Desktop\1.jpg"); List<Point> listPoint = new List<Point>(); listPoint.Add(new Point(50, 50)); listPoint.Add(new Point(150, 100)); Line2D line = Cv2.FitLine(listPoint, DistanceType.L1, 0, 1e-2, 1e-2); double k = line.Vy / line.Vx; Point p1 = new Point(0, k * (0, _ line.X1) + line.Y1); Point p2 = new Point(src1.Width, k * (src1.Width - line.X1) + line.Y1); Cv2.Line(src1, p1, p2, new Scalar(0, 0, 255)); Cv2.ImShow("test", dst); Cv2.WaitKey(); }
实验样例:
|
Src1 |
dst |
|
|
|
6.2 椭圆拟合
函数原型:
RotatedRect FitEllipse(IEnumerable<Point> points) /* 参数: points:二维点的数组或vector. 输出:椭圆的最小外接旋转矩形. 功能: 根据输入的点拟合一个椭圆 */
例子:
static void Main(string[] args) { Mat src1 = new Mat(@"C:\Users\Administrator\Desktop\1.jpg"); RotatedRect rotatedRect = new RotatedRect(); List<Point> listPoint = new List<Point>(); listPoint.Add(new Point(179, 79)); listPoint.Add(new Point(195, 115)); listPoint.Add(new Point(171, 151)); listPoint.Add(new Point(141, 155)); listPoint.Add(new Point(109, 99)); rotatedRect = Cv2.FitEllipse(listPoint); Cv2.Ellipse(src1, rotatedRect, new Scalar(0, 0, 255)); Cv2.ImShow("test", dst); Cv2.WaitKey(); }
实验样例:
|
Src1 |
dst |
|
|
|
/*-------------------------------------------------------------------------------------------------------
笔者说明:
该笔记来源于本人学习OpenCvSharp时的资料,
分享出来只是为了供大家学习,并且为了自己以后想要用的时候方便寻找。
时间:2023年3月14日
------------------------------------------------------------------------------------------------------------*/

OpenSharp算子-轮廓




浙公网安备 33010602011771号