public HRegion TransRegion(HRegion region) { 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) { region = matd.AffineTransRegion(region, "nearest_neighbor"); return(region); } } catch (Exception ex) { WriteErrorLog("VisionTool", ToolName + "模板" + ex.ToString()); } return(null); }
public HXLDPoly TransPolygonXLD(HXLDPoly xldPoly) { 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) { xldPoly = matd.AffineTransPolygonXld(xldPoly); return(xldPoly); } } catch (Exception ex) { WriteErrorLog("VisionTool", ToolName + "模板" + ex.ToString()); } return(null); }
public HImage TransImage(HImage image) { 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) { image = matd.AffineTransImage(image, "constant", "false"); return(image); } } catch (Exception ex) { WriteErrorLog("VisionTool", ToolName + "模板" + ex.ToString()); } return(null); }
public void ShowShapeModel(HWndCtrl viewCtrl) { if (modelRegion == null || !modelRegion.IsInitialized()) { return; } if (ModelXLDCont == null || !ModelXLDCont.IsInitialized()) { return; } viewCtrl.ChangeGraphicSettings(Mode.DRAWMODE, "margin"); if (modelRegion != null && modelRegion.IsInitialized()) { viewCtrl.ChangeGraphicSettings(Mode.COLOR, "blue"); viewCtrl.ChangeGraphicSettings(Mode.LINEWIDTH, 2); viewCtrl.AddIconicVar(modelRegion); } if (modelXLDContAffine == null) { modelXLDContAffine = new HXLDCont(); } if (modelXLDContAffine != null && modelXLDContAffine.IsInitialized()) { modelXLDContAffine.Dispose(); } if (refCoordinates == null || refCoordinates.Length != 3) { return; } HHomMat2D homMat2D = new HHomMat2D(); homMat2D.VectorAngleToRigid(0.0, 0.0, 0.0, refCoordinates[0].D, refCoordinates[1].D, refCoordinates[2].D); modelXLDContAffine = homMat2D.AffineTransContourXld(ModelXLDCont); if (modelXLDContAffine != null && modelXLDContAffine.IsInitialized()) { viewCtrl.ChangeGraphicSettings(Mode.COLOR, "red"); viewCtrl.ChangeGraphicSettings(Mode.LINEWIDTH, 1); viewCtrl.AddIconicVar(modelXLDContAffine); } }
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); }
public List <HHomMat2D> GetHHomMat2Ds() { if (mat2Ds == null) { mat2Ds = new List <HHomMat2D>(); } else { mat2Ds.Clear(); } for (int i = 0; i < row.Length; i++) { HHomMat2D homMat2D = new HHomMat2D(); homMat2D.VectorAngleToRigid( createShapeModel.refCoordinates[0].D, createShapeModel.refCoordinates[1].D, createShapeModel.refCoordinates[2].D, row[i].D, column[i].D, angle[i].D); mat2Ds.Add(homMat2D); } return(mat2Ds); }
private void dataGridView1_Click(object sender, EventArgs e) { DisplayMatchResult(); if (dataGridView1.SelectedRows.Count > 0) { int index = dataGridView1.SelectedRows[0].Index; HHomMat2D hHomMat2D = new HHomMat2D(); hHomMat2D.VectorAngleToRigid(0, 0, 0, nCCModel.OutputResult.Row[index].D, nCCModel.OutputResult.Col[index].D, nCCModel.OutputResult.Angle[index].D); hHomMat2D = hHomMat2D.HomMat2dTranslateLocal(-nCCModel.ModelRegion.Row, -nCCModel.ModelRegion.Column); HRegion region = nCCModel.ModelRegion.AffineTransRegion(hHomMat2D, "constant"); hWndCtrller.ChangeGraphicSettings("Color", "red"); hWndCtrller.AddIconicVar(region); HObject cross; HOperatorSet.GenCrossContourXld(out cross, nCCModel.OutputResult.Row[index].D, nCCModel.OutputResult.Col[index].D, 16, 0); hWndCtrller.AddIconicVar(cross); hWndCtrller.Repaint(); } }
private void DisplayModelControur() { hWndCtrller.AddIconicVar(Model.ModelImg); hWndCtrller.ChangeGraphicSettings("Color", "blue"); hWndCtrller.AddIconicVar(Model.SearchRegion); hWndCtrller.ChangeGraphicSettings("Color", "green"); hWndCtrller.ChangeGraphicSettings("DrawMode", "margin"); HHomMat2D mat2D = new HHomMat2D(); mat2D.VectorAngleToRigid(0.0, 0.0, 0.0, Model.ModelImgRow, Model.ModelImgCol, Model.ModelimgAng); HXLDCont cont = Model.GetModelCont(); if (cont != null) { double row, col; Model.shapeModel.GetShapeModelOrigin(out row, out col); mat2D = mat2D.HomMat2dTranslateLocal(row, col); hWndCtrller.AddIconicVar(cont.AffineTransContourXld(mat2D)); cont.Dispose(); } }
private void DisplayMatchResult() { hWndCtrller.AddIconicVar(nCCModel.InputImg); hWndCtrller.ChangeGraphicSettings("Color", "blue"); hWndCtrller.AddIconicVar(nCCModel.SearchRegion); hWndCtrller.ChangeGraphicSettings("Color", "green"); hWndCtrller.ChangeGraphicSettings("DrawMode", "margin"); for (int i = 0; i < nCCModel.OutputResult.Count; i++) { HHomMat2D hHomMat2D = new HHomMat2D(); hHomMat2D.VectorAngleToRigid(0, 0, 0, nCCModel.OutputResult.Row[i].D, nCCModel.OutputResult.Col[i].D, nCCModel.OutputResult.Angle[i].D); hHomMat2D = hHomMat2D.HomMat2dTranslateLocal(-nCCModel.ModelRegion.Row, -nCCModel.ModelRegion.Column); HRegion region = nCCModel.ModelRegion.AffineTransRegion(hHomMat2D, "constant"); hWndCtrller.AddIconicVar(region); } if (nCCModel.OutputResult.Count > 0) { HObject cross; HOperatorSet.GenCrossContourXld(out cross, nCCModel.OutputResult.Row, nCCModel.OutputResult.Col, 16, 0); hWndCtrller.AddIconicVar(cross); } }
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); } }
public HXLDCont FindShapeModel(HImage img, HShapeModel model, FindModelParameter findModelParameter, out HTuple row, out HTuple column, out HTuple angle, out HTuple scale, out HTuple score) { var t1 = HSystem.CountSeconds(); model.SetShapeModelParam("timeout", 10000); try { img.FindScaledShapeModel( model, findModelParameter.angleStart, findModelParameter.angleExtent, findModelParameter.scaleMin, findModelParameter.scaleMax, findModelParameter.minScore, findModelParameter.numMatches, findModelParameter.maxOverlap, findModelParameter.subPixel, findModelParameter.numLevels, findModelParameter.greediness, out row, out column, out angle, out scale, out score); } catch (Exception e) { Console.WriteLine(e); row = column = angle = scale = score = new HTuple(); } var t2 = HSystem.CountSeconds(); var mTime = 1000.0 * (t2 - t1); System.Diagnostics.Debug.WriteLine(mTime.ToString()); var modelContours = Model.GetShapeModelContours(1); var homMat2D = new HHomMat2D(); //HTuple Width, Height; //Img.GetImageSize(out Width, out Height); if (score.Length <= 0) { return(null); } homMat2D.VectorAngleToRigid(0, 0, 0, row, column, angle); var contoursAffinTrans = modelContours.AffineTransContourXld(homMat2D); return(contoursAffinTrans); }
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); }
private void DefineMat2Ds() { if (mat2Ds == null) { mat2Ds = new List <HHomMat2D>(); } else { mat2Ds.Clear(); } for (int i = 0; i < row_temp.Length; i++) { HHomMat2D homMat2D = new HHomMat2D(); homMat2D.VectorAngleToRigid( createShapeModel.refCoordinates[0].D, createShapeModel.refCoordinates[1].D, createShapeModel.refCoordinates[2].D, row_temp[i].D, column_temp[i].D, angle_temp[i].D); HRegion boundary_region; if (SearchRegion == null | !SearchRegion.IsInitialized()) { boundary_region = refImage.GetDomain().Boundary("inner"); } else { boundary_region = SearchRegion.Boundary("inner"); } HTuple row_rect2, col_rect2, phi_rect2, lenght1_rect2, lenght2_rect2; if (this.createShapeModel.modelRegion == null || !this.createShapeModel.modelRegion.IsInitialized()) { return; } this.createShapeModel.modelRegion.SmallestRectangle2(out row_rect2, out col_rect2, out phi_rect2, out lenght1_rect2, out lenght2_rect2); HRegion small_rect2_region = new HRegion(); small_rect2_region.GenRectangle2(row_rect2, col_rect2, phi_rect2, lenght1_rect2, lenght2_rect2); HRegion small_rect2_region_affine = homMat2D.AffineTransRegion(small_rect2_region, "nearest_neighbor"); HObject Area_temp; HOperatorSet.Intersection(boundary_region, small_rect2_region_affine, out Area_temp); HTuple area, row_tt, col_tt; HOperatorSet.AreaCenter(Area_temp, out area, out row_tt, out col_tt); if (area.D == 0) { mat2Ds.Add(homMat2D); row = row.TupleConcat(row_temp[i]); column = column.TupleConcat(column_temp[i]); angle = angle.TupleConcat(angle_temp[i]); scale = scale.TupleConcat(scale_temp[i]); score = score.TupleConcat(score_temp[i]); } if (boundary_region != null && boundary_region.IsInitialized()) { boundary_region.Dispose(); } if (small_rect2_region_affine != null && small_rect2_region_affine.IsInitialized()) { small_rect2_region_affine.Dispose(); } if (small_rect2_region != null && small_rect2_region.IsInitialized()) { small_rect2_region.Dispose(); } if (Area_temp != null && Area_temp.IsInitialized()) { Area_temp.Dispose(); } } }
// Procedure to find the coins and to read the serialized item from a // file stream private void Action() { HRegion Coin, Circle; HTuple Row, Column, Angle, Score, ResultModel; if (ImgNum == 0) { MatchingLabel.Text = "Deserialize shape models..."; MatchingLabel.Refresh(); //Reading names Stream s = File.OpenRead("serialized_shape_model"); Names = HTuple.Deserialize(s); //Reading shape models for (int i = 0; i < 4; i++) { Models[i] = HShapeModel.Deserialize(s); } s.Close(); MatchingLabel.Text = "Deserialize shape models... OK"; MatchingLabel.Refresh(); } if (ImgNum == 13) { ImgNum = 0; } HTuple Zero; if (ImgNum + 1 < 10) { Zero = "0"; } else { Zero = ""; } //Find shape based model using the read models Img.ReadImage("coins/20cent_" + Zero + (ImgNum + 1).ToString() + ".png"); ImgNum++; HRegion Region = Img.Threshold(70.0, 255.0); HRegion ConnectedRegions = Region.Connection(); HRegion SelectedRegions = ConnectedRegions.SelectShapeStd("max_area", 0); HRegion RegionTrans = SelectedRegions.ShapeTrans("convex"); Coin = new HRegion(RegionTrans.Row, RegionTrans.Column, 120); Circle = new HRegion(Coin.Row, Coin.Column, 35); HImage ImgReduced = Img.ReduceDomain(Circle); ImgReduced.FindShapeModels(Models, 0.0, new HTuple(360.0).TupleRad(), 0.6, 1, 0, "interpolation", 0, 0.9, out Row, out Column, out Angle, out Score, out ResultModel); HXLDCont ModelContours = Models[ResultModel.I].GetShapeModelContours(1); HHomMat2D HomMat2D = new HHomMat2D(); Window.SetColor("green"); HomMat2D.VectorAngleToRigid(0, 0, 0, Row, Column, Angle); HXLDCont ContoursAffineTrans = ModelContours.AffineTransContourXld(HomMat2D); HSystem.SetSystem("flush_graphic", "false"); Window.DispObj(Img); HSystem.SetSystem("flush_graphic", "true"); Window.DispObj(ContoursAffineTrans); MatchingLabel.Text = "#" + (ImgNum + 1) + ": Found: " + Names[ResultModel.I] + " coin"; }