public bool GetPatternReference(CogImage8Grey _SrcImage, CogRectangle _Region, double _OriginX, double _OriginY, ref CogPMAlignPattern _Pattern) { //CogPMAlignPattern _Pattern = new CogPMAlignPattern(); _Pattern = new CogPMAlignPattern(); CogRectangleAffine _ReferRegionAffine = new CogRectangleAffine(); _ReferRegionAffine.SetCenterLengthsRotationSkew(_Region.CenterX, _Region.CenterY, _Region.Width, _Region.Height, 0, 0); CogAffineTransformTool _AffineTool = new CogAffineTransformTool(); _AffineTool.InputImage = _SrcImage; _AffineTool.Region = _ReferRegionAffine; _AffineTool.Run(); _Pattern.TrainImage = (CogImage8Grey)_AffineTool.OutputImage; _Pattern.TrainRegion = _ReferRegionAffine; _Pattern.Origin.TranslationX = _OriginX; _Pattern.Origin.TranslationY = _OriginY; try { _Pattern.Train(); } catch { return(false); } return(true); }
public bool Run(CogImage8Grey _SrcImage, ref CogImage8Grey _DestImage, CogRectangle _InspRegion, CogLineFindAlgo _CogLineFindAlgo, ref CogLineFindResult _CogLineFindResult, int _NgNumber = 0) { bool _Result = true; SetCaliperContrastAndHalfPixel(_CogLineFindAlgo.ContrastThreshold, _CogLineFindAlgo.FilterHalfSizePixels); SetCaliperDirection(_CogLineFindAlgo.CaliperSearchDirection); SetCaliper(_CogLineFindAlgo.CaliperNumber, _CogLineFindAlgo.CaliperSearchLength, _CogLineFindAlgo.CaliperProjectionLength, _CogLineFindAlgo.IgnoreNumber); SetCaliperLine(_CogLineFindAlgo.CaliperLineStartX, _CogLineFindAlgo.CaliperLineStartY, _CogLineFindAlgo.CaliperLineEndX, _CogLineFindAlgo.CaliperLineEndY); if (true == Inspection(_SrcImage)) { GetResult(); } if (FindLineResults != null && (_CogLineFindAlgo.CaliperNumber - _CogLineFindAlgo.IgnoreNumber) < (FindLineResults.NumPointsFound + 5)) { try { _CogLineFindResult.StartX = FindLineResults.GetLineSegment().StartX; _CogLineFindResult.StartY = FindLineResults.GetLineSegment().StartY; _CogLineFindResult.EndX = FindLineResults.GetLineSegment().EndX; _CogLineFindResult.EndY = FindLineResults.GetLineSegment().EndY; _CogLineFindResult.Length = FindLineResults.GetLineSegment().Length; _CogLineFindResult.Rotation = FindLineResults.GetLineSegment().Rotation; _CogLineFindResult.PointCount = FindLineResults.Count; #region Line segment 설정별로, 결과 각도별로 보정값 계산 //Radian 값으로 설정 //Line segment가 가로 //if (FindLineProc.RunParams.ExpectedLineSegment.Rotation >= -45 && FindLineProc.RunParams.ExpectedLineSegment.Rotation <= 45) if (FindLineProc.RunParams.ExpectedLineSegment.Rotation >= -0.785 && FindLineProc.RunParams.ExpectedLineSegment.Rotation <= 0.785) { _CogLineFindResult.LineRotation = FindLineResults.GetLineSegment().Rotation; } //Line segment가 가로 //else if ((FindLineProc.RunParams.ExpectedLineSegment.Rotation >= -180 && FindLineProc.RunParams.ExpectedLineSegment.Rotation <= -130) || // (FindLineProc.RunParams.ExpectedLineSegment.Rotation >= 135 && FindLineProc.RunParams.ExpectedLineSegment.Rotation < 180)) else if ((FindLineProc.RunParams.ExpectedLineSegment.Rotation >= -3.15 && FindLineProc.RunParams.ExpectedLineSegment.Rotation <= -2.26) || (FindLineProc.RunParams.ExpectedLineSegment.Rotation >= 2.26 && FindLineProc.RunParams.ExpectedLineSegment.Rotation < 3.14)) { if (_CogLineFindResult.Rotation > 0) { _CogLineFindResult.LineRotation = _CogLineFindResult.Rotation - 3.14159; } else { _CogLineFindResult.LineRotation = 3.14159 + _CogLineFindResult.Rotation; } } //Line segment가 세로(90도) else if (FindLineProc.RunParams.ExpectedLineSegment.Rotation >= 0.785 && FindLineProc.RunParams.ExpectedLineSegment.Rotation < 2.35) { _CogLineFindResult.LineRotation = (-1.57) + _CogLineFindResult.Rotation; } else if (FindLineProc.RunParams.ExpectedLineSegment.Rotation >= -2.35 && FindLineProc.RunParams.ExpectedLineSegment.Rotation < -0.785) { _CogLineFindResult.LineRotation = _CogLineFindResult.Rotation - (-1.57); } #endregion if (_CogLineFindAlgo.UseAlignment) { CogAffineTransformTool _CogTransForm = new CogAffineTransformTool(); CogRectangleAffine _AffineRegion = new CogRectangleAffine(); _AffineRegion.SetCenterLengthsRotationSkew(_InspRegion.CenterX, _InspRegion.CenterY, _InspRegion.Width, _InspRegion.Height, _CogLineFindResult.LineRotation, 0); _CogTransForm.InputImage = _SrcImage; _CogTransForm.Region = _AffineRegion; _CogTransForm.Run(); CogCopyRegionTool _CopyRegion = new CogCopyRegionTool(); _CopyRegion.InputImage = _CogTransForm.OutputImage; _CopyRegion.DestinationImage = _SrcImage; _CopyRegion.RunParams.ImageAlignmentEnabled = true; _CopyRegion.Region = null; _CopyRegion.Run(); _DestImage = (CogImage8Grey)_CopyRegion.OutputImage; if (true == Inspection(_DestImage)) { GetResult(); } if (FindLineResults != null) { _CogLineFindResult.StartX = FindLineResults.GetLineSegment().StartX; _CogLineFindResult.StartY = FindLineResults.GetLineSegment().StartY; _CogLineFindResult.EndX = FindLineResults.GetLineSegment().EndX; _CogLineFindResult.EndY = FindLineResults.GetLineSegment().EndY; _CogLineFindResult.Length = FindLineResults.GetLineSegment().Length; _CogLineFindResult.Rotation = FindLineResults.GetLineSegment().Rotation; _CogLineFindResult.PointCount = FindLineResults.Count; _CogLineFindResult.IsGood = true; } else { _CogLineFindResult.IsGood = false; } GC.Collect(); } else { _CogLineFindResult.Rotation = FindLineResults.GetLineSegment().Rotation; double _Rotation = 0; _Rotation = _CogLineFindResult.Rotation * 180 / Math.PI; _CogLineFindResult.IsGood = true; } } catch { _CogLineFindResult.IsGood = false; } } else { _CogLineFindResult.IsGood = false; } return(_Result); }
static void Main(string[] args) { // load image var bmp = new System.Drawing.Bitmap(@"D:\test.bmp"); #region Using Vision Pro //vision pro tool initializing CogCaliperTool calipertool = new CogCaliperTool(); //or load .vpp file (specific tool vpp) calipertool = CogSerializer.LoadObjectFromFile(@"D:\CaliperTool.vpp") as CogCaliperTool; //input processing image calipertool.InputImage = new CogImage8Grey(bmp); //process calipertool.Run(); //create result record var caliper_record = calipertool.CreateLastRunRecord().SubRecords[0]; //use result Console.Write("Edge Position is : X = {0}, Y = {1}", calipertool.Results[0].PositionX, calipertool.Results[0].PositionY); //save current tool (it saved specific tool vpp) CogSerializer.SaveObjectToFile(calipertool, @"D:\CaliperTool_Saved.vpp"); #endregion #region Using ViDi in Runtime //open ViDi control (it has to open only one in the application.) ViDi2.Runtime.Local.Control runtime_control = new ViDi2.Runtime.Local.Control(); //add runtime workspace (and open) ViDi2.Runtime.IWorkspace runtime_workspace = runtime_control.Workspaces.Add("TestWorkspace", @"D:\DeeplearningWorkspace.vrws"); //select stream in workspace ViDi2.Runtime.IStream runtime_stream = runtime_workspace.Streams.Single(s => s.Name.Equals("Stream")); //pack image for use ViDi ViDi2.IImage runtime_ViDiImg = new ViDi2.FormsImage(bmp); //process ViDi2.ISample runtime_sample = runtime_stream.Process(runtime_ViDiImg); //using red result ViDi2.IRedMarking redMarking = runtime_sample.Markings["Analyze"] as ViDi2.IRedMarking; //marking has a value for each view result var r_score = redMarking.Views[0].Score; var r_region = redMarking.Views[0].Regions; var r_pose = redMarking.Views[0].Pose; //using blue result ViDi2.IBlueMarking blueMarking = runtime_sample.Markings["Locate"] as ViDi2.IBlueMarking; var b_features = blueMarking.Views[0].Features; var b_pose = blueMarking.Views[0].Pose; //using green result ViDi2.IGreenMarking greenMarking = runtime_sample.Markings["Classify"] as ViDi2.IGreenMarking; var g_best_tag = greenMarking.Views[0].BestTag; var g_tags = greenMarking.Views[0].Tags; //free vidi image runtime_ViDiImg.Dispose(); //close workspace runtime_workspace.Close(); //close vidi control runtime_control.Dispose(); #endregion #region Using ViDi - Vision Pro Integration //open ViDi control (it has to open only one in the application.) ViDi2.Runtime.Local.Control integ_control = new ViDi2.Runtime.Local.Control(); //add runtime workspace (and open) ViDi2.Runtime.IWorkspace integ_workspace = integ_control.Workspaces.Add("TestWorkspace", @"D:\DeeplearningWorkspace.vrws"); //select stream in workspace ViDi2.Runtime.IStream integ_stream = integ_workspace.Streams.Single(s => s.Name.Equals("Stream")); //vision pro tool initializing CogAffineTransformTool transform_tool = new CogAffineTransformTool(); //or load .vpp file (specific tool vpp) transform_tool = CogSerializer.LoadObjectFromFile(@"D:\TransformTool.vpp") as CogAffineTransformTool; //input processing image transform_tool.InputImage = new CogImage8Grey(bmp); //process Vision Pro transform_tool.Run(); //pack vison pro output image for use ViDi ViDi2.IImage integ_ViDiImg = new ViDi2.VisionPro.Image(transform_tool.OutputImage); //process ViDi var integ_sample = integ_stream.Process(integ_ViDiImg); //create vision pro record var vp_record = transform_tool.CreateLastRunRecord().SubRecords[0]; //create ViDi record var integ_redMarking = integ_sample.Markings["Analyze"] as ViDi2.IRedMarking; var vidi_red_record = new ViDi2.VisionPro.RedViewRecord(integ_redMarking.Views[0] as ViDi2.IRedView, new ViDi2.VisionPro.Records.DefaultRedToolGraphicCreator()); var integ_blueMarking = integ_sample.Markings["Locate"] as ViDi2.IBlueMarking; var vidi_blue_record = new ViDi2.VisionPro.BlueViewRecord(integ_blueMarking.Views[0] as ViDi2.IBlueView, new ViDi2.VisionPro.Records.DefaultBlueToolGraphicCreator()); var integ_greenMarking = integ_sample.Markings["Classify"] as ViDi2.IGreenMarking; var vidi_green_record = new ViDi2.VisionPro.GreenViewRecord(integ_greenMarking.Views[0] as ViDi2.IGreenView, new ViDi2.VisionPro.Records.DefaultGreenToolGraphicCreator()); //free vidi image integ_ViDiImg.Dispose(); //close workspace integ_workspace.Close(); //close vidi control integ_control.Dispose(); #endregion //free image bmp.Dispose(); }
public bool Run(CogImage8Grey _SrcImage, ref CogImage8Grey _DestImage, CogRectangle _InspRegion, CogLineFindAlgo _CogLineFindAlgo, ref CogLineFindResult _CogLineFindResult, int _NgNumber = 0) { bool _Result = true; SetCaliperDirection(_CogLineFindAlgo.CaliperSearchDirection); SetCaliper(_CogLineFindAlgo.CaliperNumber, _CogLineFindAlgo.CaliperSearchLength, _CogLineFindAlgo.CaliperProjectionLength, _CogLineFindAlgo.IgnoreNumber); SetCaliperLine(_CogLineFindAlgo.CaliperLineStartX, _CogLineFindAlgo.CaliperLineStartY, _CogLineFindAlgo.CaliperLineEndX, _CogLineFindAlgo.CaliperLineEndY); if (true == Inspection(_SrcImage)) { GetResult(); } if (FindLineResults != null && (_CogLineFindAlgo.CaliperNumber - _CogLineFindAlgo.IgnoreNumber) < (FindLineResults.NumPointsFound + 5)) { try { _CogLineFindResult.StartX = FindLineResults.GetLineSegment().StartX; _CogLineFindResult.StartY = FindLineResults.GetLineSegment().StartY; _CogLineFindResult.EndX = FindLineResults.GetLineSegment().EndX; _CogLineFindResult.EndY = FindLineResults.GetLineSegment().EndY; _CogLineFindResult.Length = FindLineResults.GetLineSegment().Length; _CogLineFindResult.Rotation = FindLineResults.GetLineSegment().Rotation; _CogLineFindResult.PointCount = FindLineResults.Count; if (_CogLineFindAlgo.UseAlignment) { CogAffineTransformTool _CogTransForm = new CogAffineTransformTool(); CogRectangleAffine _AffineRegion = new CogRectangleAffine(); _AffineRegion.SetCenterLengthsRotationSkew(_InspRegion.CenterX, _InspRegion.CenterY, _InspRegion.Width, _InspRegion.Height, _CogLineFindResult.Rotation, 0); _CogTransForm.InputImage = _SrcImage; _CogTransForm.Region = _AffineRegion; _CogTransForm.Run(); CogCopyRegionTool _CopyRegion = new CogCopyRegionTool(); _CopyRegion.InputImage = _CogTransForm.OutputImage; _CopyRegion.DestinationImage = _SrcImage; _CopyRegion.RunParams.ImageAlignmentEnabled = true; _CopyRegion.Region = null; _CopyRegion.Run(); _DestImage = (CogImage8Grey)_CopyRegion.OutputImage; if (true == Inspection(_DestImage)) { GetResult(); } if (FindLineResults != null) { _CogLineFindResult.StartX = FindLineResults.GetLineSegment().StartX; _CogLineFindResult.StartY = FindLineResults.GetLineSegment().StartY; _CogLineFindResult.EndX = FindLineResults.GetLineSegment().EndX; _CogLineFindResult.EndY = FindLineResults.GetLineSegment().EndY; _CogLineFindResult.Length = FindLineResults.GetLineSegment().Length; _CogLineFindResult.Rotation = FindLineResults.GetLineSegment().Rotation; _CogLineFindResult.PointCount = FindLineResults.Count; _CogLineFindResult.IsGood = true; } else { _CogLineFindResult.IsGood = false; } GC.Collect(); } else { _CogLineFindResult.Rotation = FindLineResults.GetLineSegment().Rotation; double _Rotation = 0; _Rotation = _CogLineFindResult.Rotation * 180 / Math.PI; _CogLineFindResult.IsGood = true; } } catch { _CogLineFindResult.IsGood = false; } } else { _CogLineFindResult.IsGood = false; } return(_Result); }