private void GenVirtualBtn_Click(object sender, EventArgs e) { TryDebugBtn_Click(null, new EventArgs()); if (FindCircleRow.Length >= 2 && FindCircleCol.Length >= 2) { HTuple OutLineRow1 = new HTuple(), OutLineCol1 = new HTuple(), OutLineRow2 = new HTuple(), OutLineCol2 = new HTuple(); view1.ResetView(); view1.AddImage(GrabedImg); //1.0找线 TryDebugBtn_Click(null, new EventArgs()); HTuple LineAngle = new HTuple(); HObject LineArrow = new HObject(); HObject LitCirCross = new HObject(); //2.0计算圆心连线与相机X轴的夹角 OutLineRow1 = FindCircleRow[0].D; OutLineCol1 = FindCircleCol[0].D; OutLineRow2 = FindCircleRow[1].D; OutLineCol2 = FindCircleCol[1].D; LineTypePos.CalculateLineAng(OutLineRow1, OutLineCol1, OutLineRow2, OutLineCol2, out LineAngle, out LineArrow); LineAngle = LineAngle.D / Math.PI * 180.0; HTuple LitCirRow = new HTuple(), LitCircol = new HTuple(); HTuple DistRr = (7.78 / 2.0) / 2.8 * FindCircleR[0].D; //3.0生成虚拟圆的圆心 CircleTypePos.GenCirCenter(FindCircleRow[0].D, FindCircleCol[0].D, DistRr, out LitCirRow, out LitCircol); HOperatorSet.GenCrossContourXld(out LitCirCross, LitCirRow, LitCircol, 60, 0); view1.SetDraw("blue", "margin"); view1.AddViewObject(LineArrow); view1.AddViewObject(LitCirCross); HHomMat2D MyHomMat = new HHomMat2D(); MyHomMat = MyHomMat.HomMat2dRotate((TeachCirclePara.OffSetPixelTh + LineAngle) / 180.0 * Math.PI, FindCircleRow[0], FindCircleCol[0]); //4.0旋转生成圆的圆心 MyHomMat.AffineTransPixel(LitCirRow, LitCircol, out LitCirRow, out LitCircol); HOperatorSet.GenCrossContourXld(out LitCirCross, LitCirRow, LitCircol, 60, 0); HObject FirstCirCont = new HObject(), SecondCirCont = new HObject(); HOperatorSet.GenCircleContourXld(out FirstCirCont, LitCirRow[0].D, LitCircol[0].D, 60, 0, 2 * Math.PI, "positive", 0.1); HOperatorSet.GenCircleContourXld(out SecondCirCont, LitCirRow[1].D, LitCircol[1].D, 40, 0, 2 * Math.PI, "positive", 0.1); HOperatorSet.ConcatObj(LitCirCross, FirstCirCont, out LitCirCross); HOperatorSet.ConcatObj(LitCirCross, SecondCirCont, out LitCirCross); view1.SetDraw("red", "margin"); view1.AddViewObject(LitCirCross); } }
public bool TransPoint(double row, double col, out double outRow, out double outCol) { try { HHomMat2D matd = new HHomMat2D(); matd.VectorAngleToRigid(this.info.ModelRegionRow, this.info.ModelRegionCol, this.info.ModelRegionAngle, this.info.ResultY, this.info.ResultX, this.info.ResultAngle); if (IsSuccess) { matd.AffineTransPixel(row, col, out outRow, out outCol); return(true); } } catch (Exception ex) { WriteErrorLog("VisionTool", ToolName + "模板" + ex.ToString()); } outRow = 0; outCol = 0; return(false); }
private void Action() { double S1, S2; HTuple RowCheck, ColumnCheck, AngleCheck, Score; HHomMat2D Matrix = new HHomMat2D(); HRegion ModelRegionTrans; HTuple Rect1RowCheck, Rect1ColCheck; HTuple Rect2RowCheck, Rect2ColCheck; HRegion Rectangle1 = new HRegion(); HRegion Rectangle2 = new HRegion(); HMeasure Measure1, Measure2; HTuple RowEdgeFirst1, ColumnEdgeFirst1; HTuple AmplitudeFirst1, RowEdgeSecond1; HTuple ColumnEdgeSecond1, AmplitudeSecond1; HTuple IntraDistance1, InterDistance1; HTuple RowEdgeFirst2, ColumnEdgeFirst2; HTuple AmplitudeFirst2, RowEdgeSecond2; HTuple ColumnEdgeSecond2, AmplitudeSecond2; HTuple IntraDistance2, InterDistance2; HTuple MinDistance; int NumLeads; HSystem.SetSystem("flush_graphic", "false"); Img.GrabImage(Framegrabber); Img.DispObj(Window); // Find the IC in the current image. S1 = HSystem.CountSeconds(); ShapeModel.FindShapeModel(Img, 0, new HTuple(360).TupleRad().D, 0.7, 1, 0.5, "least_squares", 4, 0.9, out RowCheck, out ColumnCheck, out AngleCheck, out Score); S2 = HSystem.CountSeconds(); MatchingTimeLabel.Text = "Time: " + String.Format("{0,4:F1}", (S2 - S1) * 1000) + "ms"; MatchingScoreLabel.Text = "Score: "; if (RowCheck.Length == 1) { MatchingScoreLabel.Text = "Score: " + String.Format("{0:F5}", Score.D); // Rotate the model for visualization purposes. Matrix.VectorAngleToRigid(new HTuple(Row), new HTuple(Column), new HTuple(0.0), RowCheck, ColumnCheck, AngleCheck); ModelRegionTrans = ModelRegion.AffineTransRegion(Matrix, "false"); Window.SetColor("green"); Window.SetDraw("fill"); ModelRegionTrans.DispObj(Window); // Compute the parameters of the measurement rectangles. Matrix.AffineTransPixel(Rect1Row, Rect1Col, out Rect1RowCheck, out Rect1ColCheck); Matrix.AffineTransPixel(Rect2Row, Rect2Col, out Rect2RowCheck, out Rect2ColCheck); // For visualization purposes, generate the two rectangles as // regions and display them. Rectangle1.GenRectangle2(Rect1RowCheck.D, Rect1ColCheck.D, RectPhi + AngleCheck.D, RectLength1, RectLength2); Rectangle2.GenRectangle2(Rect2RowCheck.D, Rect2ColCheck.D, RectPhi + AngleCheck.D, RectLength1, RectLength2); Window.SetColor("blue"); Window.SetDraw("margin"); Rectangle1.DispObj(Window); Rectangle2.DispObj(Window); // Do the actual measurements. S1 = HSystem.CountSeconds(); Measure1 = new HMeasure(Rect1RowCheck.D, Rect1ColCheck.D, RectPhi + AngleCheck.D, RectLength1, RectLength2, ImgWidth, ImgHeight, "bilinear"); Measure2 = new HMeasure(Rect2RowCheck.D, Rect2ColCheck.D, RectPhi + AngleCheck.D, RectLength1, RectLength2, ImgWidth, ImgHeight, "bilinear"); Measure1.MeasurePairs(Img, 2, 90, "positive", "all", out RowEdgeFirst1, out ColumnEdgeFirst1, out AmplitudeFirst1, out RowEdgeSecond1, out ColumnEdgeSecond1, out AmplitudeSecond1, out IntraDistance1, out InterDistance1); Measure2.MeasurePairs(Img, 2, 90, "positive", "all", out RowEdgeFirst2, out ColumnEdgeFirst2, out AmplitudeFirst2, out RowEdgeSecond2, out ColumnEdgeSecond2, out AmplitudeSecond2, out IntraDistance2, out InterDistance2); S2 = HSystem.CountSeconds(); MeasureTimeLabel.Text = "Time: " + String.Format("{0,5:F1}", (S2 - S1) * 1000) + "ms"; Window.SetColor("red"); Window.DispLine(RowEdgeFirst1 - RectLength2 * Math.Cos(AngleCheck), ColumnEdgeFirst1 - RectLength2 * Math.Sin(AngleCheck), RowEdgeFirst1 + RectLength2 * Math.Cos(AngleCheck), ColumnEdgeFirst1 + RectLength2 * Math.Sin(AngleCheck)); Window.DispLine(RowEdgeSecond1 - RectLength2 * Math.Cos(AngleCheck), ColumnEdgeSecond1 - RectLength2 * Math.Sin(AngleCheck), RowEdgeSecond1 + RectLength2 * Math.Cos(AngleCheck), ColumnEdgeSecond1 + RectLength2 * Math.Sin(AngleCheck)); Window.DispLine(RowEdgeFirst2 - RectLength2 * Math.Cos(AngleCheck), ColumnEdgeFirst2 - RectLength2 * Math.Sin(AngleCheck), RowEdgeFirst2 + RectLength2 * Math.Cos(AngleCheck), ColumnEdgeFirst2 + RectLength2 * Math.Sin(AngleCheck)); Window.DispLine(RowEdgeSecond2 - RectLength2 * Math.Cos(AngleCheck), ColumnEdgeSecond2 - RectLength2 * Math.Sin(AngleCheck), RowEdgeSecond2 + RectLength2 * Math.Cos(AngleCheck), ColumnEdgeSecond2 + RectLength2 * Math.Sin(AngleCheck)); NumLeads = IntraDistance1.Length + IntraDistance2.Length; MeasureNumLabel.Text = "Number of leads: " + String.Format("{0:D2}", NumLeads); MinDistance = InterDistance1.TupleConcat(InterDistance2).TupleMin(); MeasureDistLabel.Text = "Minimum lead distance: " + String.Format("{0:F3}", MinDistance.D); HSystem.SetSystem("flush_graphic", "true"); // Force the graphics window update by displaying an offscreen pixel Window.DispLine(-1.0, -1.0, -1.0, -1.0); } }
/// <summary> /// 在当前图像上按单元配置查找直线 /// </summary> /// <param name="img"></param> /// <param name="hFixTool"></param> /// <returns></returns> public bool FindLine(HObject img) { m_rowBegin = new HTuple(); m_colBegin = new HTuple(); m_rowEnd = new HTuple(); m_colEnd = new HTuple(); m_hLine.Dispose(); m_hCross.Dispose(); m_hCrossDrop.Dispose(); if (img == null) { return(false); } string strSelect = string.Empty; if (Hselect == "strongest") { strSelect = "all"; } else { strSelect = Hselect; } HTuple imgWidth, imgHeight; HOperatorSet.GetImageSize(img, out imgWidth, out imgHeight); m_imageWidth = imgWidth; m_imageHeight = imgHeight; double x1, y1, x2, y2; HTuple in_phi_t = in_phi + data_from_phi + data_phi * 3.1415926 / 180; double angle = in_phi_t[0].D + 3.1415926 / 2; x1 = (in_col + data_col)[0].D + L2 * Math.Cos(angle); x2 = (in_col + data_col)[0].D - L2 * Math.Cos(angle); y1 = (in_row + data_row)[0].D - L2 * Math.Sin(angle); y2 = (in_row + data_row)[0].D + L2 * Math.Sin(angle); HHomMat2D hom = new HHomMat2D(); hom.HomMat2dIdentity(); hom = hom.HomMat2dTranslate((y2 - y1) / (Hnum - 1), (x2 - x1) / (Hnum - 1)); double rect_row = y1; double rect_col = x1; double rect_phi = in_phi_t[0].D; double rect_len1 = L1; double rect_len2 = (double)Hwid / 2.0; HTuple hms = null; HOperatorSet.GenMeasureRectangle2(rect_row, rect_col, rect_phi, rect_len1, rect_len2, imgWidth, imgHeight, "nearest_neighbor", out hms); HTuple Row = new HTuple(); HTuple Col = new HTuple(); HTuple Distance = new HTuple(); HTuple outRow = new HTuple(); HTuple outCol = new HTuple(); HTuple outAmp = new HTuple(); HTuple outDistance = new HTuple(); HRegion regionCount = new HRegion(); regionCount.GenEmptyRegion(); HXLDCont contour = new HXLDCont(); for (int i = 0; i < Hnum[0].I; ++i) { if (m_bShowRectang2) { contour.Dispose(); contour.GenRectangle2ContourXld(rect_row, rect_col, rect_phi, rect_len1, rect_len2); HRegion region; region = contour.GenRegionContourXld("margin"); regionCount = regionCount.Union2(region); } try { HOperatorSet.MeasurePos(img, hms, Hsomth, Hamp, Htransition, strSelect, out outRow, out outCol, out outAmp, out outDistance); } catch { continue; } if (strSelect == "all" && outRow.Length > 1) { HTuple hIndex = outAmp.TupleSortIndex(); int nMax = hIndex[hIndex.Length - 1]; Row.Append(outRow[nMax]); Col.Append(outCol[nMax]); } else { Row.Append(outRow); Col.Append(outCol); } hom.AffineTransPixel(rect_row, rect_col, out rect_row, out rect_col); HOperatorSet.TranslateMeasure(hms, rect_row, rect_col); } HOperatorSet.CloseMeasure(hms); if (m_bShowRectang2) { m_hRectang2 = regionCount.GenContoursSkeletonXld(1, "filter"); } if (Row.Length > 1) { HXLDCont counter = new HXLDCont(); counter.GenContourPolygonXld(Row, Col); HTuple nr, nc, dist; counter.FitLineContourXld("tukey", -1, 0, 5, 2, out m_rowBegin, out m_colBegin, out m_rowEnd, out m_colEnd, out nr, out nc, out dist); HTuple rowDrop = new HTuple(); HTuple colDrop = new HTuple(); if (Row.Length - 3 > m_nNumDropPoints && m_nNumDropPoints > 0) { HTuple distance = new HTuple(); for (int i = 0; i < Row.Length; ++i) { double dis = HMisc.DistancePl(Row[i], Col[i], m_rowBegin, m_colBegin, m_rowEnd, m_colEnd); distance.Append(dis); } HTuple index = distance.TupleSortIndex(); index = index.TupleInverse(); for (int i = 0; i < m_nNumDropPoints; ++i) { int n = index[i]; rowDrop.Append(Row[n]); colDrop.Append(Col[n]); } index = index.TupleFirstN(m_nNumDropPoints - 1); Row = Row.TupleRemove(index); Col = Col.TupleRemove(index); counter.GenContourPolygonXld(Row, Col); counter.FitLineContourXld("tukey", -1, 0, 5, 2, out m_rowBegin, out m_colBegin, out m_rowEnd, out m_colEnd, out nr, out nc, out dist); } if (m_bShowLine) { HTuple rrow = new HTuple(m_rowBegin); HTuple ccol = new HTuple(m_colBegin); rrow.Append(m_rowEnd); ccol.Append(m_colEnd); m_hLine.GenContourPolygonXld(rrow, ccol); } if (m_bShowCross) { m_hCross.GenEmptyObj(); m_hCross.GenCrossContourXld(Row, Col, 17, (new HTuple(45)).TupleRad()); } if (m_bShowCrossDrop) { m_hCrossDrop.GenEmptyObj(); m_hCrossDrop.GenCrossContourXld(rowDrop, colDrop, 17, (new HTuple(45)).TupleRad()); } Result_Array[0] = m_rowBegin; Result_Array[1] = m_colBegin; Result_Array[2] = m_rowEnd; Result_Array[3] = m_colEnd; return(true); } return(false); }
public void FindShapeModel(HImage Img, HWindow window, double row1, double col1, double row2, double col2, double rowMark, double colMark) { double S1, S2; HTuple RowCheck, ColumnCheck, AngleCheck, Score; HHomMat2D Matrix = new HHomMat2D(); HRegion ModelRegionTrans; HTuple row1Check, col1Check; HTuple row2Check, col2Check; HRegion Rectangle1 = new HRegion(); //HRegion Rectangle2 = new HRegion(); //HMeasure Measure1, Measure2; //HTuple RowEdgeFirst1, ColumnEdgeFirst1; //HTuple AmplitudeFirst1, RowEdgeSecond1; //HTuple ColumnEdgeSecond1, AmplitudeSecond1; //HTuple IntraDistance1, InterDistance1; //HTuple RowEdgeFirst2, ColumnEdgeFirst2; //HTuple AmplitudeFirst2, RowEdgeSecond2; //HTuple ColumnEdgeSecond2, AmplitudeSecond2; //HTuple IntraDistance2, InterDistance2; //HTuple MinDistance; //int NumLeads; //HSystem.SetSystem("flush_graphic", "false"); //Img.GrabImage(Framegrabber); //Img.DispObj(Window); // Find the IC in the current image. S1 = HSystem.CountSeconds(); ShapeModel.FindShapeModel(Img, 0, new HTuple(360).TupleRad().D, 0.7, 1, 0.5, "least_squares", 4, 0.9, out RowCheck, out ColumnCheck, out AngleCheck, out Score); S2 = HSystem.CountSeconds(); if (RowCheck.Length == 1) { //MatchingScoreLabel.Text = "Score: " + // String.Format("{0:F5}", Score.D); // Rotate the model for visualization purposes. //创建严格的仿射变换VectorAngleToRigid Matrix.VectorAngleToRigid(new HTuple((row1 + row2) / 2), new HTuple((col1 + col2) / 2), new HTuple(0.0), RowCheck - rowMark + rowOrg, ColumnCheck - colMark + colOrg, AngleCheck); //根据Matrix变换ModelRegion ModelRegionTrans = ModelRegion.AffineTransRegion(Matrix, "false"); window.SetColor("red"); window.SetDraw("margin"); window.SetLineWidth(2); ModelRegionTrans.DispObj(window); // Compute the parameters of the measurement rectangles. Matrix.AffineTransPixel(new HTuple(row1), new HTuple(col1), out row1Check, out col1Check); Matrix.AffineTransPixel(new HTuple(row2), new HTuple(col2), out row2Check, out col2Check); Rectangle1.GenRectangle1(row1Check, col1Check, row2Check, col2Check); window.SetColor("green"); window.SetDraw("margin"); window.SetLineWidth(1); Rectangle1.DispObj(window); window.SetColor("gold"); window.DispLine(RowCheck - 15, ColumnCheck, RowCheck + 15, ColumnCheck); window.DispLine(RowCheck, ColumnCheck - 15, RowCheck, ColumnCheck + 45); // For visualization purposes, generate the two rectangles as // regions and display them. //Rectangle1.GenRectangle2(Rect1RowCheck.D, Rect1ColCheck.D, // RectPhi + AngleCheck.D, // RectLength1, RectLength2); //Rectangle2.GenRectangle2(Rect2RowCheck.D, Rect2ColCheck.D, // RectPhi + AngleCheck.D, // RectLength1, RectLength2); //window.SetColor("blue"); //window.SetDraw("margin"); //Rectangle1.DispObj(window); //Rectangle2.DispObj(window); } //MatchingTimeLabel.Text = "Time: " + // String.Format("{0,4:F1}", (S2 - S1)*1000) + "ms"; //MatchingScoreLabel.Text = "Score: "; //{ // MatchingScoreLabel.Text = "Score: " + // String.Format("{0:F5}", Score.D); // // Rotate the model for visualization purposes. // Matrix.VectorAngleToRigid(new HTuple(Row), new HTuple(Column), new HTuple(0.0), // RowCheck, ColumnCheck, AngleCheck); // ModelRegionTrans = ModelRegion.AffineTransRegion(Matrix, "false"); //window.SetColor("green"); //window.SetDraw("fill"); ////ModelRegionTrans.DispObj(window); }
public bool FindCircle(HObject img) { HTuple Row = new HTuple(); HTuple Col = new HTuple(); HTuple Distance = new HTuple(); HTuple outRow = new HTuple(); HTuple outCol = new HTuple(); HTuple outAmp = new HTuple(); HTuple outDistance = new HTuple(); HRegion regionCount = new HRegion(); regionCount.GenEmptyRegion(); HXLDCont contour = new HXLDCont(); if (img == null) { return(false); } HTuple imgWidth, imgHeight; HTuple hms = null; HOperatorSet.GetImageSize(img, out imgWidth, out imgHeight); double xCenter, yCenter, Center_radius, angle, angleSplit; angle = 0; //分割角度 angleSplit = 2 * 3.1415926 / (Hnum); xCenter = in_col[0].D + data_col; yCenter = in_row[0].D + data_row; Center_radius = in_radius[0].D; HHomMat2D hom = new HHomMat2D(); hom.HomMat2dIdentity(); hom = hom.HomMat2dRotate(angleSplit, yCenter, xCenter); double rect_row, rect_col, angleStart, L1, L2; //分割矩形的中心坐标 rect_row = yCenter; rect_col = xCenter + Center_radius; L1 = HLengh[0].D / 2; L2 = Hwid[0].D / 2; if (Hdirection != "in2out") { //由外向内 angleStart = angle + (new HTuple(180)).TupleRad(); } else { angleStart = angle; } for (int i = 0; i < Hnum[0].I; ++i) { if (m_bShowRectang2) { contour.Dispose(); contour.GenRectangle2ContourXld(rect_row, rect_col, angleStart + angleSplit * i, L1, L2); HRegion region; region = contour.GenRegionContourXld("margin"); regionCount = regionCount.Union2(region); region.Dispose(); } try {//out_row, out_col, out_radius; HOperatorSet.GenMeasureRectangle2(rect_row, rect_col, angleStart + angleSplit * i, L1, L2, imgWidth, imgHeight, "nearest_neighbor", out hms); HOperatorSet.MeasurePos(img, hms, Hsomth, Hamp, Htransition, Hselect, out outRow, out outCol, out outAmp, out outDistance); } catch { continue; } if (Hselect == "all" && outRow.Length > 1) { HTuple hIndex = outAmp.TupleSortIndex(); int nMax = hIndex[hIndex.Length - 1]; Row.Append(outRow[nMax]); Col.Append(outCol[nMax]); } else { Row.Append(outRow); Col.Append(outCol); } hom.AffineTransPixel(rect_row, rect_col, out rect_row, out rect_col); } HOperatorSet.CloseMeasure(hms); if (m_bShowRectang2) { m_hRectang2.Dispose(); m_hRectang2 = regionCount.GenContoursSkeletonXld(1, "filter"); } if (Row.Length > Hnum[0].D / 3) { HXLDCont counter = new HXLDCont(); counter.GenContourPolygonXld(Row, Col); HTuple startphi, endphi; HTuple pointorder; counter.FitCircleContourXld("atukey", -1, 0, 0, 3, 2, out out_row, out out_col, out out_radius, out startphi, out endphi, out pointorder); HTuple rowDrop = new HTuple(); HTuple colDrop = new HTuple(); if (Row.Length - 5 > m_nNumDropPoints && m_nNumDropPoints > 0) { HTuple distance = new HTuple(); for (int i = 0; i < Row.Length; ++i) { double dis = HMisc.DistancePp(Row[i], Col[i], xCenter, yCenter); distance.Append(Math.Abs(dis - Center_radius)); } HTuple index = distance.TupleSortIndex(); index = index.TupleInverse(); for (int i = 0; i < m_nNumDropPoints; ++i) { int n = index[i]; rowDrop.Append(Row[n]); colDrop.Append(Col[n]); } index = index.TupleFirstN(m_nNumDropPoints - 1); Row = Row.TupleRemove(index); Col = Col.TupleRemove(index); counter.GenContourPolygonXld(Row, Col); counter.FitCircleContourXld("atukey", -1, 0, 0, 3, 2, out out_row, out out_col, out out_radius, out startphi, out endphi, out pointorder); } m_hCircle.Dispose(); m_centerCross.Dispose(); if (m_bShowCircle) { HOperatorSet.GenCrossContourXld(out m_centerCross, out_row, out_col, (out_radius / 2) + 1, (new HTuple(0)).TupleRad()); HOperatorSet.GenCircleContourXld(out m_hCircle, out_row, out_col, out_radius, 0, 3.1415926 * 2, "positive", 1); } m_hCross.Dispose(); if (m_bShowCross) { HOperatorSet.GenCrossContourXld(out m_hCross, Row, Col, 17, (new HTuple(45)).TupleRad()); } Result_Array[0] = out_row; Result_Array[1] = out_col; Result_Array[2] = out_radius; return(true); } return(false); }