コード例 #1
0
        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);
                        }
                    }
                }
            }
        }
コード例 #2
0
        public void MainTaskThread()
        {
            int count = 0;

            double constX = 0;
            double constY = 0;

            while (flag)
            {
                CogImage8Grey image = new CogImage8Grey();
                image = camControl.CamGrab();

                if (image == null)
                {
                    continue;
                }

                blob.InputImage = image;
                blob.Run();
                double xxx = 0;
                double yyy = 0;
                if (blob.Results.GetBlobs().Count != 0)
                {
                    xxx = blob.Results.GetBlobs()[0].CenterOfMassX;
                    yyy = blob.Results.GetBlobs()[0].CenterOfMassY;
                }

                CogRecord record = new CogRecord();
                record.SubRecords.Add(blob.CreateLastRunRecord().SubRecords[0]);

                if (count < 10)
                {
                    constX += xxx;
                    constY += yyy;

                    Dispatcher.Invoke(new Action(() =>
                    {
                        MinX_textBox.Text = xxx.ToString();
                        MaxX_textBox.Text = xxx.ToString();
                        MinY_textBox.Text = yyy.ToString();
                        MaxY_textBox.Text = yyy.ToString();
                    }));
                }
                else if (count == 10)
                {
                    constX = constX / 10;
                    constY = constY / 10;

                    Dispatcher.Invoke(new Action(() =>
                    {
                        StdX_textBox.Text = constX.ToString();
                        StdY_textBox.Text = constY.ToString();
                        MinX_textBox.Text = xxx.ToString();
                        MaxX_textBox.Text = xxx.ToString();
                        MinY_textBox.Text = yyy.ToString();
                        MaxY_textBox.Text = yyy.ToString();
                    }));
                }
                else if (count % 60 == 0)
                {
                    var bmp8bpp = Grayscale.CommonAlgorithms.BT709.Apply(image.ToBitmap());
                    bmp8bpp.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\SavedImages\Routine_SavedImage-" + DateTime.Now.ToString("yyyy_MM_dd-HH_mm_ss") + ".bmp", ImageFormat.Bmp);

                    Dispatcher.Invoke(new Action(() => {
                        double minx = double.Parse(MinX_textBox.Text);
                        double miny = double.Parse(MinY_textBox.Text);
                        double maxx = double.Parse(MaxX_textBox.Text);
                        double maxy = double.Parse(MaxY_textBox.Text);

                        if (xxx < minx)
                        {
                            MinX_textBox.Text = xxx.ToString();
                        }
                        if (yyy < miny)
                        {
                            MinY_textBox.Text = yyy.ToString();
                        }
                        if (xxx > maxx)
                        {
                            MaxX_textBox.Text = xxx.ToString();
                        }
                        if (yyy > maxy)
                        {
                            MaxY_textBox.Text = yyy.ToString();
                        }
                    }));
                }
                else
                {
                    Dispatcher.Invoke(new Action(() => {
                        if (Math.Abs(constX - xxx) >= 0.5 || Math.Abs(constY - yyy) >= 0.5)
                        {
                            int a = (int.Parse(AbnormalCount_textBox.Text) + 1);
                            AbnormalCount_textBox.Text = a.ToString();
                        }
                        var bmp8bpp = Grayscale.CommonAlgorithms.BT709.Apply(image.ToBitmap());
                        bmp8bpp.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\SavedImages\NG_SavedImage-" + DateTime.Now.ToString("yyyy_MM_dd-HH_mm_ss") + ".bmp", ImageFormat.Bmp);

                        double minx = double.Parse(MinX_textBox.Text);
                        double miny = double.Parse(MinY_textBox.Text);
                        double maxx = double.Parse(MaxX_textBox.Text);
                        double maxy = double.Parse(MaxY_textBox.Text);

                        if (xxx < minx)
                        {
                            MinX_textBox.Text = xxx.ToString();
                        }
                        if (yyy < miny)
                        {
                            MinY_textBox.Text = yyy.ToString();
                        }
                        if (xxx > maxx)
                        {
                            MaxX_textBox.Text = xxx.ToString();
                        }
                        if (yyy > maxy)
                        {
                            MaxY_textBox.Text = yyy.ToString();
                        }
                    }));
                }

                count++;
                Dispatcher.Invoke(new Action(() => {
                    TotalCount_textBox.Text = count.ToString();
                    display.Image           = image;
                    display.Record          = record;
                    display.Fit();
                }));

                TextWriter tw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\SavedLog.csv", true);
                tw.WriteLine(count + "," + xxx + "," + yyy + "," + DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss"));
                tw.Close();
                Thread.Sleep(900);
            }
        }