几何尺寸与公差论坛

 找回密码
 注册
查看: 1085|回复: 2

visionpro中旋转标定怎么做?

  [复制链接]
发表于 2023-2-19 20:52:28 | 显示全部楼层 |阅读模式
一个点,正反转15度,3个点的拟合圆圆心
 楼主| 发表于 2023-2-19 20:56:27 | 显示全部楼层
在VisionPro中,可以使用CogCaliper找到圆周上的三个点,并使用CogCircleFit找到这三个点的拟合圆心。下面是一个简单的示例程序,用于查找一个点,将其旋转15度,并找到其圆周上的三个点及其拟合圆心。

scss
Copy code
// 声明变量
ICogImage sourceImg;
CogFindCircleTool findCircleTool;
CogCaliper caliper;
CogCircularArc circularArc;
CogFindCircleResult findResult;
CogCircle circle;

// 读入图像
sourceImg = CogImage8Grey.Load(@"C:\Temp\image.bmp", CogImageLoadModeConstants.Read);

// 初始化找圆工具
findCircleTool = new CogFindCircleTool();
findCircleTool.InputImage = sourceImg;
findCircleTool.RunParams.NumCalipers = 100;
findCircleTool.RunParams.CaliperSearchLength = 100;
findCircleTool.RunParams.CaliperProjectionLength = 10;
findCircleTool.RunParams.CaliperRunParams.Edge0Polarity = CogCaliperPolarityConstants.DarkToLight;
findCircleTool.RunParams.CaliperRunParams.EdgeMode = CogCaliperEdgeModeConstants.SingleEdge;
findCircleTool.RunParams.CaliperRunParams.FilterHalfSizeInPixels = 2;
findCircleTool.RunParams.CaliperRunParams.ContrastThreshold = 10;

// 找圆
findCircleTool.Run();
findResult = findCircleTool.CreateLastRunResults();

// 初始化刻度尺
caliper = new CogCaliper();
caliper.Region = findResult.CircularArc;
caliper.RanParams.Edge0Polarity = CogCaliperPolarityConstants.DarkToLight;
caliper.RanParams.EdgeMode = CogCaliperEdgeModeConstants.SingleEdge;
caliper.RanParams.NumToIgnore = 0;
caliper.RanParams.FilterHalfSizeInPixels = 2;
caliper.RanParams.ContrastThreshold = 20;

// 找圆周上的三个点
circularArc = findResult.CircularArc;
circle = new CogCircle(circularArc.CenterX, circularArc.CenterY, circularArc.Radius);
caliper.Region = circle;
caliper.Orientation = circularArc.AngleStart + Math.PI / 12;
caliper.RanParams.Edge0Position = 0.1 * circle.Radius;
caliper.RanParams.Edge1Position = 0.3 * circle.Radius;
caliper.Run();
double x1 = caliper.Edge0Position.X;
double y1 = caliper.Edge0Position.Y;
caliper.RanParams.Edge0Position = 0.4 * circle.Radius;
caliper.RanParams.Edge1Position = 0.6 * circle.Radius;
caliper.Run();
double x2 = caliper.Edge0Position.X;
double y2 = caliper.Edge0Position.Y;
caliper.RanParams.Edge0Position = 0.7 * circle.Radius;
caliper.RanParams.Edge1Position = 0.9 * circle.Radius;
caliper.Run();
double x3 = caliper.Edge0Position.X;
double y3 = caliper.Edge0Position.Y;

// 拟合圆
circle = new CogCircle(x1, y1, x2, y2, x3, y3);

// 输出结果
System.Console.WriteLine("Center: ({0}, {1})", circle.CenterX, circle.CenterY
 楼主| 发表于 2023-2-19 21:10:36 | 显示全部楼层
// 读取图像
CogImage8Grey image = new CogImage8Grey();
image.Load("path/to/image.bmp");

// 创建拟合圆的工具
CogFitCircleTool fitCircle = new CogFitCircleTool();
fitCircle.InputImage = image;
fitCircle.Run();

// 获取拟合圆的圆心坐标
double centerX = fitCircle.Results.GetCircle().CenterX;
double centerY = fitCircle.Results.GetCircle().CenterY;

// 创建用于显示图形的容器
CogCompositeShape compositeShape = new CogCompositeShape();

// 创建圆心的十字线
CogLineSegment line1 = new CogLineSegment(centerX - 20, centerY, centerX + 20, centerY);
CogLineSegment line2 = new CogLineSegment(centerX, centerY - 20, centerX, centerY + 20);
line1.Color = CogColorConstants.Red;
line2.Color = CogColorConstants.Red;

// 将十字线添加到组合图形中
compositeShape.Add(line1, "");
compositeShape.Add(line2, "");

// 旋转点
double rotationAngle = 15; // 旋转角度
double pointX = centerX + 30; // 原始点的坐标
double pointY = centerY;
double radian = rotationAngle * Math.PI / 180; // 角度转弧度
double rotatedPointX = centerX + (pointX - centerX) * Math.Cos(radian) - (pointY - centerY) * Math.Sin(radian); // 旋转后点的X坐标
double rotatedPointY = centerY + (pointX - centerX) * Math.Sin(radian) + (pointY - centerY) * Math.Cos(radian); // 旋转后点的Y坐标

// 创建点的标记
CogGraphicLabel pointLabel = new CogGraphicLabel();
pointLabel.Color = CogColorConstants.Green;
pointLabel.Font = new Font("Arial", 10);
pointLabel.Text = "●";
pointLabel.SetXYText(rotatedPointX, rotatedPointY);

// 将标记添加到组合图形中
compositeShape.Add(pointLabel, "");

// 在图像上显示组合图形
CogDisplay cogDisplay = new CogDisplay();
cogDisplay.Image = image;
cogDisplay.StaticGraphics.Add(compositeShape, "");

// 显示图像
cogDisplay.Fit(true);
以上代码中,首先使用CogFitCircleTool类拟合一个圆,并获取拟合圆的圆心坐标。然后创建一个CogCompositeShape实例作为容器,将需要显示在图像上的图形(包括圆心的十字线和旋转后的点)添加到组合图形中。最后,在CogDisplay控件上显示图像和组合图形。

注意,CogCompositeShape的Add()方法的第二个参数是图形的名字,可以为空字符串。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-12-31 01:14 , Processed in 0.040785 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表