public MainWindow() { InitializeComponent(); m_Control = new ViDi2.Runtime.Local.Control(); m_Control.Workspaces.Add("jesus", @"C:\Users\jkhong\Desktop\IDontKnow3.vrws"); m_Workspace = m_Control.Workspaces.First(); m_Stream = m_Workspace.Streams.First(); using (IImage image = new ViDi2.VisionPro.Image(new CogImage24PlanarColor(new Bitmap(@"C:\Users\jkhong\Desktop\라벨.bmp")))) { ISample sample = m_Stream.Process(image); IRedMarking red = null; IImage result = null; foreach (var a in sample.Markings) { red = a.Value as IRedMarking; result = red.OverlayImage(0, ""); result.Bitmap.Save(@"C:\Users\jkhong\Desktop\babo.bmp", System.Drawing.Imaging.ImageFormat.Bmp); } IImage resultOriginal = red.ViewImage(0); //var aa = red.Views[0] as IRedView; //aa. //result = aa.HeatMap; result.Bitmap.Save(@"C:\Users\jkhong\Desktop\babo.bmp", System.Drawing.Imaging.ImageFormat.Bmp); imageControl.Source = Convert(resultOriginal.Bitmap); imageControl_Copy.Source = Convert(result.Bitmap); } }
private void Process(string path) { foreach (var tool in m_Stream.Tools) { if (tool is ViDi2.IRedTool redTool) { //이미지 프로세스 using (Bitmap bitmap = new Bitmap(path)) { try { //ViDi2.VisionPro.Image image = new ViDi2.VisionPro.Image(new CogImage8Grey(bitmap)); ViDi2.IImage image = new ViDi2.FormsImage(bitmap); ViDi2.ISample sample = m_Stream.Process(image); foreach (var marking in sample.Markings) { if (marking.Value is ViDi2.IRedMarking redMarking) { #region VisionPro CogRecordDisplay에 ViDi검사 결과 변환하여 표시하기 //CogRecord 생성 CogRecord record = new CogRecord(); //record.Content = image.InternalImage; record.Content = image.Bitmap; //ViDi 검사결과를 VisionPro Record로 변환 CogGraphicCollection gc = new CogGraphicCollection(); ViDi2.VisionPro.RedViewRecord redViewRecord = new ViDi2.VisionPro.RedViewRecord(redMarking.Views[0] as ViDi2.IRedView, new ViDi2.VisionPro.Records.DefaultRedToolGraphicCreator()); if (redViewRecord.HasGraphics && redViewRecord.GraphicsVisible) { foreach (ICogRecord icg in redViewRecord.SubRecords) { gc.Add(icg.Content as ICogGraphic); } } record.SubRecords.Add(new CogRecord() { Content = gc, ContentType = typeof(CogGraphicCollection) }); //CogRecordDisplay에 Record 삽입 CogRecordDisplay.Record = record; CogRecordDisplay.Fit(); #endregion #region WPF Image control 에 ViDi 검사결과 표시 ViDi2.IImage overlay = redMarking.OverlayImage(); Bitmap result = new Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics graphics = Graphics.FromImage(result)) { graphics.DrawImage(bitmap, 0, 0); graphics.DrawImage(overlay.Bitmap, 0, 0); } using (var memory = new MemoryStream()) { result.Save(memory, ImageFormat.Bmp); memory.Position = 0; var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); bitmapImage.Freeze(); BitmapImageSource = bitmapImage; PropertyChangedEvent(nameof(BitmapImageSource)); } #endregion } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } }
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(); }