测量软件之计算四条边的长度
一 测量算法修改
/// <summary>
/// 测量矩形四条边
/// </summary>
/// <param name="ho_Image"></param>
/// <param name="hv_Row_Target"></param>
/// <param name="hv_Column_Target"></param>
/// <param name="hv_Phi_Target"></param>
/// <param name="hv_Angle_Target"></param>
public void MeasureRectangleTool(HObject ho_Image, out HTuple UpDistance, out HTuple LeftDistance,
out HTuple DownDistance, out HTuple RightDistance)
{
// 获取搜索区域图像
HOperatorSet.ReduceDomain(ho_Image, GlobalParamters.SearchRegion, out HObject SearchImage);
// 模板匹配
HOperatorSet.FindNccModel(SearchImage, GlobalParamters.ModelId,
GlobalParamters.FitRectModel.angleStart_F,
GlobalParamters.FitRectModel.angleExtent_F,
GlobalParamters.FitRectModel.minScore_F,
GlobalParamters.FitRectModel.numMatches_F,
GlobalParamters.FitRectModel.maxOverlap_F,
GlobalParamters.FitRectModel.subPixel_F,
GlobalParamters.FitRectModel.numLevels_F,
out HTuple row,
out HTuple column,
out HTuple angle,
out HTuple score);
// 生成仿射变换矩阵
/// 生成仿射矩阵
///
HOperatorSet.VectorAngleToRigid(GlobalParamters.XYA.Y, GlobalParamters.XYA.X, GlobalParamters.XYA.A,
row.D, column.D, angle.D, out HTuple HomMat2D);
// 对找线工具进行仿射变换
// 进行仿射变换
HTuple X1_out, Y1_out, X2_out, Y2_out;
HOperatorSet.AffineTransPixel(HomMat2D, GlobalParamters.LineUp_INFO.StartY, GlobalParamters.LineUp_INFO.StartX, out Y1_out, out X1_out);
HOperatorSet.AffineTransPixel(HomMat2D, GlobalParamters.LineUp_INFO.EndY, GlobalParamters.LineUp_INFO.EndX, out Y2_out, out X2_out);
Line_INFO affined_UpLine = new Line_INFO(Y1_out, X1_out, Y2_out, X2_out);
HOperatorSet.AffineTransPixel(HomMat2D, GlobalParamters.LineLeft_INFO.StartY, GlobalParamters.LineLeft_INFO.StartX, out Y1_out, out X1_out);
HOperatorSet.AffineTransPixel(HomMat2D, GlobalParamters.LineLeft_INFO.EndY, GlobalParamters.LineLeft_INFO.EndX, out Y2_out, out X2_out);
Line_INFO affined_LeftLine = new Line_INFO(Y1_out, X1_out, Y2_out, X2_out);
HOperatorSet.AffineTransPixel(HomMat2D, GlobalParamters.LineDown_INFO.StartY, GlobalParamters.LineDown_INFO.StartX, out Y1_out, out X1_out);
HOperatorSet.AffineTransPixel(HomMat2D, GlobalParamters.LineDown_INFO.EndY, GlobalParamters.LineDown_INFO.EndX, out Y2_out, out X2_out);
Line_INFO affined_DownLine = new Line_INFO(Y1_out, X1_out, Y2_out, X2_out);
HOperatorSet.AffineTransPixel(HomMat2D, GlobalParamters.LineRight_INFO.StartY, GlobalParamters.LineRight_INFO.StartX, out Y1_out, out X1_out);
HOperatorSet.AffineTransPixel(HomMat2D, GlobalParamters.LineRight_INFO.EndY, GlobalParamters.LineRight_INFO.EndX, out Y2_out, out X2_out);
Line_INFO affined_RightLine = new Line_INFO(Y1_out, X1_out, Y2_out, X2_out);
// 读取找线参数
// 利用测量参数和找线工具拟合直线
AlgoHelper.MeasureLine(AlgoHelper.HobjectToHimage(ho_Image),
affined_UpLine,
GlobalParamters.LineRectMetrologyInfo,
out Line_INFO OutLine_up,
out HTuple Row_up,
out HTuple Col_up,
out HXLDCont MeasureXLD_up,
new HRegion());
AlgoHelper.MeasureLine(AlgoHelper.HobjectToHimage(ho_Image),
affined_LeftLine,
GlobalParamters.LineRectMetrologyInfo,
out Line_INFO OutLine_left,
out HTuple Row_left,
out HTuple Col_left,
out HXLDCont MeasureXLD_left,
new HRegion());
AlgoHelper.MeasureLine(AlgoHelper.HobjectToHimage(ho_Image),
affined_DownLine,
GlobalParamters.LineRectMetrologyInfo,
out Line_INFO OutLine_down,
out HTuple Row_down,
out HTuple Col_down,
out HXLDCont MeasureXLD_down,
new HRegion());
AlgoHelper.MeasureLine(AlgoHelper.HobjectToHimage(ho_Image),
affined_RightLine,
GlobalParamters.LineRectMetrologyInfo,
out Line_INFO OutLine_right,
out HTuple Row_right,
out HTuple Col_right,
out HXLDCont MeasureXLD_right,
new HRegion());
// 获取中心点
HOperatorSet.IntersectionLines(OutLine_up.StartY, OutLine_up.StartX, OutLine_up.EndY, OutLine_up.EndX, OutLine_left.StartY, OutLine_left.StartX, OutLine_left.EndY, OutLine_left.EndX, out HTuple y1, out HTuple x1, out HTuple isOverlapping1);
HOperatorSet.IntersectionLines(OutLine_left.StartY, OutLine_left.StartX, OutLine_left.EndY, OutLine_left.EndX, OutLine_down.StartY, OutLine_down.StartX, OutLine_down.EndY, OutLine_down.EndX, out HTuple y2, out HTuple x2, out HTuple isOverlapping2);
HOperatorSet.IntersectionLines(OutLine_down.StartY, OutLine_down.StartX, OutLine_down.EndY, OutLine_down.EndX, OutLine_right.StartY, OutLine_right.StartX, OutLine_right.EndY, OutLine_right.EndX, out HTuple y3, out HTuple x3, out HTuple isOverlapping3);
HOperatorSet.IntersectionLines(OutLine_right.StartY, OutLine_right.StartX, OutLine_right.EndY, OutLine_right.EndX, OutLine_up.StartY, OutLine_up.StartX, OutLine_up.EndY, OutLine_up.EndX, out HTuple y4, out HTuple x4, out HTuple isOverlapping4);
HOperatorSet.ReadTuple("./CameraParameters.tup", out HTuple hv_CameraParameters);
HOperatorSet.ReadTuple("./CameraPose.tup", out HTuple hv_CameraPose);
HOperatorSet.ChangeRadialDistortionCamPar("adaptive", hv_CameraParameters, 0,
out HTuple hv_CamParamChange);
HTuple X1,Y1,X2,Y2;
// 左边
HOperatorSet.ImagePointsToWorldPlane(hv_CamParamChange, hv_CameraPose, y1,
x1, "mm", out X1, out Y1);
HOperatorSet.ImagePointsToWorldPlane(hv_CamParamChange, hv_CameraPose, y2,
x2, "mm", out X2, out Y2);
HOperatorSet.DistancePp(Y1, X1, Y2, X2, out LeftDistance);
// 下边
HOperatorSet.ImagePointsToWorldPlane(hv_CamParamChange, hv_CameraPose, y2,
x2, "mm", out X1, out Y1);
HOperatorSet.ImagePointsToWorldPlane(hv_CamParamChange, hv_CameraPose, y3,
x3, "mm", out X2, out Y2);
HOperatorSet.DistancePp(Y1, X1, Y2, X2, out DownDistance);
// 右边
HOperatorSet.ImagePointsToWorldPlane(hv_CamParamChange, hv_CameraPose, y3,
x3, "mm", out X1, out Y1);
HOperatorSet.ImagePointsToWorldPlane(hv_CamParamChange, hv_CameraPose, y4,
x4, "mm", out X2, out Y2);
HOperatorSet.DistancePp(Y1, X1, Y2, X2, out RightDistance);
// 上边
HOperatorSet.ImagePointsToWorldPlane(hv_CamParamChange, hv_CameraPose, y4,
x4, "mm", out X1, out Y1);
HOperatorSet.ImagePointsToWorldPlane(hv_CamParamChange, hv_CameraPose, y1,
x1, "mm", out X2, out Y2);
HOperatorSet.DistancePp(Y1, X1, Y2, X2, out UpDistance);
}
/// <summary>
/// 测量矩形四条边
/// </summary>
/// <param name="ho_Image"></param>
/// <param name="UpDistance"></param>
/// <param name="LeftDistance"></param>
/// <param name="DownDistance"></param>
/// <param name="RightDistance"></param>
public void MeasureRectangleTool(HObject ho_Image, out HTuple UpDistance, out HTuple LeftDistance,
out HTuple DownDistance, out HTuple RightDistance)
{
halconAlgo.MeasureRectangleTool( ho_Image, out UpDistance, out LeftDistance,
out DownDistance, out RightDistance);
}
二 通讯修改
清除无用通讯代码
Measure,1
三 工作流程修改
四 返回上位机信息
CommunicateService.Instance.Send("NG,"+"上边长度,"+ UpDistance + ",左边长度," + LeftDistance + ",下边长度," + DownDistance + ",右边长度," + RightDistance);
CommunicateService.Instance.Send("OK," + "上边长度," + UpDistance + ",左边长度," + LeftDistance + ",下边长度," + DownDistance + ",右边长度," + RightDistance);
–
五 图像转灰度图像
/// <summary>
/// 用于检测的图像函数
/// </summary>
/// <param name="hImg"></param>
public void GrabDetectImage(HImage hImg)
{
// 1.获取到mainform窗口
var mainForm = Application.OpenForms.OfType<MainForm>().FirstOrDefault();
// 2.获取窗口上的Halcon的显示控件
HWindow_Final hWindow = mainForm.hWindow_Final_CameraImg;
HOperatorSet.MapImage(hImg, Map, out HObject imageMapped);
// 3. 显示图像
hWindow.HobjectToHimage(imageMapped);
// 转灰度图像
HOperatorSet.Rgb1ToGray(imageMapped,out imageMapped);
// 获取当前时间
DateTime dateTime = DateTime.Now;
// 4.把图像加到图像队列里
//(3) 利用添加图像函数,把图像添加到workflowService的图像队列里
ImageInfo imageInfo = new ImageInfo(
dateTime.ToString("yyyy_MM_dd"),
dateTime.ToString("HH_mm_ss_fff"),
imageMapped
);
WorkFlowService.Instance.AddImageInfo1(imageInfo);
}