Esempio n. 1
0
 private void GetOriginImage()
 {
     try
     {
         using (Mat img = CvInvoke.Imread(@"img\origin.bmp", Emgu.CV.CvEnum.ImreadModes.Grayscale))
         {
             Image <Gray, byte> _img = img.ToImage <Gray, byte>();
             _img = ComputerVison.RoiImage(_img, Config.Parameter.ROI);
             VectorOfPoint cnt = new VectorOfPoint();
             cnt = ComputerVison.FindContours(_img, Config.Parameter.THRESHOLD_VALUE);
             RotatedRect a = CvInvoke.MinAreaRect(cnt);
             ORGRec         = a.MinAreaRect();
             ORGRec.X      += Config.Parameter.ROI.X - 10;
             ORGRec.Y      += Config.Parameter.ROI.Y - 10;
             ORGRec.Height += 20;
             ORGRec.Width  += 20;
             using (Image <Bgr, byte> iBgr2 = _img.Convert <Bgr, byte>())
             {
                 CvInvoke.Rectangle(iBgr2, ORGRec, new MCvScalar(0, 255, 0), 3);
                 CvInvoke.Imwrite("img\\originRoi.bmp", iBgr2);
             }
             Point[] p = ComputerVison.Search2Tip(cnt);
             ORG.SetPointA(p[0]);
             ORG.SetPointB(p[1]);
             cnt.Dispose();
             _img.Dispose();
         }
     }
     catch (Exception er)
     {
         MessageBox.Show(er.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
Esempio n. 2
0
        private void Handling()
        {
            Response result = new Response();
            string   name   = DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString() + "_" + DateTime.Now.Second.ToString();

            Light_Mode(modeCamera.Checked);
            Thread.Sleep(500);
            Image <Bgr, byte> iBgr = GetImage();

            Light_Mode(false);
            Image <Gray, byte> iGray = iBgr.Convert <Gray, byte>();

            iGray = ComputerVison.RoiImage(iGray, Config.Parameter.ROI);
            VectorOfPoint cnt = new VectorOfPoint();

            Point[] p = new Point[2];
            cnt = ComputerVison.FindContours(iGray, Config.Parameter.THRESHOLD_VALUE);
            if (cnt != null)
            {
                p = ComputerVison.Search2Tip(cnt);
                ComputerVison.Calculator(ref result, ORG.PointA, ORG.PointB, p[0], p[1], Config.Parameter.LabelSize.Width, false);
                ComputerVison.RouPoint(ORG.PointO, ref p[0], result.ANGLE);
                ComputerVison.RouPoint(ORG.PointO, ref p[1], result.ANGLE);
                ComputerVison.Calculator(ref result, ORG.PointA, ORG.PointB, p[0], p[1], Config.Parameter.LabelSize.Width, true);
                short y = (short)Math.Round(result.X * Config.Parameter.PULSE_Y + 150);
                short x = (short)Math.Round(result.Y * Config.Parameter.PULSE_X + 20);
                short z = (short)Math.Round(-result.ANGLE * Config.Parameter.PULSE_Z / 360);
                if (modeCamera.Checked)
                {
                    PLCCommunicate(x, y, z);
                }
                //log.Invoke(new MethodInvoker(delegate ()
                //{
                //    log.Text = "X:" + x.ToString("F3") + " Y:" + y.ToString("F3") + " Angle:" + z.ToString();
                //}));
                iGray = iBgr.Convert <Gray, byte>();
                ComputerVison.RotationImage(ref iGray, Config.Parameter.ROTATION_CENTER, (float)result.ANGLE);
                cnt = ComputerVison.FindContours(iGray, Config.Parameter.THRESHOLD_VALUE);
                using (Image <Bgr, byte> iBgr2 = iGray.Convert <Bgr, byte>())
                {
                    if (cnt != null)
                    {
                        RotatedRect r = CvInvoke.MinAreaRect(cnt);
                        CvInvoke.Rectangle(iBgr2, r.MinAreaRect(), new MCvScalar(0, 255, 0), 3);
                    }
                    if (modeCamera.Checked)
                    {
                        CvInvoke.Imwrite(@"backup\" + name + ".bmp", iBgr);
                        CvInvoke.Imwrite(@"backup_H\" + name + "trans.bmp", iBgr2);
                    }
                    p_imShow.Invoke(new MethodInvoker(delegate()
                    {
                        p_imShow.Image = iBgr2.ToBitmap();
                    }));
                }
                cnt.Dispose();
            }
            else
            {
                using (Image <Bgr, byte> iBgr2 = iGray.Convert <Bgr, byte>())
                {
                    CvInvoke.Rectangle(iBgr2, ORGRec, new MCvScalar(0, 255, 0), 3);
                    p_imShow.Invoke(new MethodInvoker(delegate()
                    {
                        p_imShow.Image = iBgr2.ToBitmap();
                    }));
                }
                DialogResult kq = MessageBox.Show("Not found label from images! You want to try again!", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (kq == DialogResult.Yes)
                {
                    iGray.Dispose();
                    iBgr.Dispose();
                    Handling();
                }
                else
                {
                    result.ANGLE = 0;
                    result.X     = 0;
                    result.Y     = 0;
                }
            }
            iGray.Dispose();
            iBgr.Dispose();
        }