public override bool doLocal() { try { NowResult = new LocalResult(); St_LinesParam LinesPara = new St_LinesParam(2); LinesPara = NowVisionPara.Lines; HTuple LineRow1 = new HTuple(), LineCol1 = new HTuple(), LineRow2 = new HTuple(), LineCol2 = new HTuple(); HObject LinesContour = new HObject(), LinePtCont = new HObject(); //找出所有的直线 LineTypePos.FindLine(LinesPara, NowImg, out LineRow1, out LineCol1, out LineRow2, out LineCol2, out LinesContour, out LinePtCont); HTuple CrossRow = new HTuple(), CrossCol = new HTuple(), IsParallel = new HTuple(); //第一条直线和第二条直线的交点 HOperatorSet.IntersectionLl(LineRow1[0], LineCol1[0], LineRow2[0], LineCol2[0], LineRow1[1], LineCol1[1], LineRow2[1], LineCol2[1], out CrossRow, out CrossCol, out IsParallel); HTuple angle; //图像坐标轴X轴到直线1的角度 HOperatorSet.AngleLx(LineRow1[0], LineCol1[0], LineRow2[0], LineCol2[0], out angle); NowResult.row = CrossRow.D; NowResult.col = CrossCol.D; NowResult.angle = angle.D; HOperatorSet.ConcatObj(LinesContour, LinePtCont, out LinesContour); LinePtCont.Dispose(); NowResult.ShowContour = LinesContour; return(true); } catch { return(false); } }
/// <summary> /// 計算線與線的角度 /// </summary> /// <param name="line1">IMeasureGeoModel 線段1</param> /// <param name="line2">IMeasureGeoModel 線段2</param> /// <returns></returns> public static AngleResult AngleLineToLine(IMeasureGeoModel line1, IMeasureGeoModel line2) { AngleResult result = null; //check if (isMeasureModelValid(line1) && isMeasureModelValid(line2)) { //act HTuple angle, firstPhi, secondPhi, interRow, interCol, isParallel; HOperatorSet.AngleLx(line1.Row1, line1.Col1, line1.Row2, line1.Col2, out firstPhi); HOperatorSet.AngleLx(line2.Row1, line2.Col1, line2.Row2, line2.Col2, out secondPhi); HOperatorSet.IntersectionLl(line1.Row1, line1.Col1, line1.Row2, line1.Col2, line2.Row1, line2.Col1, line2.Row2, line2.Col2, out interRow, out interCol, out isParallel); HOperatorSet.AngleLl(line1.Row1, line1.Col1, line1.Row2, line1.Col2, line2.Row1, line2.Col1, line2.Row2, line2.Col2, out angle); result = new AngleResult() { Angle = angle, StartPhi = firstPhi, EndPhi = secondPhi, Row = interRow, Col = interCol, }; } return(result); }
public override bool doLocal() { try { NowResult = new LocalResult(); St_CirclesParam CirclesParam = new St_CirclesParam(2); CirclesParam = NowVisionPara.Circles; HTuple CircleRows = new HTuple(), CircleCols = new HTuple(), CircleRs = new HTuple(), StartPhis = new HTuple(), EndPhis = new HTuple(); HObject CircleContour = new HObject(), CircleCont = new HObject(), CenterCross = new HObject(); //找出要找的圆 CircleTypePos.FindCircle(NowImg, CirclesParam, out CircleRows, out CircleCols, out CircleRs, out StartPhis, out EndPhis, out CircleContour, out CircleCont, out CenterCross); HOperatorSet.ConcatObj(CircleContour, CircleCont, out CircleContour); HOperatorSet.ConcatObj(CircleContour, CenterCross, out CircleContour); CircleCont.Dispose(); CenterCross.Dispose(); NowResult.ShowContour = CircleContour; NowResult.row = (CircleRows[0].D + CircleCols[1].D) / 2.0; NowResult.col = (CircleCols[0].D + CircleCols[1].D) / 2.0; HTuple angle = new HTuple(); HOperatorSet.AngleLx(CircleRows[0], CircleCols[0], CircleRows[1], CircleCols[1], out angle); NowResult.angle = angle.D; return(true); } catch { return(false); } }
private void Button_Click_D_Measure(object sender, RoutedEventArgs e) { HObject ho_Rectange_Again = null; HOperatorSet.SetDraw(this.CamSetting.HalconID, "margin"); HOperatorSet.SetColor(this.CamSetting.HalconID, "#FF00FF"); HTuple hv_r2r = null, hv_r2c = null, hv_r2phi = null, hv_r2w = null, hv_r2h = null; HOperatorSet.DrawRectangle2(this.CamSetting.HalconID, out hv_r2r, out hv_r2c, out hv_r2phi, out hv_r2w, out hv_r2h); HOperatorSet.GenRectangle2(out ho_Rectange_Again, hv_r2r, hv_r2c, hv_r2phi, hv_r2w, hv_r2h); HTuple hv_c = null, hv_r = null; try { /* * switch (global.GetIns().CamSel) * { * case 0: CameraADisp.Measure_Diameter(Obj[0], hv_r2r, hv_r2c, hv_r2phi, hv_r2w, hv_r2h, out hv_c, out hv_r, this.CamSetting.HalconID); break; * case 1: CameraBDisp.Measure_Diameter(Obj[1], hv_r2r, hv_r2c, hv_r2phi, hv_r2w, hv_r2h, out hv_c, out hv_r, this.CamSetting.HalconID); break; * case 2: CameraCDisp.Measure_Diameter(Obj[2], hv_r2r, hv_r2c, hv_r2phi, hv_r2w, hv_r2h, out hv_c, out hv_r, this.CamSetting.HalconID); break; * // case 3: CameraDDisp.Measure_Diameter(Obj[3], hv_r2r, hv_r2c, hv_r2phi, hv_r2w, hv_r2h, this.CamSetting.HalconID); break; * default: break; * } */ Info_Ctrl Infc = new Info_Ctrl(); ImageOperate.Measure_Diameter(Obj[global.GetIns().CamSel], hv_r2r, hv_r2c, hv_r2phi, hv_r2w, hv_r2h, out Infc, this.CamSetting.HalconID, ImageOperate.Track_Model); INI.axis_roi.ElementAt(global.GetIns().CamSel).axis_d1_r1 = hv_r2r - Infc.pos_y; INI.axis_roi.ElementAt(global.GetIns().CamSel).axis_d1_c1 = hv_r2c - Infc.pos_x; INI.axis_roi.ElementAt(global.GetIns().CamSel).axis_d1_r2 = hv_r2w; INI.axis_roi.ElementAt(global.GetIns().CamSel).axis_d1_c2 = hv_r2h; INI.axis_roi.ElementAt(global.GetIns().CamSel).axis_d1_phi = hv_r2phi - Infc.pos_angle; try { HTuple hv_Angle = null; HOperatorSet.AngleLx(Infc.pos_y, Infc.pos_x, hv_r2r, hv_r2c, out hv_Angle); INI.axis_roi.ElementAt(global.GetIns().CamSel).axis_d1_relative_phi = hv_Angle; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } HOperatorSet.DispObj(ho_Rectange_Again, this.CamSetting.HalconID); INI.writting(); } catch (Exception ex) { history.HistoryNotify += ex.ToString() + "\r\n"; return; } }
private void LoadingParams(string CameraLabelText, string LineDetectName, string filePath) { CameraLabel.Text = CameraLabelText; LineDetect.SaveLineDataName = LineDetectName; ho_Image = new HImage(filePath); LineDetect.ho_Image = ho_Image; if (!LineDetect.ReadParams(LineDetect.SaveLineDataName)) { MessageBox.Show("读取默认参数失败"); } //生成ROI roiController.ROIList.Clear(); roiController.roiMode = ROIRectangle2; ROIRectangle2.midR = (LineDetect.hv_MeasureRow1 + LineDetect.hv_MeasureRow2) / 2; ROIRectangle2.midC = (LineDetect.hv_MeasureColumn1 + LineDetect.hv_MeasureColumn2) / 2; HOperatorSet.AngleLx(LineDetect.hv_MeasureRow1, LineDetect.hv_MeasureColumn1, LineDetect.hv_MeasureRow2, LineDetect.hv_MeasureColumn2, out HTuple AngleL); ROIRectangle2.phi = -Convert.ToDouble(AngleL.ToString()); HOperatorSet.DistancePp(LineDetect.hv_MeasureRow1, LineDetect.hv_MeasureColumn1, LineDetect.hv_MeasureRow2, LineDetect.hv_MeasureColumn2, out HTuple LengthL); ROIRectangle2.length1 = Convert.ToDouble(LengthL.ToString()) / 2; ROIRectangle2.length2 = LineDetect.hv_MeasureLength1; roiController.roiMode.createROI(ROIRectangle2.midC, ROIRectangle2.midR); LineDetect.RefreshParams(); ReDraw(); roiController.ROIList.Add(roiController.roiMode); roiController.activeROIidx = -1; HWndCtrl.addIconicVar(ho_Image); HWndCtrl.resetAll(); HWndCtrl.repaint(); SendMessage(this.TLPanel_Bottom.Handle, WM_SETREDRAW, 0, IntPtr.Zero);//禁止重绘 TLPCheckParam.Controls.Clear(); TLPCheckParam.Controls.Add(LineDetect); LineDetect.Show(); SendMessage(TLPanel_Bottom.Handle, WM_SETREDRAW, 1, IntPtr.Zero); //取消禁止 TLPanel_Bottom.Refresh(); //刷新控件 }
private void Button_Click_Slot_Measure(object sender, RoutedEventArgs e) { HObject ho_Rectange_Again = null; HOperatorSet.SetDraw(this.CamSetting.HalconID, "margin"); HOperatorSet.SetColor(this.CamSetting.HalconID, "green"); HTuple hv_r2r = null, hv_r2c = null, hv_r2phi = null, hv_r2w = null, hv_r2h = null; HOperatorSet.DrawRectangle2(this.CamSetting.HalconID, out hv_r2r, out hv_r2c, out hv_r2phi, out hv_r2w, out hv_r2h); HOperatorSet.GenRectangle2(out ho_Rectange_Again, hv_r2r, hv_r2c, hv_r2phi, hv_r2w, hv_r2h); HTuple hv_r = null, hv_c = null; try { Info_Ctrl Infc = new Info_Ctrl(); ImageOperate.Measure_Diameter(Obj[global.GetIns().CamSel], hv_r2r, hv_r2c, hv_r2phi, hv_r2w, hv_r2h, out Infc, this.CamSetting.HalconID, ImageOperate.Track_Model); INI.axis_roi.ElementAt(global.GetIns().CamSel).axis_d2_r1 = hv_r2r - Infc.pos_y; INI.axis_roi.ElementAt(global.GetIns().CamSel).axis_d2_c1 = hv_r2c - Infc.pos_x; INI.axis_roi.ElementAt(global.GetIns().CamSel).axis_d2_r2 = hv_r2w; INI.axis_roi.ElementAt(global.GetIns().CamSel).axis_d2_c2 = hv_r2h; INI.axis_roi.ElementAt(global.GetIns().CamSel).axis_d2_phi = hv_r2phi - Infc.pos_angle; try { HTuple hv_Angle = null; HOperatorSet.AngleLx(Infc.pos_y, Infc.pos_x, hv_r2r, hv_r2c, out hv_Angle); INI.axis_roi.ElementAt(global.GetIns().CamSel).axis_d2_relative_phi = hv_Angle; } catch (Exception exe) { MessageBox.Show(exe.ToString()); } HOperatorSet.DispObj(ho_Rectange_Again, this.CamSetting.HalconID); INI.writting(); } catch (HalconException ex) { history.HistoryNotify += ex.ToString() + "\r\n"; return; } }
/// <summary> /// 計算兩線段的對稱線 /// </summary> /// <param name="modelOne"></param> /// <param name="modelTwo"></param> /// <returns></returns> public static LineResult CalculateSymmetryLine(IMeasureGeoModel modelOne, IMeasureGeoModel modelTwo) { LineResult result = null; var modelValid = isMeasureModelValid(modelOne) && isMeasureModelValid(modelTwo); if (modelValid) { //取得第一條線段的中點 var pointModel = getMidPoint(modelOne); var lineModel = modelTwo; //計算第一線段中點到第二線段上的投影點(垂直距離) HTuple rowProj, colProj; HOperatorSet.ProjectionPl(pointModel.Row1, pointModel.Col1, lineModel.Row1, lineModel.Col1, lineModel.Row2, lineModel.Col2, out rowProj, out colProj); //計算第一線段中點與第二線段上的投影點距離 (point to point) HTuple distance; HOperatorSet.DistancePp(rowProj, colProj, pointModel.Row1, pointModel.Col1, out distance); //取兩線中點 var cRow = (pointModel.Row1 + rowProj) / 2.0; var cCol = (pointModel.Col1 + colProj) / 2.0; //取角度 HTuple angle; HOperatorSet.AngleLx(lineModel.Row1, lineModel.Col1, lineModel.Row2, lineModel.Col2, out angle); //線段 double[] rows, cols; DetermineLine(cRow, cCol, angle, distance, out rows, out cols); result = new LineResult() { Row1 = rows[0], Row2 = rows[1], Col1 = cols[0], Col2 = cols[1], Distance = distance, }; } return(result); }
public override bool doLocal() { try { NowResult = new LocalResult(); St_LinesParam LinesPara = new St_LinesParam(2); LinesPara = NowVisionPara.Lines; HTuple LineRow1 = new HTuple(), LineCol1 = new HTuple(), LineRow2 = new HTuple(), LineCol2 = new HTuple(); HObject LinesContour = new HObject(), LinePtCont = new HObject(); //找出所有的直线 LineTypePos.FindLine(LinesPara, NowImg, out LineRow1, out LineCol1, out LineRow2, out LineCol2, out LinesContour, out LinePtCont); St_CirclesParam CirclesParam = new St_CirclesParam(2); CirclesParam = NowVisionPara.Circles; HTuple CircleRows = new HTuple(), CircleCols = new HTuple(), CircleRs = new HTuple(), StartPhis = new HTuple(), EndPhis = new HTuple(); HObject CircleContour = new HObject(), CircleCont = new HObject(), CenterCross = new HObject(); HTuple angle; //图像坐标轴X轴到直线1的角度 HOperatorSet.AngleLx(LineRow1[0], LineCol1[0], LineRow2[0], LineCol2[0], out angle); //找出要找的圆 CircleTypePos.FindCircle(NowImg, CirclesParam, out CircleRows, out CircleCols, out CircleRs, out StartPhis, out EndPhis, out CircleContour, out CircleCont, out CenterCross); HOperatorSet.ConcatObj(CircleContour, CircleCont, out CircleContour); HOperatorSet.ConcatObj(CircleContour, CenterCross, out CircleContour); CircleCont.Dispose(); CenterCross.Dispose(); HOperatorSet.ConcatObj(CircleContour, LinesContour, out CircleContour); HOperatorSet.ConcatObj(CircleContour, LinePtCont, out CircleContour); LinesContour.Dispose(); LinePtCont.Dispose(); NowResult.row = CircleRows[0].D; NowResult.col = CircleCols[0].D; NowResult.angle = angle.D; NowResult.ShowContour = CircleContour; return(true); } catch { return(false); } }
public static void ShowMeasureRectangle(HTuple hv_ImageWindow, HTuple hv_ZoomFactor, LineDetect LineDetect, HTuple color) { HTuple hv_Row1 = new HTuple(); HTuple hv_Column1 = new HTuple(); HTuple hv_Row2 = new HTuple(); HTuple hv_Column2 = new HTuple(); HTuple hv_MeasureLength1 = new HTuple(); hv_Row1 = LineDetect.hv_MeasureRow1 * hv_ZoomFactor; hv_Column1 = LineDetect.hv_MeasureColumn1 * hv_ZoomFactor; hv_Row2 = LineDetect.hv_MeasureRow2 * hv_ZoomFactor; hv_Column2 = LineDetect.hv_MeasureColumn2 * hv_ZoomFactor; hv_MeasureLength1 = LineDetect.hv_MeasureLength1 * hv_ZoomFactor; HOperatorSet.DistancePp(hv_Row1, hv_Column1, hv_Row2, hv_Column2, out HTuple hv_Distance); HOperatorSet.AngleLx(hv_Row1, hv_Column1, hv_Row2, hv_Column2, out HTuple hv_Angle); HOperatorSet.GenRectangle2(out HObject ho_Rectangle, (hv_Row1 + hv_Row2) / 2, (hv_Column1 + hv_Column2) / 2, hv_Angle, hv_Distance / 2, hv_MeasureLength1); HOperatorSet.SetColor(hv_ImageWindow, color); HOperatorSet.SetDraw(hv_ImageWindow, "margin"); HOperatorSet.SetLineWidth(hv_ImageWindow, 2); HOperatorSet.DispObj(ho_Rectangle, hv_ImageWindow); HOperatorSet.SetLineWidth(hv_ImageWindow, 1); ho_Rectangle.Dispose(); }
// Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_Rectangle, ho_ImageReduced; HObject ho_ModelContours, ho_ShowContours, ho_ModelContour; HObject ho_MeasureContour, ho_ResultContours = null, ho_Contour = null; HObject ho_UsedEdges = null, ho_Cross = null; // Local control variables HTuple hv_ImageFiles = null, hv_Width = null; HTuple hv_Height = null, hv_WindowHandle = new HTuple(); HTuple hv_Area = null, hv_RowRefer = null, hv_ColRefer = null; HTuple hv_ModelID = null, hv_HomMat2D = null, hv_MetrologyHandle = null; HTuple hv_Line = null, hv_LineIndices = null, hv_Row = null; HTuple hv_Column = null, hv_i = null, hv_RowFound = new HTuple(); HTuple hv_ColFound = new HTuple(), hv_AngleFound = new HTuple(); HTuple hv_ScoreFound = new HTuple(), hv_UsedRow = new HTuple(); HTuple hv_UsedColumn = new HTuple(), hv_Angle = new HTuple(); HTuple hv_Degree = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_ModelContours); HOperatorSet.GenEmptyObj(out ho_ShowContours); HOperatorSet.GenEmptyObj(out ho_ModelContour); HOperatorSet.GenEmptyObj(out ho_MeasureContour); HOperatorSet.GenEmptyObj(out ho_ResultContours); HOperatorSet.GenEmptyObj(out ho_Contour); HOperatorSet.GenEmptyObj(out ho_UsedEdges); HOperatorSet.GenEmptyObj(out ho_Cross); //dev_close_window(...); //**读取图片所在路径** list_image_files("Image", "default", new HTuple(), out hv_ImageFiles); ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, hv_ImageFiles.TupleSelect(0)); HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); //dev_open_window(...); HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "margin"); HOperatorSet.SetLineWidth(hv_ExpDefaultWinHandle, 2); HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); //创建模板 ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, 400, 570, 550, 700); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_Rectangle, out ho_ImageReduced); HOperatorSet.AreaCenter(ho_ImageReduced, out hv_Area, out hv_RowRefer, out hv_ColRefer); HOperatorSet.CreateShapeModel(ho_ImageReduced, "auto", (new HTuple(-10)).TupleRad() , (new HTuple(20)).TupleRad(), "auto", "auto", "use_polarity", "auto", "auto", out hv_ModelID); ho_ModelContours.Dispose(); HOperatorSet.GetShapeModelContours(out ho_ModelContours, hv_ModelID, 1); HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_RowRefer, hv_ColRefer, 0, out hv_HomMat2D); ho_ShowContours.Dispose(); HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ShowContours, hv_HomMat2D); HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); HOperatorSet.DispObj(ho_ShowContours, hv_ExpDefaultWinHandle); //创建测量模板 HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle); HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height); //从左向右在矩形上边缘画一条直线* hv_Line = new HTuple(); hv_Line[0] = 445; hv_Line[1] = 600; hv_Line[2] = 445; hv_Line[3] = 670; HOperatorSet.AddMetrologyObjectGeneric(hv_MetrologyHandle, "line", hv_Line, 25, 5, 1, 30, new HTuple(), new HTuple(), out hv_LineIndices); ho_ModelContour.Dispose(); HOperatorSet.GetMetrologyObjectModelContour(out ho_ModelContour, hv_MetrologyHandle, "all", 1.5); ho_MeasureContour.Dispose(); HOperatorSet.GetMetrologyObjectMeasures(out ho_MeasureContour, hv_MetrologyHandle, "all", "all", out hv_Row, out hv_Column); //把测量的位置和模板的位置关联起来 HOperatorSet.SetMetrologyModelParam(hv_MetrologyHandle, "reference_system", ((hv_RowRefer.TupleConcat( hv_ColRefer))).TupleConcat(0)); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_transition", "positive"); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_select", "first"); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_length1", 25); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_length2", 5); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "measure_threshold", 15); HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_LineIndices, "min_score", 0.3); for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_ImageFiles.TupleLength())) - 1); hv_i = (int)hv_i + 1) { ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, hv_ImageFiles.TupleSelect(hv_i)); HOperatorSet.FindShapeModel(ho_Image, hv_ModelID, (new HTuple(-10)).TupleRad() , (new HTuple(20)).TupleRad(), 0.3, 1, 0.5, "least_squares", 0, 0.75, out hv_RowFound, out hv_ColFound, out hv_AngleFound, out hv_ScoreFound); if ((int)(new HTuple((new HTuple(1)).TupleEqual(new HTuple(hv_RowFound.TupleLength() )))) != 0) { HOperatorSet.HomMat2dIdentity(out hv_HomMat2D); HOperatorSet.HomMat2dRotate(hv_HomMat2D, hv_AngleFound, 0, 0, out hv_HomMat2D); HOperatorSet.HomMat2dTranslate(hv_HomMat2D, hv_RowFound - 0, hv_ColFound - 0, out hv_HomMat2D); ho_ResultContours.Dispose(); HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ResultContours, hv_HomMat2D); HOperatorSet.AlignMetrologyModel(hv_MetrologyHandle, hv_RowFound, hv_ColFound, hv_AngleFound); //应用测量 HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle); //获取结果 ho_Contour.Dispose(); HOperatorSet.GetMetrologyObjectMeasures(out ho_Contour, hv_MetrologyHandle, "all", "all", out hv_Row, out hv_Column); HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "used_edges", "row", out hv_UsedRow); HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "used_edges", "column", out hv_UsedColumn); ho_UsedEdges.Dispose(); HOperatorSet.GenCrossContourXld(out ho_UsedEdges, hv_UsedRow, hv_UsedColumn, 10, (new HTuple(45)).TupleRad()); ho_ResultContours.Dispose(); HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContours, hv_MetrologyHandle, "all", "all", 1.5); ho_Cross.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross, hv_RowFound, hv_ColFound, 40, hv_AngleFound); HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); HOperatorSet.DispObj(ho_Cross, hv_ExpDefaultWinHandle); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); HOperatorSet.DispObj(ho_ResultContours, hv_ExpDefaultWinHandle); HOperatorSet.AngleLx(hv_UsedRow.TupleSelect(1), hv_UsedColumn.TupleSelect( 1), hv_UsedRow.TupleSelect(6), hv_UsedColumn.TupleSelect(6), out hv_Angle); HOperatorSet.TupleDeg(hv_Angle, out hv_Degree); disp_message(hv_ExpDefaultWinHandle, "定位成功!", "window", 12, 12, "black", "true"); disp_message(hv_ExpDefaultWinHandle, ((((new HTuple("定位区域中心坐标") + "(") + hv_RowFound) + new HTuple(",")) + hv_ColFound) + ")", "window", 32, 12, "black", "true"); HOperatorSet.TupleDeg(hv_AngleFound, out hv_Degree); disp_message(hv_ExpDefaultWinHandle, ("相对模板角度为" + hv_Degree) + "°", "window", 52, 12, "black", "true"); } else { disp_message(hv_ExpDefaultWinHandle, "没有找到模板", "window", hv_Row, hv_Column, "black", "true"); } HDevelopStop(); } HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle); HOperatorSet.ClearShapeModel(hv_ModelID); ho_Image.Dispose(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_ModelContours.Dispose(); ho_ShowContours.Dispose(); ho_ModelContour.Dispose(); ho_MeasureContour.Dispose(); ho_ResultContours.Dispose(); ho_Contour.Dispose(); ho_UsedEdges.Dispose(); ho_Cross.Dispose(); }
/// <summary> /// 获取 吸嘴中新对准点 /// </summary> /// <param name="snappos"></param> /// <param name="u0degModle"></param> /// <param name="Point2dVisionCoor"></param> /// <returns></returns> public XYUPoint GetPosWhenUpCameraSnap(int nIndex, XYUPoint snappos, XYUPoint Point2dVisionCoor) { XYUPoint posull = new XYUPoint(double.MaxValue, double.MaxValue, double.MaxValue); if (nIndex > resultCalibMulitNozzle.nozzleSturts.Count || nIndex < 0) { return(posull); } XYUPoint NozzleCalibRobotCoor = resultCalibMulitNozzle.nozzleSturts[nIndex].NozzleCalibRobotCoor; Point2d NozzleCalibVisionCoor = resultCalibMulitNozzle.nozzleSturts[nIndex].NozzleCalibVisionCoor; double u0degModle = resultCalibMulitNozzle.nozzleSturts[nIndex].u0degModle; bool IsCounterclockwise_Rotation = resultCalibMulitNozzle.nozzleSturts[nIndex].IsCounterclockwise_Rotation; // u0degModle 0 度模板(做模板) u的角度 HTuple qx, qy; HTuple dsqx, dsqy; HTuple dcqx, dcqy; HOperatorSet.AffineTransPoint2d(m_Hom2Drtd, snappos.x, snappos.y, out dsqx, out dsqy); HOperatorSet.AffineTransPoint2d(m_Hom2Drtd, CalibPos.x, CalibPos.y, out dcqx, out dcqy); HTuple calbx, calby; HOperatorSet.AffineTransPoint2d(m_Hom2Ddtr, dcqx, dcqy, out calbx, out calby); HOperatorSet.AffineTransPoint2d(m_Hom2Dutd, Point2dVisionCoor.x, Point2dVisionCoor.y, out qx, out qy); HTuple vecx, vecy; vecx = dsqx - dcqx; vecy = dsqy - dcqy; //mark 坐标 HTuple dmarkx, dmarky; dmarkx = qx + vecx; dmarky = qy + vecy; HTuple rmarkx, rmarky; HOperatorSet.AffineTransPoint2d(m_Hom2Ddtr, dmarkx, dmarky, out rmarkx, out rmarky); if (Point2dVisionCoor.u > Math.PI) { Point2dVisionCoor.u = Point2dVisionCoor.u - 2 * Math.PI; } else if (Point2dVisionCoor.u < -Math.PI) { Point2dVisionCoor.u = Point2dVisionCoor.u + 2 * Math.PI; } //旋转轴的物理 角度 HTuple tupleAngle = 0; if (!IsCounterclockwise_Rotation)// 旋转轴 正向 逆时针 IsCounterclockwise_Rotation=ture { tupleAngle = u0degModle - 180.000 * Point2dVisionCoor.u / Math.PI; } else { tupleAngle = u0degModle + 180.000 * Point2dVisionCoor.u / Math.PI; } HTuple rqx, rqy; HOperatorSet.AffineTransPoint2d(m_Hom2Ddtr, qx, qy, out rqx, out rqy); XYUPoint post = new XYUPoint(); post.x = rqx + snappos.x - CalibPos.x; post.y = rqy + snappos.y - CalibPos.y; HTuple vecNx, vecNy; HTuple dcnqx, dcnqy; HOperatorSet.AffineTransPoint2d(m_Hom2Drtd, NozzleCalibRobotCoor.x, NozzleCalibRobotCoor.y, out dcnqx, out dcnqy); vecNx = NozzleCalibVisionCoor.x - dcnqx; vecNy = NozzleCalibVisionCoor.y - dcnqy; HTuple lens = Math.Sqrt(vecNx.D * vecNx.D + vecNy.D * vecNy.D); //需要旋转的角度 HTuple AngleRote = tupleAngle - snappos.u; HTuple tupleAnglecalib = 0; HOperatorSet.AngleLx(dcnqy, dcnqx, NozzleCalibVisionCoor.y, NozzleCalibVisionCoor.x, out tupleAnglecalib); // HOperatorSet.AngleLl(0,0,0,100,dcnqy, dcnqx, NozzleCalibVisionCoor.y, NozzleCalibVisionCoor.x, out tupleAnglecalib); HTuple destangleandR = 0; if (!IsCounterclockwise_Rotation) { destangleandR = tupleAnglecalib - Math.PI * (tupleAngle - NozzleCalibRobotCoor.u) / 180.0000; } else { destangleandR = tupleAnglecalib + Math.PI * (tupleAngle - NozzleCalibRobotCoor.u) / 180.0000; } HTuple Destnozzledx = dsqx + Math.Cos(destangleandR.D) * lens; HTuple Destnozzledy = dsqy - Math.Sin(destangleandR.D) * lens; HTuple rnozzlex, rnozzley; HOperatorSet.AffineTransPoint2d(m_Hom2Ddtr, Destnozzledx, Destnozzledy, out rnozzlex, out rnozzley); HTuple offsetx = rmarkx - rnozzlex; HTuple offsety = rmarky - rnozzley; XYUPoint pos = new XYUPoint(snappos.x + offsetx.D, snappos.y + offsety.D, tupleAngle); return(pos); }
/// <summary> /// 获取 吸嘴中新对准点 上相机拍照 获取吸嘴对应的点视觉坐标 转换得出 机械坐标点(带旋转的机械手 适用 上相机 拍照拾取) /// </summary>带旋转的机械手 适用 上相机 拍照拾取 /// <param name="snappos"> 拍照点 机械手坐标</param> /// <param name="u0degModle"> 0 度模板对应的 机械手 机械坐标</param> /// <param name="Point2dVisionCoor">视觉mark的视觉坐标</param> /// <returns></returns> public XYUPoint GetPosWhenUpCameraSnap(XYUPoint snappos, double u0degModle, XYUPoint Point2dVisionCoor) { // u0degModle 0 度模板(做模板) u的角度 HTuple qx, qy; HTuple dsqx, dsqy; HTuple dcqx, dcqy; HOperatorSet.AffineTransPoint2d(m_Hom2Drtd, snappos.x, snappos.y, out dsqx, out dsqy); HOperatorSet.AffineTransPoint2d(m_Hom2Drtd, CalibPos.x, CalibPos.y, out dcqx, out dcqy); HTuple calbx, calby; HOperatorSet.AffineTransPoint2d(m_Hom2Ddtr, dcqx, dcqy, out calbx, out calby); HOperatorSet.AffineTransPoint2d(m_Hom2Dutd, Point2dVisionCoor.x, Point2dVisionCoor.y, out qx, out qy); HTuple vecx, vecy; vecx = dsqx - dcqx; vecy = dsqy - dcqy; //mark 坐标 HTuple dmarkx, dmarky; dmarkx = qx + vecx; dmarky = qy + vecy; HTuple rmarkx, rmarky; HOperatorSet.AffineTransPoint2d(m_Hom2Ddtr, dmarkx, dmarky, out rmarkx, out rmarky); if (Point2dVisionCoor.u > Math.PI) { Point2dVisionCoor.u = Point2dVisionCoor.u - 2 * Math.PI; } else if (Point2dVisionCoor.u < -Math.PI) { Point2dVisionCoor.u = Point2dVisionCoor.u + 2 * Math.PI; } HTuple tupleAngle = u0degModle - 180.000 * Point2dVisionCoor.u / Math.PI; HTuple rqx, rqy; //HOperatorSet.AffineTransPoint2d(m_Hom2Ddtr, qx, qy, out rqx, out rqy); //XYUPoint post = new XYUPoint(); //post.x = rqx + snappos.x - CalibPos.x; //post.y = rqy + snappos.y - CalibPos.y; HTuple vecNx, vecNy; HTuple dcnqx, dcnqy; HOperatorSet.AffineTransPoint2d(m_Hom2Drtd, NozzleCalibRobotCoor.x, NozzleCalibRobotCoor.y, out dcnqx, out dcnqy); vecNx = NozzleCalibVisionCoor.x - dcnqx; vecNy = NozzleCalibVisionCoor.y - dcnqy; HTuple lens = Math.Sqrt(vecNx.D * vecNx.D + vecNy.D * vecNy.D); ////旋转后到的角度 // HTuple tupleAngle = u0degModle+180.000* Point2dVisionCoor.u/Math.PI; //需要旋转的角度 HTuple AngleRote = tupleAngle - snappos.u; HTuple tupleAnglecalib = 0; HOperatorSet.AngleLx(dcnqy, dcnqx, NozzleCalibVisionCoor.y, NozzleCalibVisionCoor.x, out tupleAnglecalib); // HOperatorSet.AngleLl(0,0,0,100,dcnqy, dcnqx, NozzleCalibVisionCoor.y, NozzleCalibVisionCoor.x, out tupleAnglecalib); HTuple destangleandR = tupleAnglecalib - Math.PI * (tupleAngle - NozzleCalibRobotCoor.u) / 180.0000; HTuple Destnozzledx = dsqx + Math.Cos(destangleandR.D) * lens; HTuple Destnozzledy = dsqy - Math.Sin(destangleandR.D) * lens; HTuple rnozzlex, rnozzley; HOperatorSet.AffineTransPoint2d(m_Hom2Ddtr, Destnozzledx, Destnozzledy, out rnozzlex, out rnozzley); HTuple offsetx = rmarkx - rnozzlex; HTuple offsety = rmarky - rnozzley; XYUPoint pos = new XYUPoint(snappos.x + offsetx.D, snappos.y + offsety.D, tupleAngle); return(pos); }
/// <summary> /// 执行 /// </summary> /// <param name="image">图像</param> /// <param name="outputs">输出结果</param> /// <returns>执行结果</returns> public void Execute(object image, out ItemCollection outputs) { HObject hImage = image as HObject; outputs = new ItemCollection(); stopwatch.Restart(); //初始化变量 HObject ho_GrayImage, ho_Regions; HObject ho_ConnectedRegions, ho_SelectedRegions, ho_SelectedRegions1; HObject ho_Cross1 = null, ho_Cross2 = null; // Local control variables HTuple hv_Area = null, hv_Row = null, hv_Column = null; HTuple hv_Area1 = null, hv_Row1 = null, hv_Column1 = null; HTuple hv_DistanceMin = new HTuple(), hv_DistanceMax = new HTuple(); HTuple hv_Index = new HTuple(), hv_TempDistance = new HTuple(); HTuple hv_Distance = new HTuple(), hv_Indices = new HTuple(); HTuple hv_Angle = new HTuple(), hv_Deg = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_GrayImage); HOperatorSet.GenEmptyObj(out ho_Regions); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_Cross1); HOperatorSet.GenEmptyObj(out ho_Cross2); try { HTuple width, height; HOperatorSet.GetImageSize(hImage, out width, out height); //若未初始化,则进行初始化 if (!isInit) { isInit = true; } if (runningWindow == null) { try { runningWindow = (RunningWindow as HSmartWindowControlWPF).HalconWindow; } catch (Exception) { } } if (configWindow == null) { try { configWindow = (ConfigWindow as HSmartWindowControlWPF).HalconWindow; } catch (Exception) { } } HOperatorSet.SetSystem("flush_graphic", "false"); if (runningWindow != null) { SetWindowPart(runningWindow, width, height); HOperatorSet.ClearWindow(runningWindow); HOperatorSet.DispObj(hImage, runningWindow); } if (configWindow != null) { SetWindowPart(configWindow, width, height); HOperatorSet.ClearWindow(configWindow); HOperatorSet.DispObj(hImage, configWindow); } //执行主任务 ho_GrayImage.Dispose(); HOperatorSet.Rgb1ToGray(hImage, out ho_GrayImage); //Blob ho_Regions.Dispose(); HOperatorSet.Threshold(ho_GrayImage, out ho_Regions, 100, 255); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions); //选择圆 ho_SelectedRegions.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, (new HTuple("circularity")).TupleConcat( "area"), "and", (new HTuple(0.85)).TupleConcat(7000), (new HTuple(1)).TupleConcat( 8000)); HOperatorSet.AreaCenter(ho_SelectedRegions, out hv_Area, out hv_Row, out hv_Column); //选择Mask点 ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions1, ((new HTuple("height")).TupleConcat( "area")).TupleConcat("width"), "and", ((new HTuple(35)).TupleConcat(1000)).TupleConcat( 35), ((new HTuple(70)).TupleConcat(1600)).TupleConcat(70)); HOperatorSet.AreaCenter(ho_SelectedRegions1, out hv_Area1, out hv_Row1, out hv_Column1); if ((int)((new HTuple((new HTuple(hv_Row.TupleLength())).TupleGreater(1))).TupleAnd( new HTuple((new HTuple(hv_Row1.TupleLength())).TupleGreater(0)))) != 0) { //找出与Mask点距离最短的原点 HOperatorSet.DistancePr(ho_SelectedRegions, hv_Row1, hv_Column1, out hv_DistanceMin, out hv_DistanceMax); for (hv_Index = 1; (int)hv_Index <= (int)(new HTuple(hv_Row.TupleLength())); hv_Index = (int)hv_Index + 1) { HOperatorSet.DistancePp(hv_Row.TupleSelect(hv_Index - 1), hv_Column.TupleSelect( hv_Index - 1), hv_Row1.TupleSelect(0), hv_Column1.TupleSelect(0), out hv_TempDistance); hv_Distance = hv_Distance.TupleConcat(hv_TempDistance); } HOperatorSet.TupleSortIndex(hv_Distance, out hv_Indices); //原点角度 HOperatorSet.AngleLx(hv_Row.TupleSelect(hv_Indices.TupleSelect(0)), hv_Column.TupleSelect( hv_Indices.TupleSelect(0)), hv_Row.TupleSelect(hv_Indices.TupleSelect((new HTuple(hv_Indices.TupleLength() )) - 1)), hv_Column.TupleSelect(hv_Indices.TupleSelect((new HTuple(hv_Indices.TupleLength() )) - 1)), out hv_Angle); HOperatorSet.TupleDeg(hv_Angle, out hv_Deg); Outputs["BaseX"].Value = hv_Column.TupleSelect(hv_Indices.TupleSelect(0)).D; Outputs["BaseY"].Value = hv_Row.TupleSelect(hv_Indices.TupleSelect(0)).D; Outputs["Angle"].Value = hv_Angle.D; //显示结果 ho_Cross1.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross1, hv_Row.TupleSelect(hv_Indices.TupleSelect( 0)), hv_Column.TupleSelect(hv_Indices.TupleSelect(0)), 24, 0.785398); ho_Cross2.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross2, hv_Row.TupleSelect(hv_Indices.TupleSelect( (new HTuple(hv_Indices.TupleLength())) - 1)), hv_Column.TupleSelect(hv_Indices.TupleSelect( (new HTuple(hv_Indices.TupleLength())) - 1)), 24, 0.785398); if (runningWindow != null) { HOperatorSet.DispObj(hImage, runningWindow); HOperatorSet.DispObj(ho_SelectedRegions, runningWindow); HOperatorSet.DispObj(ho_SelectedRegions1, runningWindow); HOperatorSet.DispObj(ho_Cross1, runningWindow); HOperatorSet.DispObj(ho_Cross2, runningWindow); HOperatorSet.DispLine(runningWindow, hv_Row.TupleSelect(hv_Indices.TupleSelect( 0)), hv_Column.TupleSelect(hv_Indices.TupleSelect(0)), hv_Row.TupleSelect( hv_Indices.TupleSelect((new HTuple(hv_Indices.TupleLength())) - 1)), hv_Column.TupleSelect( hv_Indices.TupleSelect((new HTuple(hv_Indices.TupleLength())) - 1))); } if (configWindow != null) { HOperatorSet.DispObj(ho_SelectedRegions, configWindow); HOperatorSet.DispObj(ho_SelectedRegions1, configWindow); HOperatorSet.DispObj(ho_Cross1, configWindow); HOperatorSet.DispObj(ho_Cross2, configWindow); HOperatorSet.DispLine(configWindow, hv_Row.TupleSelect(hv_Indices.TupleSelect( 0)), hv_Column.TupleSelect(hv_Indices.TupleSelect(0)), hv_Row.TupleSelect( hv_Indices.TupleSelect((new HTuple(hv_Indices.TupleLength())) - 1)), hv_Column.TupleSelect( hv_Indices.TupleSelect((new HTuple(hv_Indices.TupleLength())) - 1))); } } stopwatch.Stop(); RunStatus = new RunStatus(stopwatch.Elapsed.TotalMilliseconds); outputs = new ItemCollection(Outputs); } catch (Exception ex) { stopwatch.Stop(); RunStatus = new RunStatus(stopwatch.Elapsed.TotalMilliseconds, EResult.Error, ex.Message, ex); throw; } finally { HOperatorSet.SetSystem("flush_graphic", "true"); //释放本次运行的临时资源 hImage.Dispose(); ho_GrayImage.Dispose(); ho_Regions.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_SelectedRegions1.Dispose(); ho_Cross1.Dispose(); ho_Cross2.Dispose(); } }
public bool FindMark(string FileName, out double centerRow, out double centerCol, out double angle) { bool result = false; if (hv_ModelID == null) { GenMarkModel(out hv_ModelID, out ho_ModelContours, out hv_MetrologyHandle); } HObject ho_Image; HObject ho_ResultContours = null, ho_Contour = null; HObject ho_UsedEdges = null, ho_Cross = null; HTuple hv_RowFound = new HTuple(), hv_ColFound = new HTuple(), hv_AngleFound = new HTuple(); HTuple hv_ScoreFound = new HTuple(); HTuple hv_HomMat2D = null; HTuple hv_Column = null, hv_Row = null; HTuple hv_UsedColumn = new HTuple(), hv_UsedRow = new HTuple(); HTuple hv_Angle = new HTuple(), hv_Degree = new HTuple(); HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_ResultContours); HOperatorSet.GenEmptyObj(out ho_Contour); HOperatorSet.GenEmptyObj(out ho_UsedEdges); HOperatorSet.GenEmptyObj(out ho_Cross); ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, FileName); HOperatorSet.FindShapeModel(ho_Image, hv_ModelID, (new HTuple(-10)).TupleRad() , (new HTuple(20)).TupleRad(), 0.3, 1, 0.5, "least_squares", 0, 0.75, out hv_RowFound, out hv_ColFound, out hv_AngleFound, out hv_ScoreFound); if ((int)(new HTuple((new HTuple(1)).TupleEqual(new HTuple(hv_RowFound.TupleLength() )))) != 0) { HOperatorSet.HomMat2dIdentity(out hv_HomMat2D); HOperatorSet.HomMat2dRotate(hv_HomMat2D, hv_AngleFound, 0, 0, out hv_HomMat2D); HOperatorSet.HomMat2dTranslate(hv_HomMat2D, hv_RowFound - 0, hv_ColFound - 0, out hv_HomMat2D); ho_ResultContours.Dispose(); HOperatorSet.AffineTransContourXld(ho_ModelContours, out ho_ResultContours, hv_HomMat2D); HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red"); HOperatorSet.DispObj(ho_ResultContours, hv_ExpDefaultWinHandle); HOperatorSet.AlignMetrologyModel(hv_MetrologyHandle, hv_RowFound, hv_ColFound, hv_AngleFound); //应用测量 HOperatorSet.ApplyMetrologyModel(ho_Image, hv_MetrologyHandle); //获取结果 ho_Contour.Dispose(); HOperatorSet.GetMetrologyObjectMeasures(out ho_Contour, hv_MetrologyHandle, "all", "all", out hv_Row, out hv_Column); HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "used_edges", "row", out hv_UsedRow); HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, "all", "all", "used_edges", "column", out hv_UsedColumn); ho_UsedEdges.Dispose(); HOperatorSet.GenCrossContourXld(out ho_UsedEdges, hv_UsedRow, hv_UsedColumn, 10, (new HTuple(45)).TupleRad()); ho_ResultContours.Dispose(); HOperatorSet.GetMetrologyObjectResultContour(out ho_ResultContours, hv_MetrologyHandle, "all", "all", 1.5); ho_Cross.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross, hv_RowFound, hv_ColFound, 40, hv_AngleFound); //HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); //HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); //HOperatorSet.DispObj(ho_Cross, hv_ExpDefaultWinHandle); //HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); //HOperatorSet.DispObj(ho_ResultContours, hv_ExpDefaultWinHandle); HOperatorSet.AngleLx(hv_UsedRow.TupleSelect(1), hv_UsedColumn.TupleSelect( 1), hv_UsedRow.TupleSelect(6), hv_UsedColumn.TupleSelect(6), out hv_Angle); HOperatorSet.TupleDeg(hv_Angle, out hv_Degree); //disp_message(hv_ExpDefaultWinHandle, "定位成功!", "window", 12, 12, "black", // "true"); disp_message(hv_ExpDefaultWinHandle, ((((((new HTuple("mark位置:") + "[ ") + hv_RowFound) + new HTuple(",")) + hv_ColFound) + new HTuple(",")) + hv_Degree) + "° ]", "window", 32, 12, "black", "true"); //相对模板角度 //HOperatorSet.TupleDeg(hv_AngleFound, out hv_Degree); //disp_message(hv_ExpDefaultWinHandle, ("相对模板角度为" + hv_Degree) + "°", "window", // 52, 12, "black", "true"); centerRow = hv_RowFound; centerCol = hv_ColFound; angle = hv_Degree; result = true; } else { disp_message(hv_ExpDefaultWinHandle, "未识别到靶标", "window", hv_Row, hv_Column, "black", "true"); centerRow = centerCol = angle = 0; } ho_Image.Dispose(); ho_ResultContours.Dispose(); ho_Contour.Dispose(); ho_UsedEdges.Dispose(); ho_Cross.Dispose(); return(result); }
private void action() { // Stack for temporary objects HObject[] OTemp = new HObject[20]; // Local iconic variables HObject ho_Rectangle, ho_ImageReduced; HObject ho_Region, ho_ConnectedRegions, ho_SelectedRegions; HObject ho_Rectangle1, ho_ImageRotate, ho_Region1, ho_RegionAffineTrans; HObject ho_Region2, ho_RegionUnionu, ho_RegionUniond, ho_Contours; HObject ho_SelectedContours, ho_SmoothedContours1, ho_ClippedContoursu; HObject ho_ClippedContoursd, ho_Rectangle2, ho_ClippedContoursuObjectSelected = null; HObject ho_ClippedContoursdObjectSelected = null, ho_Circle = null; HObject ho_Circle1 = null, ho_ConnectedRegions1, ho_SelectedRegions1; HObject ho_SortedRegionssdj, ho_Region3, ho_ConnectedRegions2; HObject ho_SelectedRegions2, ho_SortedRegionsxdj, ho_RegionLines = null; HObject ho_RegionAffineTrans1; // Local control variables HTuple hv_Width = null, hv_Height = null; HTuple hv_jd = null, hv_Row = null, hv_Column = null, hv_Phi = null; HTuple hv_Length1 = null, hv_Length2 = null, hv_HomMat2DIdentity = null; HTuple hv_HomMat2DRotate = null, hv_Row1 = null, hv_Column1 = null; HTuple hv_Row2 = null, hv_Column2 = null, hv_Rown = null; HTuple hv_Rowu = null, hv_Columnu = null, hv_Phiu = null; HTuple hv_Length1u = null, hv_Length2u = null, hv_MeasureHandleu = null; HTuple hv_GrayValuesu = null, hv_Sigmau = null, hv_Functionu = null; HTuple hv_SmoothedFunctionu = null, hv_FirstDerivativeu = null; HTuple hv_ZeroCrossingsu = null, hv_RowStartu = null, hv_ColStartu = null; HTuple hv_RowEndu = null, hv_ColEndu = null, hv_iu = null; HTuple hv_Rowd = null, hv_Columnd = null, hv_Phid = null; HTuple hv_Length1d = null, hv_Length2d = null, hv_MeasureHandled = null; HTuple hv_GrayValuesd = null, hv_Sigmad = null, hv_Functiond = null; HTuple hv_SmoothedFunctiond = null, hv_FirstDerivatived = null; HTuple hv_ZeroCrossingsd = null, hv_RowStartd = null, hv_ColStartd = null; HTuple hv_RowEndd = null, hv_ColEndd = null, hv_id = null; HTuple hv_RowIntuAll = null, hv_ColumnIntuAll = null, hv_u1 = null; HTuple hv_i = null, hv_Length = new HTuple(), hv_Indices1 = new HTuple(); HTuple hv_RowIntu = new HTuple(), hv_ColumnIntu = new HTuple(); HTuple hv_IsOverlappingu = new HTuple(), hv_RowIntdAll = null; HTuple hv_ColumnIntdAll = null, hv_j = null, hv_RowIntd = new HTuple(); HTuple hv_ColumnIntd = new HTuple(), hv_IsOverlappingd = new HTuple(); HTuple hv_Sortedu = null, hv_Sortedd = null, hv_RowIntuu = null; HTuple hv_ColumnIntuu = null, hv_RowIntud = null, hv_ColumnIntud = null; HTuple hv_uu = null, hv_ud = null, hv_ub1 = null, hv_RowIntdu = null; HTuple hv_ColumnIntdu = null, hv_RowIntdd = null, hv_ColumnIntdd = null; HTuple hv_du = null, hv_dd = null, hv_db1 = null, hv_Areasdj = null; HTuple hv_Rowsdj = null, hv_Columnsdj = null, hv_sdj = null; HTuple hv_Areaxdj = null, hv_Rowxdj = null, hv_Columnxdj = null; HTuple hv_xdj = null, hv_djsl1 = null, hv_djsl = null; HTuple hv_Mean1 = null, hv_Mean2 = null, hv_Mean3 = null; HTuple hv_Mean4 = null, hv_Columnuu = null, hv_Columndd = null; HTuple hv_AngleAll = null, hv_l = null, hv_Angle = new HTuple(); HTuple hv_DegAll = null, hv_Mean5 = null, hv_Mean8 = null; HTuple hv_Columnuum = null, hv_uum1 = null, hv_uus = null; HTuple hv_Columnddm = null, hv_ddm = null, hv_dds = null; HTuple hv_ColumnmArr = null, hv_Mean9 = null, hv_Mean10 = null; HTuple hv_Sorteda = null, hv_Ai = null, hv_Ax = null, hv_pixeldist = null; HTuple hv_L = null, hv_D = null, hv_Dmax = null, hv_Dmin = null; HTuple hv_d = null, hv_dmax = null, hv_dmin = null, hv_LS = null; HTuple hv_HomMat2DIdentity1 = null, hv_HomMat2DRotate1 = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Rectangle); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Region); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_Rectangle1); HOperatorSet.GenEmptyObj(out ho_ImageRotate); HOperatorSet.GenEmptyObj(out ho_Region1); HOperatorSet.GenEmptyObj(out ho_RegionAffineTrans); HOperatorSet.GenEmptyObj(out ho_Region2); HOperatorSet.GenEmptyObj(out ho_RegionUnionu); HOperatorSet.GenEmptyObj(out ho_RegionUniond); HOperatorSet.GenEmptyObj(out ho_Contours); HOperatorSet.GenEmptyObj(out ho_SelectedContours); HOperatorSet.GenEmptyObj(out ho_SmoothedContours1); HOperatorSet.GenEmptyObj(out ho_ClippedContoursu); HOperatorSet.GenEmptyObj(out ho_ClippedContoursd); HOperatorSet.GenEmptyObj(out ho_Rectangle2); HOperatorSet.GenEmptyObj(out ho_ClippedContoursuObjectSelected); HOperatorSet.GenEmptyObj(out ho_ClippedContoursdObjectSelected); HOperatorSet.GenEmptyObj(out ho_Circle); HOperatorSet.GenEmptyObj(out ho_Circle1); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_SortedRegionssdj); HOperatorSet.GenEmptyObj(out ho_Region3); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions2); HOperatorSet.GenEmptyObj(out ho_SelectedRegions2); HOperatorSet.GenEmptyObj(out ho_SortedRegionsxdj); HOperatorSet.GenEmptyObj(out ho_RegionLines); HOperatorSet.GenEmptyObj(out ho_RegionAffineTrans1); HOperatorSet.Union1(algorithm.Region, out RegionToDisp); try { HOperatorSet.GetImageSize(Image, out hv_Width, out hv_Height); ho_Rectangle.Dispose(); HOperatorSet.GenRectangle1(out ho_Rectangle, DRow1m, DCol1m, DRow2m, DCol2m); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(Image, ho_Rectangle, out ho_ImageReduced); //* ho_Region.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Region, 0, 128); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions, out ho_SelectedRegions, "max_area", 70); //hv_jd = 0; HOperatorSet.SmallestRectangle2(ho_SelectedRegions, out hv_Row, out hv_Column, out hv_Phi, out hv_Length1, out hv_Length2); ho_Rectangle1.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle1, hv_Row, hv_Column, hv_Phi, hv_Length1, hv_Length2); ho_ImageRotate.Dispose(); HOperatorSet.RotateImage(Image, out ho_ImageRotate, -(hv_Phi.TupleDeg()), "constant"); //* ho_Region1.Dispose(); HOperatorSet.Threshold(ho_ImageRotate, out ho_Region1, 0, 128); //* HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity); HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity, -hv_Phi, hv_Height / 2, hv_Width / 2, out hv_HomMat2DRotate); ho_RegionAffineTrans.Dispose(); HOperatorSet.AffineTransRegion(ho_Rectangle1, out ho_RegionAffineTrans, hv_HomMat2DRotate, "nearest_neighbor"); HOperatorSet.SmallestRectangle1(ho_RegionAffineTrans, out hv_Row1, out hv_Column1, out hv_Row2, out hv_Column2); //*** ho_Region2.Dispose(); HOperatorSet.GenRegionPoints(out ho_Region2, hv_Row1, hv_Column1); ho_RegionUnionu.Dispose(); HOperatorSet.Union1(ho_Region2, out ho_RegionUnionu); ho_RegionUniond.Dispose(); HOperatorSet.Union1(ho_Region2, out ho_RegionUniond); ho_Contours.Dispose(); HOperatorSet.GenContourRegionXld(ho_Region1, out ho_Contours, "border"); ho_SelectedContours.Dispose(); HOperatorSet.SelectContoursXld(ho_Contours, out ho_SelectedContours, "contour_length", 20, 2000000, -1, 1); ho_SmoothedContours1.Dispose(); HOperatorSet.SmoothContoursXld(ho_SelectedContours, out ho_SmoothedContours1, 13); //* hv_Rown = (hv_Row1 + hv_Row2) / 2; ho_ClippedContoursu.Dispose(); HOperatorSet.ClipContoursXld(ho_SmoothedContours1, out ho_ClippedContoursu, hv_Row1, hv_Column1, hv_Rown, hv_Column2); ho_ClippedContoursd.Dispose(); HOperatorSet.ClipContoursXld(ho_SmoothedContours1, out ho_ClippedContoursd, hv_Rown, hv_Column1, hv_Row2, hv_Column2); hv_Rowu = (hv_Row1 + hv_Rown) / 2; hv_Columnu = (hv_Column1 + hv_Column2) / 2; hv_Phiu = 0; hv_Length1u = (hv_Column2 - hv_Column1) / 2; hv_Length2u = (hv_Rown - hv_Row1) / 2; //dev_set_draw ('margin') HOperatorSet.GenMeasureRectangle2(hv_Rowu, hv_Columnu, hv_Phiu, hv_Length1u, hv_Length2u, hv_Width, hv_Height, "bilinear", out hv_MeasureHandleu); ho_Rectangle2.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle2, hv_Rowu, hv_Columnu, hv_Phiu, hv_Length1u, hv_Length2u); HOperatorSet.MeasureProjection(ho_ImageRotate, hv_MeasureHandleu, out hv_GrayValuesu); hv_Sigmau = 5; HOperatorSet.CreateFunct1dArray(hv_GrayValuesu, out hv_Functionu); HOperatorSet.SmoothFunct1dGauss(hv_Functionu, hv_Sigmau, out hv_SmoothedFunctionu); HOperatorSet.DerivateFunct1d(hv_SmoothedFunctionu, "first", out hv_FirstDerivativeu); HOperatorSet.ZeroCrossingsFunct1d(hv_FirstDerivativeu, out hv_ZeroCrossingsu); hv_RowStartu = hv_Rowu - (hv_Length2u / 2); hv_ColStartu = (hv_Columnu - hv_Length1u) + hv_ZeroCrossingsu; hv_RowEndu = hv_Rowu + (hv_Length2u / 2); hv_ColEndu = (hv_Columnu - hv_Length1u) + hv_ZeroCrossingsu; hv_iu = new HTuple(hv_ZeroCrossingsu.TupleLength()); hv_Rowd = (hv_Rown + hv_Row2) / 2; hv_Columnd = (hv_Column1 + hv_Column2) / 2; hv_Phid = 0; hv_Length1d = (hv_Column2 - hv_Column1) / 2; hv_Length2d = (hv_Row2 - hv_Rown) / 2; HOperatorSet.GenMeasureRectangle2(hv_Rowd, hv_Columnd, hv_Phid, hv_Length1d, hv_Length2d, hv_Width, hv_Height, "bilinear", out hv_MeasureHandled); HOperatorSet.MeasureProjection(ho_ImageRotate, hv_MeasureHandled, out hv_GrayValuesd); // hv_Sigmad = 5; HOperatorSet.CreateFunct1dArray(hv_GrayValuesd, out hv_Functiond); HOperatorSet.SmoothFunct1dGauss(hv_Functiond, hv_Sigmad, out hv_SmoothedFunctiond); HOperatorSet.DerivateFunct1d(hv_SmoothedFunctiond, "first", out hv_FirstDerivatived); HOperatorSet.ZeroCrossingsFunct1d(hv_FirstDerivatived, out hv_ZeroCrossingsd); HOperatorSet.CloseMeasure(hv_MeasureHandleu); HOperatorSet.CloseMeasure(hv_MeasureHandled); hv_RowStartd = hv_Rowd - (hv_Length2d / 2); hv_ColStartd = (hv_Columnd - hv_Length1d) + hv_ZeroCrossingsd; hv_RowEndd = hv_Rowd + (hv_Length2d / 2); hv_ColEndd = (hv_Columnd - hv_Length1d) + hv_ZeroCrossingsd; hv_id = new HTuple(hv_ZeroCrossingsd.TupleLength()); hv_RowIntuAll = new HTuple(); hv_ColumnIntuAll = new HTuple(); hv_u1 = 0; HTuple end_val96 = hv_iu - 1; HTuple step_val96 = 1; for (hv_i = 0; hv_i.Continue(end_val96, step_val96); hv_i = hv_i.TupleAdd(step_val96)) { HOperatorSet.LengthXld(ho_ClippedContoursu, out hv_Length); HOperatorSet.TupleFind(hv_Length, hv_Length.TupleMax(), out hv_Indices1); ho_ClippedContoursuObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_ClippedContoursu, out ho_ClippedContoursuObjectSelected, hv_Indices1 + 1); HOperatorSet.IntersectionLineContourXld(ho_ClippedContoursuObjectSelected, hv_RowStartu, hv_ColStartu.TupleSelect(hv_i), hv_RowEndu, hv_ColEndu.TupleSelect( hv_i), out hv_RowIntu, out hv_ColumnIntu, out hv_IsOverlappingu); //*gen_cross_contour_xld (Cross1u, RowIntu, ColumnIntu, 12, 0)//根据每个输入点交叉的形状创键一个XLD轮廓(contour) hv_RowIntuAll = hv_RowIntuAll.TupleConcat(hv_RowIntu); hv_ColumnIntuAll = hv_ColumnIntuAll.TupleConcat(hv_ColumnIntu); hv_u1 = hv_u1 + 1; } hv_RowIntdAll = new HTuple(); hv_ColumnIntdAll = new HTuple(); HTuple end_val109 = hv_id - 1; HTuple step_val109 = 1; for (hv_j = 0; hv_j.Continue(end_val109, step_val109); hv_j = hv_j.TupleAdd(step_val109)) { HOperatorSet.LengthXld(ho_ClippedContoursd, out hv_Length); HOperatorSet.TupleFind(hv_Length, hv_Length.TupleMax(), out hv_Indices1); ho_ClippedContoursdObjectSelected.Dispose(); HOperatorSet.SelectObj(ho_ClippedContoursd, out ho_ClippedContoursdObjectSelected, hv_Indices1 + 1); HOperatorSet.IntersectionLineContourXld(ho_ClippedContoursdObjectSelected, hv_RowStartd, hv_ColStartd.TupleSelect(hv_j), hv_RowEndd, hv_ColEndd.TupleSelect( hv_j), out hv_RowIntd, out hv_ColumnIntd, out hv_IsOverlappingd); //*gen_cross_contour_xld (Cross1d, RowIntd, ColumnIntd, 12, 0)//根据每个输入点交叉的形状创键一个XLD轮廓(contour) hv_RowIntdAll = hv_RowIntdAll.TupleConcat(hv_RowIntd); hv_ColumnIntdAll = hv_ColumnIntdAll.TupleConcat(hv_ColumnIntd); } //stop () HOperatorSet.TupleSort(hv_RowIntuAll, out hv_Sortedu); HOperatorSet.TupleSort(hv_RowIntdAll, out hv_Sortedd); hv_RowIntuu = new HTuple(); hv_ColumnIntuu = new HTuple(); hv_RowIntud = new HTuple(); hv_ColumnIntud = new HTuple(); hv_uu = 0; hv_ud = 0; for (hv_ub1 = 0; (int)hv_ub1 <= (int)((new HTuple(hv_Sortedu.TupleLength())) - 1); hv_ub1 = (int)hv_ub1 + 1) { if ((int)(new HTuple(((hv_RowIntuAll.TupleSelect(hv_ub1))).TupleLess(((((hv_Sortedu.TupleSelect( 0)) * 2) + (hv_Sortedu.TupleSelect((new HTuple(hv_Sortedu.TupleLength())) - 1))) / 3) + 3))) != 0) { hv_RowIntuu = hv_RowIntuu.TupleConcat(hv_RowIntuAll.TupleSelect(hv_ub1)); hv_ColumnIntuu = hv_ColumnIntuu.TupleConcat(hv_ColumnIntuAll.TupleSelect( hv_ub1)); ho_Circle.Dispose(); HOperatorSet.GenCircle(out ho_Circle, hv_RowIntuAll.TupleSelect(hv_ub1), hv_ColumnIntuAll.TupleSelect(hv_ub1), 1); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_RegionUnionu, ho_Circle, out ExpTmpOutVar_0); ho_RegionUnionu.Dispose(); ho_RegionUnionu = ExpTmpOutVar_0; } hv_uu = hv_uu + 1; } if ((int)(new HTuple(((hv_RowIntuAll.TupleSelect(hv_ub1))).TupleGreater((((hv_Sortedu.TupleSelect( 0)) + ((hv_Sortedu.TupleSelect((new HTuple(hv_Sortedu.TupleLength())) - 1)) * 2)) / 3) - 3))) != 0) { hv_RowIntud = hv_RowIntud.TupleConcat(hv_RowIntuAll.TupleSelect(hv_ub1)); hv_ColumnIntud = hv_ColumnIntud.TupleConcat(hv_ColumnIntuAll.TupleSelect( hv_ub1)); hv_ud = hv_ud + 1; } } //stop () hv_RowIntdu = new HTuple(); hv_ColumnIntdu = new HTuple(); hv_RowIntdd = new HTuple(); hv_ColumnIntdd = new HTuple(); hv_du = 0; hv_dd = 0; for (hv_db1 = 0; (int)hv_db1 <= (int)((new HTuple(hv_Sortedd.TupleLength())) - 1); hv_db1 = (int)hv_db1 + 1) { if ((int)(new HTuple(((hv_RowIntdAll.TupleSelect(hv_db1))).TupleLess(((((hv_Sortedd.TupleSelect( 0)) * 2) + (hv_Sortedd.TupleSelect((new HTuple(hv_Sortedd.TupleLength())) - 1))) / 3) + 3))) != 0) { hv_RowIntdu = hv_RowIntdu.TupleConcat(hv_RowIntdAll.TupleSelect(hv_db1)); hv_ColumnIntdu = hv_ColumnIntdu.TupleConcat(hv_ColumnIntdAll.TupleSelect( hv_db1)); hv_du = hv_du + 1; } if ((int)(new HTuple(((hv_RowIntdAll.TupleSelect(hv_db1))).TupleGreater((((hv_Sortedd.TupleSelect( 0)) + ((hv_Sortedd.TupleSelect((new HTuple(hv_Sortedd.TupleLength())) - 1)) * 2)) / 3) - 3))) != 0) { hv_RowIntdd = hv_RowIntdd.TupleConcat(hv_RowIntdAll.TupleSelect(hv_db1)); hv_ColumnIntdd = hv_ColumnIntdd.TupleConcat(hv_ColumnIntdAll.TupleSelect( hv_db1)); ho_Circle1.Dispose(); HOperatorSet.GenCircle(out ho_Circle1, hv_RowIntdAll.TupleSelect(hv_db1), hv_ColumnIntdAll.TupleSelect(hv_db1), 1); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_RegionUniond, ho_Circle1, out ExpTmpOutVar_0); ho_RegionUniond.Dispose(); ho_RegionUniond = ExpTmpOutVar_0; } hv_dd = hv_dd + 1; } } ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_RegionUnionu, out ho_ConnectedRegions1); ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions1, out ho_SelectedRegions1, "area", "and", 3, 99999); ho_SortedRegionssdj.Dispose(); HOperatorSet.SortRegion(ho_SelectedRegions1, out ho_SortedRegionssdj, "upper_left", "true", "column"); HOperatorSet.AreaCenter(ho_SortedRegionssdj, out hv_Areasdj, out hv_Rowsdj, out hv_Columnsdj); ho_Region3.Dispose(); HOperatorSet.GenRegionPoints(out ho_Region3, hv_Rowsdj.TupleSelect(0), hv_Columnsdj.TupleSelect( 0)); hv_sdj = new HTuple(hv_Columnsdj.TupleLength()); ho_ConnectedRegions2.Dispose(); HOperatorSet.Connection(ho_RegionUniond, out ho_ConnectedRegions2); ho_SelectedRegions2.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions2, out ho_SelectedRegions2, "area", "and", 3, 99999); ho_SortedRegionsxdj.Dispose(); HOperatorSet.SortRegion(ho_SelectedRegions2, out ho_SortedRegionsxdj, "upper_left", "true", "column"); HOperatorSet.AreaCenter(ho_SortedRegionsxdj, out hv_Areaxdj, out hv_Rowxdj, out hv_Columnxdj); hv_xdj = new HTuple(hv_Columnxdj.TupleLength()); hv_djsl1 = new HTuple(); hv_djsl1 = hv_djsl1.TupleConcat(hv_sdj); hv_djsl1 = hv_djsl1.TupleConcat(hv_xdj); HOperatorSet.TupleMin(hv_djsl1, out hv_djsl); HOperatorSet.TupleMean(hv_RowIntuu, out hv_Mean1); HOperatorSet.TupleMean(hv_RowIntud, out hv_Mean2); HOperatorSet.TupleMean(hv_RowIntdu, out hv_Mean3); HOperatorSet.TupleMean(hv_RowIntdd, out hv_Mean4); hv_Columnuu = new HTuple(); hv_Columndd = new HTuple(); hv_AngleAll = new HTuple(); HTuple end_val191 = hv_djsl - 1; HTuple step_val191 = 1; for (hv_l = 0; hv_l.Continue(end_val191, step_val191); hv_l = hv_l.TupleAdd(step_val191)) { ho_RegionLines.Dispose(); HOperatorSet.GenRegionLine(out ho_RegionLines, hv_Rowsdj.TupleSelect(hv_l), hv_Columnsdj.TupleSelect(hv_l), hv_Rowxdj.TupleSelect(hv_l), hv_Columnxdj.TupleSelect( hv_l)); { HObject ExpTmpOutVar_0; HOperatorSet.Union2(ho_Region3, ho_RegionLines, out ExpTmpOutVar_0); ho_Region3.Dispose(); ho_Region3 = ExpTmpOutVar_0; } HOperatorSet.AngleLx(hv_Rowsdj.TupleSelect(hv_l), hv_Columnsdj.TupleSelect( hv_l), hv_Rowxdj.TupleSelect(hv_l), hv_Columnxdj.TupleSelect(hv_l), out hv_Angle); hv_AngleAll = hv_AngleAll.TupleConcat(0 - hv_Angle); if ((int)(new HTuple(hv_l.TupleGreater(0))) != 0) { hv_Columnuu = hv_Columnuu.TupleConcat((hv_ColumnIntuu.TupleSelect(hv_l)) - (hv_ColumnIntuu.TupleSelect( hv_l - 1))); hv_Columndd = hv_Columndd.TupleConcat((hv_ColumnIntdd.TupleSelect(hv_l)) - (hv_ColumnIntdd.TupleSelect( hv_l - 1))); } //stop () } HOperatorSet.TupleDeg(hv_AngleAll, out hv_DegAll); HOperatorSet.TupleMean(hv_Columnuu, out hv_Mean5); HOperatorSet.TupleMean(hv_Columndd, out hv_Mean8); //**过滤 hv_Columnuum = new HTuple(); hv_uum1 = 0; for (hv_uus = 0; (int)hv_uus <= (int)((new HTuple(hv_Columnuu.TupleLength())) - 1); hv_uus = (int)hv_uus + 1) { if ((int)((new HTuple(hv_Columnuu.TupleLessEqual(hv_Mean5 * 1.1))).TupleAnd(new HTuple(hv_Columnuu.TupleGreaterEqual( hv_Mean5 * 0.9)))) != 0) { hv_Columnuum = hv_Columnuum.TupleConcat(hv_Columnuu.TupleSelect(hv_uus)); hv_uum1 = hv_uum1 + 1; } } hv_Columnddm = new HTuple(); hv_ddm = 0; for (hv_dds = 0; (int)hv_dds <= (int)((new HTuple(hv_Columndd.TupleLength())) - 1); hv_dds = (int)hv_dds + 1) { if ((int)((new HTuple(hv_Columndd.TupleLessEqual(hv_Mean8 * 1.1))).TupleAnd(new HTuple(hv_Columndd.TupleGreaterEqual( hv_Mean8 * 0.9)))) != 0) { hv_Columnddm = hv_Columnddm.TupleConcat(hv_Columndd.TupleSelect(hv_dds)); hv_ddm = hv_ddm + 1; } } hv_ColumnmArr = new HTuple(); hv_ColumnmArr = hv_ColumnmArr.TupleConcat(hv_Columnuum); hv_ColumnmArr = hv_ColumnmArr.TupleConcat(hv_Columnddm); HOperatorSet.TupleMean(hv_ColumnmArr, out hv_Mean9); HOperatorSet.TupleMean(hv_DegAll, out hv_Mean10); HOperatorSet.TupleSort(hv_DegAll, out hv_Sorteda); hv_Ai = hv_Sorteda[0]; hv_Ax = hv_Sorteda[(new HTuple(hv_Sorteda.TupleLength())) - 1]; hv_pixeldist = 1; hv_L = ((hv_Columnsdj.TupleSelect(hv_djsl - 1)) - (hv_Columnsdj.TupleSelect(0))) * hv_pixeldist; hv_D = (hv_Mean4 - hv_Mean1) * hv_pixeldist; hv_Dmax = ((hv_RowIntdd.TupleMax()) - (hv_RowIntuu.TupleMin())) * hv_pixeldist; hv_Dmin = ((hv_RowIntdd.TupleMin()) - (hv_RowIntuu.TupleMax())) * hv_pixeldist; hv_d = (hv_Mean3 - hv_Mean2) * hv_pixeldist; hv_dmax = ((hv_RowIntdu.TupleMax()) - (hv_RowIntud.TupleMin())) * hv_pixeldist; hv_dmin = ((hv_RowIntdu.TupleMin()) - (hv_RowIntud.TupleMax())) * hv_pixeldist; hv_LS = hv_Mean9 * hv_pixeldist; //*区域旋转 HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity1); HOperatorSet.HomMat2dRotate(hv_HomMat2DIdentity1, hv_Phi, hv_Height / 2, hv_Width / 2, out hv_HomMat2DRotate1); ho_RegionAffineTrans1.Dispose(); HOperatorSet.AffineTransRegion(ho_Region3, out ho_RegionAffineTrans1, hv_HomMat2DRotate1, "nearest_neighbor"); HOperatorSet.Union1(ho_RegionAffineTrans1, out RegionToDisp); HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("齿数"); hv_result = hv_result.TupleConcat(hv_djsl.D); hv_result = hv_result.TupleConcat("螺纹长度"); hv_result = hv_result.TupleConcat(hv_L.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹大径平均值"); hv_result = hv_result.TupleConcat(hv_D.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹大径最小值"); hv_result = hv_result.TupleConcat(hv_Dmin.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹大径最大值"); hv_result = hv_result.TupleConcat(hv_Dmax.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹小径平均值"); hv_result = hv_result.TupleConcat(hv_d.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹小径最小值"); hv_result = hv_result.TupleConcat(hv_dmin.D * pixeldist); hv_result = hv_result.TupleConcat("螺纹小径最大值"); hv_result = hv_result.TupleConcat(hv_dmax.D * pixeldist); hv_result = hv_result.TupleConcat("牙距"); hv_result = hv_result.TupleConcat(hv_LS.D * pixeldist); hv_result = hv_result.TupleConcat("平均牙倾角"); hv_result = hv_result.TupleConcat(hv_Mean10.D); hv_result = hv_result.TupleConcat("最小牙倾角"); hv_result = hv_result.TupleConcat(hv_Ai.D); hv_result = hv_result.TupleConcat("最大牙倾角"); hv_result = hv_result.TupleConcat(hv_Ax.D); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_Rectangle1.Dispose(); ho_ImageRotate.Dispose(); ho_Region1.Dispose(); ho_RegionAffineTrans.Dispose(); ho_Region2.Dispose(); ho_RegionUnionu.Dispose(); ho_RegionUniond.Dispose(); ho_Contours.Dispose(); ho_SelectedContours.Dispose(); ho_SmoothedContours1.Dispose(); ho_ClippedContoursu.Dispose(); ho_ClippedContoursd.Dispose(); ho_Rectangle2.Dispose(); ho_ClippedContoursuObjectSelected.Dispose(); ho_ClippedContoursdObjectSelected.Dispose(); ho_Circle.Dispose(); ho_Circle1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_SortedRegionssdj.Dispose(); ho_Region3.Dispose(); ho_ConnectedRegions2.Dispose(); ho_SelectedRegions2.Dispose(); ho_SortedRegionsxdj.Dispose(); ho_RegionLines.Dispose(); ho_RegionAffineTrans1.Dispose(); algorithm.Region.Dispose(); } catch { HTuple hv_result = GetHv_result(); hv_result = hv_result.TupleConcat("齿数"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺纹长度"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺纹大径平均值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺纹大径最小值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺纹大径最大值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺纹小径平均值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺纹小径最小值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("螺纹小径最大值"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("牙距"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("平均牙倾角"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("最小牙倾角"); hv_result = hv_result.TupleConcat(0); hv_result = hv_result.TupleConcat("最大牙倾角"); hv_result = hv_result.TupleConcat(0); result = hv_result.Clone(); ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_Rectangle1.Dispose(); ho_ImageRotate.Dispose(); ho_Region1.Dispose(); ho_RegionAffineTrans.Dispose(); ho_Region2.Dispose(); ho_RegionUnionu.Dispose(); ho_RegionUniond.Dispose(); ho_Contours.Dispose(); ho_SelectedContours.Dispose(); ho_SmoothedContours1.Dispose(); ho_ClippedContoursu.Dispose(); ho_ClippedContoursd.Dispose(); ho_Rectangle2.Dispose(); ho_ClippedContoursuObjectSelected.Dispose(); ho_ClippedContoursdObjectSelected.Dispose(); ho_Circle.Dispose(); ho_Circle1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_SortedRegionssdj.Dispose(); ho_Region3.Dispose(); ho_ConnectedRegions2.Dispose(); ho_SelectedRegions2.Dispose(); ho_SortedRegionsxdj.Dispose(); ho_RegionLines.Dispose(); ho_RegionAffineTrans1.Dispose(); algorithm.Region.Dispose(); } finally { ho_Rectangle.Dispose(); ho_ImageReduced.Dispose(); ho_Region.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions.Dispose(); ho_Rectangle1.Dispose(); ho_ImageRotate.Dispose(); ho_Region1.Dispose(); ho_RegionAffineTrans.Dispose(); ho_Region2.Dispose(); ho_RegionUnionu.Dispose(); ho_RegionUniond.Dispose(); ho_Contours.Dispose(); ho_SelectedContours.Dispose(); ho_SmoothedContours1.Dispose(); ho_ClippedContoursu.Dispose(); ho_ClippedContoursd.Dispose(); ho_Rectangle2.Dispose(); ho_ClippedContoursuObjectSelected.Dispose(); ho_ClippedContoursdObjectSelected.Dispose(); ho_Circle.Dispose(); ho_Circle1.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions1.Dispose(); ho_SortedRegionssdj.Dispose(); ho_Region3.Dispose(); ho_ConnectedRegions2.Dispose(); ho_SelectedRegions2.Dispose(); ho_SortedRegionsxdj.Dispose(); ho_RegionLines.Dispose(); ho_RegionAffineTrans1.Dispose(); algorithm.Region.Dispose(); } }
public void Run(SoftwareRunState softwareRunState) { Stopwatch sw = new Stopwatch(); sw.Restart(); HTuple homMat2DArrow = null; HObject arrow = null, arrowTrans = null; HObject drawLine = null, imageReducedLine; if (inputImage == null) { FormFindLine.Instance.SetToolStatus("工具输入图像为空", ToolRunStatu.Not_Input_Image); return; } try { UpdateImage(); if (inputPose != null) { HTuple Row = inputPose.X - templatePose.X; HTuple Col = inputPose.Y - templatePose.Y; HTuple angle = inputPose.U - templatePose.U; HTuple _homMat2D; HOperatorSet.HomMat2dIdentity(out _homMat2D); HOperatorSet.HomMat2dRotate(_homMat2D, (HTuple)(angle), (HTuple)templatePose.X, (HTuple)templatePose.Y, out _homMat2D); HOperatorSet.HomMat2dTranslate(_homMat2D, (HTuple)(Row), (HTuple)(Col), out _homMat2D); //对预期线的起始点做放射变换 HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineStartRow, (HTuple)expectLineStartCol, out newExpectLineStartRow, out newExpectLineStartCol); HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineEndRow, (HTuple)expectLineEndCol, out newExpectLineEndRow, out newExpectLineEndCol); } else { newExpectLineStartRow = expectLineStartRow; newExpectLineStartCol = expectLineStartCol; newExpectLineEndRow = expectLineEndRow; newExpectLineEndCol = expectLineEndCol; } HTuple handleID; HOperatorSet.CreateMetrologyModel(out handleID); HTuple width, height; HOperatorSet.GetImageSize(inputImage, out width, out height); HOperatorSet.SetMetrologyModelImageSize(handleID, width[0], height[0]); HTuple index; HOperatorSet.AddMetrologyObjectLineMeasure(handleID, newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol, new HTuple(50), new HTuple(20), new HTuple(1), new HTuple(30), new HTuple(), new HTuple(), out index); //参数在这里设置 HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_transition"), new HTuple(polarity)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("num_measures"), new HTuple(cliperNum)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length1"), new HTuple(length)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length2"), new HTuple(weidth)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_threshold"), new HTuple(threshold)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_select"), new HTuple(edgeSelect)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_sigma"), new HTuple(sigma)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("min_score"), new HTuple(minScore)); HOperatorSet.ApplyMetrologyModel(inputImage, handleID); //显示所有卡尺 HTuple pointRow, pointCol; HOperatorSet.GetMetrologyObjectMeasures(out contoursDisp, handleID, new HTuple("all"), new HTuple("all"), out pointRow, out pointCol); //显示指示找线方向的箭头 #region 测试箭头 HTuple arrowRow = null, arrowColumn = null; HOperatorSet.GenRegionLine(out drawLine, newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol); HOperatorSet.ReduceDomain(inputImage, drawLine, out imageReducedLine); HOperatorSet.GetRegionPoints(imageReducedLine, out arrowRow, out arrowColumn); if (arrowRow.Length < 200) { CommonMethods.CommonMethods.gen_arrow_contour_xld(out arrow, arrowRow[0], arrowColumn[0], arrowRow[arrowRow.Length - 1], arrowColumn[arrowRow.Length - 1], 20, 20); } else { CommonMethods.CommonMethods.gen_arrow_contour_xld(out arrow, arrowRow[0], arrowColumn[0], arrowRow[200], arrowColumn[200], 20, 20); } HOperatorSet.VectorAngleToRigid(newExpectLineStartRow, newExpectLineStartCol, 0, (newExpectLineStartRow + newExpectLineEndRow) / 2, (newExpectLineStartCol + newExpectLineEndCol) / 2, new HTuple(-90).TupleRad(), out homMat2DArrow); HOperatorSet.AffineTransContourXld(arrow, out arrowDisp, homMat2DArrow); #endregion //把点显示出来 HOperatorSet.GenCrossContourXld(out crossDisp, pointRow, pointCol, new HTuple(12), new HTuple(0)); //得到所找到的线 HTuple parameter; HOperatorSet.GetMetrologyObjectResult(handleID, new HTuple("all"), new HTuple("all"), new HTuple("result_type"), new HTuple("all_param"), out parameter); HOperatorSet.GetMetrologyObjectResultContour(out LineDisp, handleID, new HTuple("all"), new HTuple("all"), new HTuple(1.5)); if (parameter.Length >= 4) { ResultLineStartRow = parameter[0]; ResultLineStartCol = parameter[1]; ResultLineEndRow = parameter[2]; ResultLineEndCol = parameter[3]; Point start = new Point() { Row = ResultLineStartRow, Col = ResultLineStartCol }; Point end = new Point() { Row = ResultLineEndRow, Col = ResultLineEndCol }; resultLine = new Line() { StartPoint = start, EndPoint = end }; } HOperatorSet.AngleLx(ResultLineStartRow, ResultLineStartCol, ResultLineEndRow, ResultLineEndCol, out _angle); if (softwareRunState == SoftwareRunState.Debug) { DispMainWindow(FormFindLine.Instance.myHwindow); FormFindLine.Instance.tbx_resultStartRow.Text = ResultLineStartRow.ToString(); FormFindLine.Instance.tbx_resultStartCol.Text = ResultLineEndCol.ToString(); FormFindLine.Instance.tbx_resultEndRow.Text = ResultLineEndRow.ToString(); FormFindLine.Instance.tbx_resultEndCol.Text = ResultLineEndCol.ToString(); } HOperatorSet.ClearMetrologyModel(handleID); // 参数传递 ParamsTrans(); sw.Stop(); runTime = $"运行时间: {sw.ElapsedMilliseconds} ms"; FormFindLine.Instance.SetToolStatus("工具运行成功!", ToolRunStatu.Succeed); } catch (Exception ex) { FormFindLine.Instance.SetToolStatus($"工具运行异常,异常原因: {ex}", ToolRunStatu.Tool_Run_Error); } finally { //homMat2DArrow.Dispose(); //arrow.Dispose(); //arrowTrans.Dispose(); } }
public void Run() { if (inputImage == null) { FormFindLine.Instance.TextBoxMessageDisp("图像为空", System.Drawing.Color.Red); toolRunStatu = ToolRunStatu.Not_Input_Image; return; } try { UpdateImage(); if (inputPose != null) { HTuple Row = inputPose.X - templatePose.X; HTuple Col = inputPose.Y - templatePose.Y; HTuple angle = inputPose.U - templatePose.U; HTuple _homMat2D; HOperatorSet.HomMat2dIdentity(out _homMat2D); HOperatorSet.HomMat2dRotate(_homMat2D, (HTuple)(angle), (HTuple)templatePose.X, (HTuple)templatePose.Y, out _homMat2D); HOperatorSet.HomMat2dTranslate(_homMat2D, (HTuple)(Row), (HTuple)(Col), out _homMat2D); //对预期线的起始点做放射变换 HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineStartRow, (HTuple)expectLineStartCol, out newExpectLineStartRow, out newExpectLineStartCol); HOperatorSet.AffineTransPixel(_homMat2D, (HTuple)expectLineEndRow, (HTuple)expectLineEndCol, out newExpectLineEndRow, out newExpectLineEndCol); } else { newExpectLineStartRow = expectLineStartRow; newExpectLineStartCol = expectLineStartCol; newExpectLineEndRow = expectLineEndRow; newExpectLineEndCol = expectLineEndCol; } HTuple handleID; HOperatorSet.CreateMetrologyModel(out handleID); HTuple width, height; HOperatorSet.GetImageSize(inputImage, out width, out height); HOperatorSet.SetMetrologyModelImageSize(handleID, width[0], height[0]); HTuple index; HOperatorSet.AddMetrologyObjectLineMeasure(handleID, newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol, new HTuple(50), new HTuple(20), new HTuple(1), new HTuple(30), new HTuple(), new HTuple(), out index); //参数在这里设置 HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_transition"), new HTuple(polarity)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("num_measures"), new HTuple(cliperNum)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length1"), new HTuple(length)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_length2"), new HTuple(weidth)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_threshold"), new HTuple(threshold)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_select"), new HTuple(edgeSelect)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("measure_sigma"), new HTuple(sigma)); HOperatorSet.SetMetrologyObjectParam(handleID, new HTuple("all"), new HTuple("min_score"), new HTuple(minScore)); HOperatorSet.ApplyMetrologyModel(inputImage, handleID); //显示所有卡尺 HTuple pointRow, pointCol; HOperatorSet.GetMetrologyObjectMeasures(out contours, handleID, new HTuple("all"), new HTuple("all"), out pointRow, out pointCol); HOperatorSet.SetColor(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, new HTuple("blue")); HOperatorSet.DispObj(contours, FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow); FormFindLine.Instance.myHwindow.DispObj(contours, "blue"); //显示指示找线方向的箭头 HTuple arrowAngle; HOperatorSet.AngleLx(newExpectLineStartRow, newExpectLineStartCol, newExpectLineEndRow, newExpectLineEndCol, out arrowAngle); #region 测试箭头 #endregion arrowAngle = arrowAngle + Math.PI / 2; arrowAngle = arrowAngle.TupleDeg(); HTuple row = (newExpectLineStartRow + newExpectLineEndRow) / 2; HTuple column = (newExpectLineStartCol + newExpectLineEndCol) / 2; double drow, dcolumn; double arrowLength = length + 100; if (0 <= arrowAngle && arrowAngle <= 90) { drow = Math.Abs(arrowLength * Math.Sin(((HTuple)arrowAngle).TupleRad())); dcolumn = Math.Abs(arrowLength * Math.Cos(((HTuple)arrowAngle).TupleRad())); HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row - drow, column + dcolumn, 5.0); } else if (arrowAngle > 90 && arrowAngle <= 180) { drow = arrowLength * Math.Sin(((HTuple)(180 - arrowAngle)).TupleRad()); dcolumn = arrowLength * Math.Cos(((HTuple)(180 - arrowAngle)).TupleRad()); HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row - drow, column - dcolumn, 5.0); } else if (arrowAngle < 0 && arrowAngle >= -90) { drow = arrowLength * Math.Sin(((HTuple)arrowAngle * -1).TupleRad()); dcolumn = arrowLength * Math.Cos(((HTuple)arrowAngle * -1).TupleRad()); HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row + drow, column + dcolumn, 5.0); } else if (arrowAngle < -90 && arrowAngle >= -180) { drow = Math.Abs(arrowLength * Math.Sin(((HTuple)arrowAngle + 180).TupleRad())); dcolumn = Math.Abs(arrowLength * Math.Cos(((HTuple)arrowAngle + 180).TupleRad())); HOperatorSet.DispArrow(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, row, column, row + drow, column - dcolumn, 5.0); } //把点显示出来 HObject cross; HOperatorSet.GenCrossContourXld(out cross, pointRow, pointCol, new HTuple(12), new HTuple(0)); HOperatorSet.SetColor(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, new HTuple("orange")); HOperatorSet.DispObj(cross, FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow); FormFindLine.Instance.myHwindow.DispObj(cross, "orange"); //得到所找到的线 HTuple parameter; HObject line; HOperatorSet.GetMetrologyObjectResult(handleID, new HTuple("all"), new HTuple("all"), new HTuple("result_type"), new HTuple("all_param"), out parameter); HOperatorSet.GetMetrologyObjectResultContour(out line, handleID, new HTuple("all"), new HTuple("all"), new HTuple(1.5)); if (parameter.Length >= 4) { ResultLineStartRow = parameter[0]; ResultLineStartCol = parameter[1]; ResultLineEndRow = parameter[2]; ResultLineEndCol = parameter[3]; Point start = new Point() { Row = ResultLineStartRow, Col = ResultLineStartCol }; Point end = new Point() { Row = ResultLineEndRow, Col = ResultLineEndCol }; resultLine = new Line() { StartPoint = start, EndPoint = end }; //显示找到的线 HOperatorSet.SetColor(FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow, new HTuple("green")); // HOperatorSet.DispObj(line, GetWindowHandle(jobName)); FormFindLine.Instance.myHwindow.hWindowControl.HalconWindow.SetLineWidth(2); FormFindLine.Instance.myHwindow.DispObj(line, "green"); } HOperatorSet.AngleLx(ResultLineStartRow, ResultLineStartCol, ResultLineEndRow, ResultLineEndCol, out _angle); HOperatorSet.ClearMetrologyModel(handleID); FormFindLine.Instance.tbx_resultStartRow.Text = ResultLineStartRow.ToString(); FormFindLine.Instance.tbx_resultStartCol.Text = ResultLineEndCol.ToString(); FormFindLine.Instance.tbx_resultEndRow.Text = ResultLineEndRow.ToString(); FormFindLine.Instance.tbx_resultEndCol.Text = ResultLineEndCol.ToString(); FormFindLine.Instance.TextBoxMessageDisp("运行成功", System.Drawing.Color.Green); toolRunStatu = ToolRunStatu.Succeed; } catch (Exception ex) { FormFindLine.Instance.TextBoxMessageDisp("工具运行异常" + ex.Message, System.Drawing.Color.Red); } }
/// <summary> /// 执行 /// </summary> /// <param name="image">图像</param> /// <param name="outputs">输出结果</param> /// <returns>执行结果</returns> public void Execute(object image, out ItemCollection outputs) { HObject hImage = image as HObject; outputs = new ItemCollection(); stopwatch.Restart(); // Local iconic variables HObject ho_GrayImage, ho_ImageMean, ho_RegionDynThresh; HObject ho_RegionFillUp, ho_ConnectedRegions, ho_SelectedRegions1; HObject ho_SelectedRegions2, ho_Regions1 = null, ho_Regions2 = null; HObject ho_Circle1 = null, ho_Circle2 = null, ho_Cross1 = null; HObject ho_Arrow = null; // Local control variables HTuple hv_Number1 = null, hv_Number2 = null, hv_Row1 = new HTuple(); HTuple hv_Column1 = new HTuple(), hv_Radius1 = new HTuple(); HTuple hv_Row2 = new HTuple(), hv_Column2 = new HTuple(); HTuple hv_Radius2 = new HTuple(), hv_RowArray1 = new HTuple(); HTuple hv_ColumnArray1 = new HTuple(), hv_RowArray2 = new HTuple(); HTuple hv_ColumnArray2 = new HTuple(), hv_ResultRow1 = new HTuple(); HTuple hv_ResultColumn1 = new HTuple(), hv_ArcType1 = new HTuple(); HTuple hv_ResultRow2 = new HTuple(), hv_ResultColumn2 = new HTuple(); HTuple hv_ArcType2 = new HTuple(), hv_RowCenter1 = new HTuple(); HTuple hv_ColCenter1 = new HTuple(), hv_StartPhi1 = new HTuple(); HTuple hv_EndPhi1 = new HTuple(), hv_PointOrder1 = new HTuple(); HTuple hv_ArcAngle1 = new HTuple(), hv_RowCenter2 = new HTuple(); HTuple hv_ColCenter2 = new HTuple(), hv_StartPhi2 = new HTuple(); HTuple hv_EndPhi2 = new HTuple(), hv_PointOrder2 = new HTuple(); HTuple hv_ArcAngle2 = new HTuple(), hv_Angle = new HTuple(); HTuple hv_HomMat2D = new HTuple(), hv_Qy = new HTuple(); HTuple hv_Qx = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_GrayImage); HOperatorSet.GenEmptyObj(out ho_ImageMean); HOperatorSet.GenEmptyObj(out ho_RegionDynThresh); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions2); HOperatorSet.GenEmptyObj(out ho_Regions1); HOperatorSet.GenEmptyObj(out ho_Regions2); HOperatorSet.GenEmptyObj(out ho_Circle1); HOperatorSet.GenEmptyObj(out ho_Circle2); HOperatorSet.GenEmptyObj(out ho_Cross1); HOperatorSet.GenEmptyObj(out ho_Arrow); try { HTuple width, height; HOperatorSet.GetImageSize(hImage, out width, out height); //若未初始化,则进行初始化 if (!isInit) { isInit = true; } if (runningWindow == null) { try { runningWindow = (RunningWindow as HSmartWindowControlWPF).HalconWindow; } catch (Exception) { } } if (configWindow == null) { try { configWindow = (ConfigWindow as HSmartWindowControlWPF).HalconWindow; } catch (Exception) { } } if (runningWindow != null) { HOperatorSet.ClearWindow(runningWindow); HOperatorSet.DispObj(hImage, runningWindow); UpdatePart(runningWindow); } if (configWindow != null) { HOperatorSet.ClearWindow(configWindow); HOperatorSet.DispObj(hImage, configWindow); UpdatePart(runningWindow); } //执行主任务 //转为灰度图 ho_GrayImage.Dispose(); HOperatorSet.Rgb1ToGray(hImage, out ho_GrayImage); //动态阈值分割 ho_ImageMean.Dispose(); HOperatorSet.MeanImage(ho_GrayImage, out ho_ImageMean, new HTuple(Inputs["MeanImageMaskHeight"].Value), new HTuple(Inputs["MeanImageMaskWidth"].Value)); ho_RegionDynThresh.Dispose(); HOperatorSet.DynThreshold(ho_GrayImage, ho_ImageMean, out ho_RegionDynThresh, new HTuple(Inputs["DynThresholdOffset"].Value), "dark"); //填充并获取圆 ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_RegionDynThresh, out ho_RegionFillUp); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_RegionFillUp, out ho_ConnectedRegions); ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions1, (new HTuple("circularity")).TupleConcat( "area"), "and", (new HTuple(Inputs["Blob1MincirCularity"].Value)).TupleConcat(new HTuple(Inputs["Blob1MinArea"].Value)), (new HTuple(1.2)).TupleConcat( new HTuple(Inputs["Blob1MaxArea"].Value))); ho_SelectedRegions2.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions2, (new HTuple("circularity")).TupleConcat( "area"), "and", (new HTuple(Inputs["Blob2MincirCularity"].Value)).TupleConcat(new HTuple(Inputs["Blob2MinArea"].Value)), (new HTuple(1.2)).TupleConcat( new HTuple(Inputs["Blob2MaxArea"].Value))); HOperatorSet.CountObj(ho_SelectedRegions1, out hv_Number1); HOperatorSet.CountObj(ho_SelectedRegions2, out hv_Number2); if ((hv_Number1.I == 1) && (hv_Number2.I == 1)) { //产生最小外接圆 HOperatorSet.SmallestCircle(ho_SelectedRegions1, out hv_Row1, out hv_Column1, out hv_Radius1); HOperatorSet.SmallestCircle(ho_SelectedRegions2, out hv_Row2, out hv_Column2, out hv_Radius2); hv_RowArray1 = new HTuple(new double[] { hv_Row1 + hv_Radius1, hv_Row1, hv_Row1 - hv_Radius1, hv_Row1, hv_Row1 + hv_Radius1 }); hv_ColumnArray1 = new HTuple(new double[] { hv_Column1, hv_Column1 + hv_Radius1, hv_Column1, hv_Column1 - hv_Radius1, hv_Column1 }); hv_RowArray2 = new HTuple(new double[] { hv_Row2 + hv_Radius2, hv_Row2, hv_Row2 - hv_Radius2, hv_Row2, hv_Row2 + hv_Radius2 }); hv_ColumnArray2 = new HTuple(new double[] { hv_Column2, hv_Column2 + hv_Radius2, hv_Column2, hv_Column2 - hv_Radius2, hv_Column2 }); //对最小外接圆的位置进行圆拟合 ho_Regions1.Dispose(); spoke(ho_GrayImage, out ho_Regions1, (new HTuple(Inputs["Elements"].Value)), (new HTuple(Inputs["DetectHeight"].Value)), (new HTuple(Inputs["DetectWidth"].Value)), (new HTuple(Inputs["Sigma"].Value)), (new HTuple(Inputs["Threshold"].Value)), (new HTuple(Inputs["Transition"].Value)), (new HTuple(Inputs["Select"].Value)), hv_RowArray1, hv_ColumnArray1, "outer", out hv_ResultRow1, out hv_ResultColumn1, out hv_ArcType1); ho_Regions2.Dispose(); spoke(ho_GrayImage, out ho_Regions2, (new HTuple(Inputs["Elements"].Value)), (new HTuple(Inputs["DetectHeight"].Value)), (new HTuple(Inputs["DetectWidth"].Value)), (new HTuple(Inputs["Sigma"].Value)), (new HTuple(Inputs["Threshold"].Value)), (new HTuple(Inputs["Transition"].Value)), (new HTuple(Inputs["Select"].Value)), hv_RowArray2, hv_ColumnArray2, "outer", out hv_ResultRow2, out hv_ResultColumn2, out hv_ArcType2); ho_Circle1.Dispose(); pts_to_best_circle(out ho_Circle1, hv_ResultRow1, hv_ResultColumn1, (new HTuple(Inputs["ActiveNum"].Value)), "circle", out hv_RowCenter1, out hv_ColCenter1, out hv_Radius1, out hv_StartPhi1, out hv_EndPhi1, out hv_PointOrder1, out hv_ArcAngle1); ho_Circle2.Dispose(); pts_to_best_circle(out ho_Circle2, hv_ResultRow2, hv_ResultColumn2, (new HTuple(Inputs["ActiveNum"].Value)), "circle", out hv_RowCenter2, out hv_ColCenter2, out hv_Radius2, out hv_StartPhi2, out hv_EndPhi2, out hv_PointOrder2, out hv_ArcAngle2); //计算角度 HOperatorSet.AngleLx(hv_RowCenter1, hv_ColCenter1, hv_RowCenter2, hv_ColCenter2, out hv_Angle); ho_Cross1.Dispose(); HOperatorSet.GenCrossContourXld(out ho_Cross1, hv_RowCenter1, hv_ColCenter1, 32, hv_Angle + ((new HTuple(45)).TupleRad())); ho_Arrow.Dispose(); gen_arrow_contour_xld(out ho_Arrow, hv_RowCenter1, hv_ColCenter1, hv_RowCenter2, hv_ColCenter2, 25, 25); //计算仿射变换矩阵 HOperatorSet.VectorAngleToRigid((new HTuple(Inputs["BaseY"].Value)), (new HTuple(Inputs["BaseX"].Value)), (new HTuple(Inputs["BaseAngle"].Value)), hv_RowCenter1, hv_ColCenter1, hv_Angle, out hv_HomMat2D); //计算仿射变换结果 HOperatorSet.AffineTransPoint2d(hv_HomMat2D, (new HTuple(Inputs["LocationY1"].Value)), (new HTuple(Inputs["LocationX1"].Value)), out hv_Qy, out hv_Qx); Outputs["LocationX1"].Value = hv_Qx.D; Outputs["LocationY1"].Value = hv_Qy.D; HOperatorSet.AffineTransPoint2d(hv_HomMat2D, (new HTuple(Inputs["LocationY2"].Value)), (new HTuple(Inputs["LocationX2"].Value)), out hv_Qy, out hv_Qx); Outputs["LocationX2"].Value = hv_Qx.D; Outputs["LocationY2"].Value = hv_Qy.D; HOperatorSet.AffineTransPoint2d(hv_HomMat2D, (new HTuple(Inputs["LocationY3"].Value)), (new HTuple(Inputs["LocationX3"].Value)), out hv_Qy, out hv_Qx); Outputs["LocationX3"].Value = hv_Qx.D; Outputs["LocationY3"].Value = hv_Qy.D; //显示结果 //HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle); //HOperatorSet.DispObj(ho_SelectedRegions1, hv_ExpDefaultWinHandle); //HOperatorSet.DispObj(ho_SelectedRegions2, hv_ExpDefaultWinHandle); //HOperatorSet.DispObj(ho_Arrow, hv_ExpDefaultWinHandle); //HOperatorSet.DispObj(ho_Cross1, hv_ExpDefaultWinHandle); } //显示结果 if (runningWindow != null) { UpdatePart(runningWindow); } if (configWindow != null) { UpdatePart(runningWindow); } stopwatch.Stop(); RunStatus = new RunStatus(stopwatch.Elapsed.TotalMilliseconds); outputs = new ItemCollection(Outputs); } catch (Exception ex) { stopwatch.Stop(); RunStatus = new RunStatus(stopwatch.Elapsed.TotalMilliseconds, EResult.Error, ex.Message, ex); throw; } finally { hImage.Dispose(); ho_GrayImage.Dispose(); ho_ImageMean.Dispose(); ho_RegionDynThresh.Dispose(); ho_RegionFillUp.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions1.Dispose(); ho_SelectedRegions2.Dispose(); ho_Regions1.Dispose(); ho_Regions2.Dispose(); ho_Circle1.Dispose(); ho_Circle2.Dispose(); ho_Cross1.Dispose(); ho_Arrow.Dispose(); } }
static void GetAwgOriginPosition(HObject _image, out double awgX, out double awgY, out double awgAngle, out Bitmap resultImage) { try { HOperatorSet.Threshold(_image, out HObject region1, 120, 255); HOperatorSet.ErosionCircle(region1, out HObject region2, 5.5); region1.Dispose(); HOperatorSet.DilationCircle(region2, out HObject region3, 5.5); region2.Dispose(); HOperatorSet.Connection(region3, out HObject connectedregins1); region3.Dispose(); HOperatorSet.SelectShape(connectedregins1, out HObject selectedregions, "area", "and", 100000, 10000000); connectedregins1.Dispose(); HOperatorSet.Union1(selectedregions, out HObject regionunion); selectedregions.Dispose(); HOperatorSet.ShapeTrans(regionunion, out HObject regiontrans, "rectangle2"); regionunion.Dispose(); HOperatorSet.RegionToBin(regiontrans, out HObject _transimage, 255, 0, 3840, 2748); regiontrans.Dispose(); HOperatorSet.PointsFoerstner(_transimage, 1, 2, 3, 200, 0.3, "gauss", "false", out HTuple rowJunctions, out HTuple columnJunctions, out HTuple coRRJunctions, out HTuple coRCJunctions, out HTuple coCCJunctions, out HTuple rowArea, out HTuple columnArea, out HTuple coRRArea, out HTuple coRCArea, out HTuple coCCArea); if (rowJunctions.Length < 2) { throw new Exception("awgCheck error in PointsFoerstner"); } HTuple phi; HTuple _angle; HTuple _row, _column; HOperatorSet.TupleRad(-75, out _angle); int hypotenuselength = 730; if (columnJunctions[0] < columnJunctions[1]) { HOperatorSet.AngleLx(rowJunctions[0], columnJunctions[0], rowJunctions[1], columnJunctions[1], out phi); _angle += phi; _row = rowJunctions[1] + Math.Cos(_angle) * hypotenuselength; _column = columnJunctions[1] + Math.Sin(_angle) * hypotenuselength; } else { HOperatorSet.AngleLx(rowJunctions[1], columnJunctions[1], rowJunctions[0], columnJunctions[0], out phi); _angle += phi; _row = rowJunctions[0] + Math.Cos(_angle) * hypotenuselength; _column = columnJunctions[0] + Math.Sin(_angle) * hypotenuselength; } visionFun.scale_image_range(_image, out HObject imageScaled, 180, 230); HOperatorSet.MeanImage(imageScaled, out HObject imageMean, 5, 5); visionFun.findline(imageMean, out HObject line, _row, _column, phi, 150, 150, "nearest_neighbor", 2.5, 25, "positive", "last"); HOperatorSet.WriteImage(_image, "bmp", 0, AppDomain.CurrentDomain.BaseDirectory + "awg.bmp"); double rx = (rowJunctions[0] + rowJunctions[1]) / 2; double ry = (columnJunctions[0] + columnJunctions[1]) / 2; HOperatorSet.TupleRad(-90, out HTuple _angle90); visionFun.findline(_image, out HObject line1, rx, ry, phi + _angle90, 300, 500, "nearest_neighbor", 2.5, 50, "positive", "first"); HOperatorSet.RegionToBin(line, out _transimage, 255, 0, 3840, 2748); HOperatorSet.PointsFoerstner(_transimage, 1, 2, 3, 200, 0.3, "gauss", "false", out HTuple rowJunctions1, out HTuple columnJunctions1, out HTuple coRRJunctions1, out HTuple coRCJunctions1, out HTuple coCCJunctions1, out HTuple rowArea1, out HTuple columnArea1, out HTuple coRRArea1, out HTuple coRCArea1, out HTuple coCCArea1); HOperatorSet.RegionToBin(line1, out _transimage, 255, 0, 3840, 2748); HOperatorSet.PointsFoerstner(_transimage, 1, 2, 3, 200, 0.3, "gauss", "false", out HTuple rowJunctions2, out HTuple columnJunctions2, out HTuple coRRJunctions2, out HTuple coRCJunctions2, out HTuple coCCJunctions2, out HTuple rowArea2, out HTuple columnArea2, out HTuple coRRArea2, out HTuple coRCArea2, out HTuple coCCArea2); _transimage.Dispose(); HOperatorSet.IntersectionLines(rowJunctions1[0], columnJunctions1[0], rowJunctions1[1], columnJunctions1[1], rowJunctions2[0], columnJunctions2[0], rowJunctions2[1], columnJunctions2[1], out HTuple finalRow, out HTuple finalColumn, out HTuple isoverlapping); HOperatorSet.AffineTransPoint2d(calibratedata, finalRow, finalColumn, out HTuple x, out HTuple y); Console.WriteLine($"awg row value: {finalRow.D}"); Console.WriteLine($"awg column value: {finalColumn.D}"); visionFun.HObject2Bpp8(_image, out Bitmap bitmap); resultImage = BitMapZd.DrawCross(bitmap, (float)finalColumn.D, (float)finalRow.D, 45, 30, 10, Color.Red); awgX = x; awgY = y; awgAngle = phi; } catch (Exception ex) { HOperatorSet.WriteImage(_image, "bmp", 0, AppDomain.CurrentDomain.BaseDirectory + "awg.bmp"); throw ex; } }
void GetAwgPosition(HObject _image, out HTuple awgX, out HTuple awgY, out HObject cross) { try { HOperatorSet.Threshold(_image, out HObject region1, 120, 255); HOperatorSet.ErosionCircle(region1, out HObject region2, 5.5); region1.Dispose(); HOperatorSet.DilationCircle(region2, out HObject region3, 5.5); region2.Dispose(); HOperatorSet.Connection(region3, out HObject connectedregins1); region3.Dispose(); HOperatorSet.SelectShape(connectedregins1, out HObject selectedregions, "area", "and", 100000, 10000000); connectedregins1.Dispose(); HOperatorSet.Union1(selectedregions, out HObject regionunion); selectedregions.Dispose(); HOperatorSet.ShapeTrans(regionunion, out HObject regiontrans, "rectangle2"); regionunion.Dispose(); HOperatorSet.RegionToBin(regiontrans, out HObject _transimage, 255, 0, 3840, 2748); regiontrans.Dispose(); HOperatorSet.PointsFoerstner(_transimage, 1, 2, 3, 200, 0.3, "gauss", "false", out HTuple rowJunctions, out HTuple columnJunctions, out HTuple coRRJunctions, out HTuple coRCJunctions, out HTuple coCCJunctions, out HTuple rowArea, out HTuple columnArea, out HTuple coRRArea, out HTuple coRCArea, out HTuple coCCArea); if (rowJunctions.Length < 2) { throw new Exception("awgCheck error in PointsFoerstner"); } HTuple phi; HTuple _angle; HTuple _row, _column; HOperatorSet.TupleRad(-75, out _angle); int hypotenuselength = 730; if (columnJunctions[0] < columnJunctions[1]) { HOperatorSet.AngleLx(rowJunctions[0], columnJunctions[0], rowJunctions[1], columnJunctions[1], out phi); _angle += phi; _row = rowJunctions[1] + Math.Cos(_angle) * hypotenuselength; _column = columnJunctions[1] + Math.Sin(_angle) * hypotenuselength; } else { HOperatorSet.AngleLx(rowJunctions[1], columnJunctions[1], rowJunctions[0], columnJunctions[0], out phi); _angle += phi; _row = rowJunctions[0] + Math.Cos(_angle) * hypotenuselength; _column = columnJunctions[0] + Math.Sin(_angle) * hypotenuselength; } visionFun.scale_image_range(_image, out HObject imageScaled, 180, 230); HOperatorSet.MeanImage(imageScaled, out HObject imageMean, 5, 5); visionFun.findline(imageMean, out HObject line, _row, _column, phi, 150, 150, "nearest_neighbor", 2.5, 25, "positive", "last"); HOperatorSet.WriteImage(_image, "bmp", 0, AppDomain.CurrentDomain.BaseDirectory + "awg.bmp"); double rx = (rowJunctions[0] + rowJunctions[1]) / 2; double ry = (columnJunctions[0] + columnJunctions[1]) / 2; HOperatorSet.TupleRad(-90, out HTuple _angle90); visionFun.findline(_image, out HObject line1, rx, ry, phi + _angle90, 300, 500, "nearest_neighbor", 2.5, 50, "positive", "first"); HOperatorSet.RegionToBin(line, out _transimage, 255, 0, 3840, 2748); HOperatorSet.PointsFoerstner(_transimage, 1, 2, 3, 200, 0.3, "gauss", "false", out HTuple rowJunctions1, out HTuple columnJunctions1, out HTuple coRRJunctions1, out HTuple coRCJunctions1, out HTuple coCCJunctions1, out HTuple rowArea1, out HTuple columnArea1, out HTuple coRRArea1, out HTuple coRCArea1, out HTuple coCCArea1); HOperatorSet.RegionToBin(line1, out _transimage, 255, 0, 3840, 2748); HOperatorSet.PointsFoerstner(_transimage, 1, 2, 3, 200, 0.3, "gauss", "false", out HTuple rowJunctions2, out HTuple columnJunctions2, out HTuple coRRJunctions2, out HTuple coRCJunctions2, out HTuple coCCJunctions2, out HTuple rowArea2, out HTuple columnArea2, out HTuple coRRArea2, out HTuple coRCArea2, out HTuple coCCArea2); _transimage.Dispose(); HOperatorSet.IntersectionLines(rowJunctions1[0], columnJunctions1[0], rowJunctions1[1], columnJunctions1[1], rowJunctions2[0], columnJunctions2[0], rowJunctions2[1], columnJunctions2[1], out awgX, out awgY, out HTuple isoverlapping); HOperatorSet.GenCrossContourXld(out cross, awgX, awgY, 20, phi); } catch (Exception ex) { HOperatorSet.WriteImage(_image, "bmp", 0, AppDomain.CurrentDomain.BaseDirectory + "awg.bmp"); throw ex; } }
// Main procedure private void action() { // Local iconic variables HObject ho_Image = null, ho_ImageMean = null, ho_Regions1 = null; HObject ho_RegionFillUp = null, ho_ConnectedRegions1 = null; HObject ho_SelectedRegions = null, ho_RegionErosion = null; HObject ho_RegionDifference = null, ho_ImageReduced = null; HObject ho_Regions = null, ho_ConnectedRegions = null, ho_SelectedRegions1 = null; HObject ho_RegionDilation = null, ho_RegionFillUp1 = null, ho_SelectedRegions2 = null; HObject ho_RegionUnion = null, ho_Rectangle = null; // Local control variables HTuple hv_Index = null, hv_WindowHandle = new HTuple(); HTuple hv_Area = new HTuple(), hv_Row = new HTuple(), hv_Column = new HTuple(); HTuple hv_Area1 = new HTuple(), hv_Row1 = new HTuple(); HTuple hv_Column1 = new HTuple(), hv_angles = new HTuple(); HTuple hv_rectangles = new HTuple(), hv_index = new HTuple(); HTuple hv_offset = new HTuple(), hv_Angle = new HTuple(); HTuple hv_deltaRow = new HTuple(), hv_deltaColumn = new HTuple(); HTuple hv_rectRow = new HTuple(), hv_rectColumn = new HTuple(); HTuple hv_Indices = new HTuple(), hv_Sum = null; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_Image); HOperatorSet.GenEmptyObj(out ho_ImageMean); HOperatorSet.GenEmptyObj(out ho_Regions1); HOperatorSet.GenEmptyObj(out ho_RegionFillUp); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions); HOperatorSet.GenEmptyObj(out ho_RegionErosion); HOperatorSet.GenEmptyObj(out ho_RegionDifference); HOperatorSet.GenEmptyObj(out ho_ImageReduced); HOperatorSet.GenEmptyObj(out ho_Regions); HOperatorSet.GenEmptyObj(out ho_ConnectedRegions); HOperatorSet.GenEmptyObj(out ho_SelectedRegions1); HOperatorSet.GenEmptyObj(out ho_RegionDilation); HOperatorSet.GenEmptyObj(out ho_RegionFillUp1); HOperatorSet.GenEmptyObj(out ho_SelectedRegions2); HOperatorSet.GenEmptyObj(out ho_RegionUnion); HOperatorSet.GenEmptyObj(out ho_Rectangle); //read_image (PicBlockid18773, 'C:/Users/sunru/Documents/WeChat Files/sunrh109989/FileStorage/File/2019-08/190808 五金件检测(1)/190808 五金件检测/1/2.bmp') for (hv_Index = 10; (int)hv_Index <= 10; hv_Index = (int)hv_Index + 1) { ho_Image.Dispose(); HOperatorSet.ReadImage(out ho_Image, ("C:/Users/sunru/Documents/WeChat Files/sunrh109989/FileStorage/File/2019-08/190809/190809/2/" + hv_Index) + ".bmp"); //dev_close_window(...); dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle); ho_ImageMean.Dispose(); HOperatorSet.MeanImage(ho_Image, out ho_ImageMean, 19, 19); //select region HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red"); ho_Regions1.Dispose(); HOperatorSet.Threshold(ho_ImageMean, out ho_Regions1, 160, 250); ho_RegionFillUp.Dispose(); HOperatorSet.FillUp(ho_Regions1, out ho_RegionFillUp); ho_ConnectedRegions1.Dispose(); HOperatorSet.Connection(ho_RegionFillUp, out ho_ConnectedRegions1); ho_SelectedRegions.Dispose(); HOperatorSet.SelectShapeStd(ho_ConnectedRegions1, out ho_SelectedRegions, "max_area", 70); ho_RegionErosion.Dispose(); HOperatorSet.ErosionCircle(ho_SelectedRegions, out ho_RegionErosion, 400); ho_RegionDifference.Dispose(); HOperatorSet.Difference(ho_SelectedRegions, ho_RegionErosion, out ho_RegionDifference ); ho_ImageReduced.Dispose(); HOperatorSet.ReduceDomain(ho_ImageMean, ho_RegionDifference, out ho_ImageReduced ); ho_Regions.Dispose(); HOperatorSet.Threshold(ho_ImageReduced, out ho_Regions, 13, 150); ho_ConnectedRegions.Dispose(); HOperatorSet.Connection(ho_Regions, out ho_ConnectedRegions); ho_SelectedRegions1.Dispose(); HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions1, "outer_radius", "and", 20.18, 122.94); ho_RegionDilation.Dispose(); HOperatorSet.DilationCircle(ho_SelectedRegions1, out ho_RegionDilation, 20); ho_RegionFillUp1.Dispose(); HOperatorSet.FillUp(ho_RegionDilation, out ho_RegionFillUp1); ho_SelectedRegions2.Dispose(); HOperatorSet.SelectShape(ho_RegionFillUp1, out ho_SelectedRegions2, (new HTuple("circularity")).TupleConcat( "area"), "and", (new HTuple(0.78073)).TupleConcat(10963.3), (new HTuple(1)).TupleConcat( 31697.3)); HOperatorSet.AreaCenter(ho_SelectedRegions2, out hv_Area, out hv_Row, out hv_Column); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); HOperatorSet.DispCross(hv_ExpDefaultWinHandle, hv_Row, hv_Column, 86, 0); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "red"); //Find the center of whole region ho_RegionUnion.Dispose(); HOperatorSet.Union1(ho_SelectedRegions2, out ho_RegionUnion); HOperatorSet.AreaCenter(ho_RegionUnion, out hv_Area1, out hv_Row1, out hv_Column1); HOperatorSet.DispCross(hv_ExpDefaultWinHandle, hv_Row1, hv_Column1, 86, 0); hv_angles = new HTuple(); hv_rectangles = new HTuple(); HOperatorSet.SetColor(hv_ExpDefaultWinHandle, "green"); for (hv_index = 0; (int)hv_index <= 5; hv_index = (int)hv_index + 1) { //index := 0 hv_offset = 0.7; HOperatorSet.AngleLx(hv_Row.TupleSelect(hv_index), hv_Column.TupleSelect( hv_index), hv_Row1, hv_Column1, out hv_Angle); hv_angles = hv_angles.TupleConcat(hv_Angle); hv_deltaRow = (hv_Row.TupleSelect(hv_index)) - hv_Row1; hv_deltaColumn = (hv_Column.TupleSelect(hv_index)) - hv_Column1; hv_rectRow = (hv_offset * hv_deltaRow) + hv_Row1; hv_rectColumn = (hv_offset * hv_deltaColumn) + hv_Column1; HOperatorSet.DispCross(hv_ExpDefaultWinHandle, hv_rectRow, hv_rectColumn, 86, hv_Angle); //gen measure rectangle ho_Rectangle.Dispose(); HOperatorSet.GenRectangle2(out ho_Rectangle, hv_rectRow, hv_rectColumn, hv_Angle, 60, 150); hv_rectangles = ((((((hv_rectangles.TupleConcat(hv_rectRow))).TupleConcat( hv_rectColumn))).TupleConcat(hv_Angle))).TupleConcat((new HTuple(60)).TupleConcat( 150)); } //sort HOperatorSet.TupleSortIndex(hv_angles, out hv_Indices); HDevelopStop(); } HOperatorSet.TupleAdd(hv_Indices, 1, out hv_Sum); ho_Image.Dispose(); ho_ImageMean.Dispose(); ho_Regions1.Dispose(); ho_RegionFillUp.Dispose(); ho_ConnectedRegions1.Dispose(); ho_SelectedRegions.Dispose(); ho_RegionErosion.Dispose(); ho_RegionDifference.Dispose(); ho_ImageReduced.Dispose(); ho_Regions.Dispose(); ho_ConnectedRegions.Dispose(); ho_SelectedRegions1.Dispose(); ho_RegionDilation.Dispose(); ho_RegionFillUp1.Dispose(); ho_SelectedRegions2.Dispose(); ho_RegionUnion.Dispose(); ho_Rectangle.Dispose(); }
public void findModel(HObject ho_Image, HObject ho_DomainRegion_01, HObject ho_DomainRegion_02, HObject ho_ModelRegion_01, HObject ho_ModelContours_01, HObject ho_ModelRegion_02, HObject ho_ModelContours_02, out HObject ho_ContoursAffinTrans_01, out HObject ho_RegionAffinTrans_01, out HObject ho_ContoursAffinTrans_02, out HObject ho_RegionAffinTrans_02, HTuple hv_ModelId_01, HTuple hv_ModelId_02, HTuple hv_CenterRow_01, HTuple hv_CenterColumn_01, HTuple hv_CenterRow_02, HTuple hv_CenterColumn_02, HTuple hv_AngleModel, out HTuple hv_HomMat2D, out bool hv_Result) { // Local iconic variables HObject ho_RegionMoved_01 = null, ho_RegionMoved_02 = null; HObject ho_ImageReduce_01 = null, ho_ImageReduce_02 = null; // Local control variables HTuple hv_MatchRow_01 = null, hv_MatchColumn_01 = null; HTuple hv_MatchAngle_01 = null, hv_MatchScore_01 = null; HTuple hv_MatchRow_02 = null, hv_MatchColumn_02 = null; HTuple hv_MatchAngle_02 = null, hv_MatchScore_02 = null; HTuple hv_HomMat2D_01 = new HTuple(), hv_HomMat2D_02 = new HTuple(); HTuple hv_Angle = new HTuple(); // Initialize local and output iconic variables HOperatorSet.GenEmptyObj(out ho_ImageReduce_01); HOperatorSet.GenEmptyObj(out ho_ImageReduce_02); HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans_01); HOperatorSet.GenEmptyObj(out ho_RegionAffinTrans_01); HOperatorSet.GenEmptyObj(out ho_ContoursAffinTrans_02); HOperatorSet.GenEmptyObj(out ho_RegionAffinTrans_02); HOperatorSet.GenEmptyObj(out ho_RegionMoved_01); HOperatorSet.GenEmptyObj(out ho_RegionMoved_02); hv_HomMat2D = new HTuple(); ho_ImageReduce_01.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_DomainRegion_01, out ho_ImageReduce_01); ho_ImageReduce_02.Dispose(); HOperatorSet.ReduceDomain(ho_Image, ho_DomainRegion_02, out ho_ImageReduce_02); HOperatorSet.FindShapeModel(ho_ImageReduce_01, hv_ModelId_01, (new HTuple(0)).TupleRad() , (new HTuple(360)).TupleRad(), 0.5, 1, 0.5, "least_squares", (new HTuple(4)).TupleConcat( 1), 0.65, out hv_MatchRow_01, out hv_MatchColumn_01, out hv_MatchAngle_01, out hv_MatchScore_01); HOperatorSet.FindShapeModel(ho_ImageReduce_02, hv_ModelId_02, (new HTuple(0)).TupleRad() , (new HTuple(360)).TupleRad(), 0.5, 1, 0.5, "least_squares", (new HTuple(4)).TupleConcat( 1), 0.65, out hv_MatchRow_02, out hv_MatchColumn_02, out hv_MatchAngle_02, out hv_MatchScore_02); hv_Result = false; if ((int)((new HTuple((new HTuple(hv_MatchScore_01.TupleLength())).TupleEqual( 1))).TupleAnd(new HTuple((new HTuple(hv_MatchScore_02.TupleLength())).TupleEqual( 1)))) != 0) { ho_RegionMoved_01.Dispose(); HOperatorSet.MoveRegion(ho_ModelRegion_01, out ho_RegionMoved_01, -hv_CenterRow_01, -hv_CenterColumn_01); ho_RegionMoved_02.Dispose(); HOperatorSet.MoveRegion(ho_ModelRegion_02, out ho_RegionMoved_02, -hv_CenterRow_02, -hv_CenterColumn_02); HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_MatchRow_01, hv_MatchColumn_01, hv_MatchAngle_01, out hv_HomMat2D_01); ho_ContoursAffinTrans_01.Dispose(); HOperatorSet.AffineTransContourXld(ho_ModelContours_01, out ho_ContoursAffinTrans_01, hv_HomMat2D_01); ho_RegionAffinTrans_01.Dispose(); HOperatorSet.AffineTransRegion(ho_RegionMoved_01, out ho_RegionAffinTrans_01, hv_HomMat2D_01, "nearest_neighbor"); HOperatorSet.VectorAngleToRigid(0, 0, 0, hv_MatchRow_02, hv_MatchColumn_02, hv_MatchAngle_02, out hv_HomMat2D_02); ho_ContoursAffinTrans_02.Dispose(); HOperatorSet.AffineTransContourXld(ho_ModelContours_02, out ho_ContoursAffinTrans_02, hv_HomMat2D_02); ho_RegionAffinTrans_02.Dispose(); HOperatorSet.AffineTransRegion(ho_RegionMoved_02, out ho_RegionAffinTrans_02, hv_HomMat2D_02, "nearest_neighbor"); HOperatorSet.AngleLx(hv_MatchRow_01, hv_MatchColumn_01, hv_MatchRow_02, hv_MatchColumn_02, out hv_Angle); HOperatorSet.VectorAngleToRigid(0, 0, hv_AngleModel, hv_MatchRow_01, hv_MatchColumn_01, hv_Angle, out hv_HomMat2D); //affine_trans_pixel (HomMat2D, Rows, Cols, RowTrans, ColTrans) //gen_circle_contour_xld (ContCircle1, RowTrans, ColTrans, Circle_Radius, 0, 6.28318, 'positive', 1) hv_Result = true; } ho_RegionMoved_01.Dispose(); ho_RegionMoved_02.Dispose(); return; }