예제 #1
0
        // =========================================================
        private void DrawCirclesFunct(Bitmap bitmap, VideoProcessing vp)
        {
            List <Shapes.Circle> Circles = VideoDetection.FindCircles(vp, bitmap);

            using (Graphics g = Graphics.FromImage(bitmap))
                using (Pen pen = new Pen(Color.DarkOrange, 2))
                    for (int i = 0, n = Circles.Count; i < n; i++)
                    {
                        Circles[i].ToRawResolution();
                        g.DrawEllipse(pen,
                                      (float)(Circles[i].X - Circles[i].Radius), (float)(Circles[i].Y - Circles[i].Radius),
                                      (float)(Circles[i].Radius * 2), (float)(Circles[i].Radius * 2));
                    }
        }
        private static PartLocation DoCameraCalibration(VideoProcessing vp, PartLocation movement)
        {
            var cnc      = Global.Instance.cnc;
            var distance = new List <PartLocation>();
            var pixels   = new List <PartLocation>();

            // turn on slack compensation
            var savedSlackCompensation = cnc.SlackCompensation;

            cnc.SlackCompensation = true;

            var startingPos = cnc.XYLocation;

            startingPos.A = 0;

            vp.MarkA.Clear();

            for (int i = -4; i < 5; i++)
            {
                //move
                var newLocation = startingPos + (i * movement);
                if (!cnc.CNC_XYA(newLocation))
                {
                    return(null);
                }

                //try 5 times to find a circle
                List <Shapes.Circle> circles = new List <Shapes.Circle>();
                for (int tries = 5; tries > 0 && circles.Count == 0; tries--)
                {
                    circles = VideoDetection.FindCircles(vp);
                }
                if (circles.Count == 0)
                {
                    continue;                     //not found, move and try again
                }
                //find largest circle of the bunch
                var circle = circles.Aggregate((c, d) => c.Radius > d.Radius ? c : d); //find largest circle if we have multiple
                //  var circlePL = (1 / zoom) * circle.ToPartLocation(); //compensate for zoom
                circle.ToScreenUnzoomedResolution();
                distance.Add(newLocation);
                pixels.Add(circle.ToPartLocation());

                vp.MarkA.Add(circle.Clone().ToScreenResolution().ToPartLocation().ToPointF());
                //DisplayText(String.Format("Actual Loc = {0}\t Measured Loc = {1}", newLocation, UpCamera.PixelsToActualLocation(circlePL)), Color.Blue);
            }


            double XmmPerPixel = 0, YmmPerPixel = 0;

            if (pixels.Count < 2)
            {
                Global.Instance.mainForm.ShowMessageBox("Unable To Detect Circles",
                                                        "Try to adjust upcamera processing to see circles, and ensure upcamera needle position is correctly configured",
                                                        MessageBoxButtons.OK);
            }
            else
            {
                // Do regression on X and Y
                var Xs     = pixels.Select(xx => xx.X).ToArray();
                var Ys     = distance.Select(xx => xx.X).ToArray();
                var result = SimpleRegression.Fit(Xs, Ys);
                XmmPerPixel = result.Item2;

                Xs          = pixels.Select(xx => xx.Y).ToArray();
                Ys          = distance.Select(xx => xx.Y).ToArray();
                result      = SimpleRegression.Fit(Xs, Ys);
                YmmPerPixel = result.Item2;


                Global.Instance.DisplayText(String.Format("{0} Xmm/pixel   {1} Ymm/pixel", XmmPerPixel, YmmPerPixel), Color.Purple);

                // Now move to the center

                /* need to get gotolocation upcamera working still
                 * double X, Y; //error offset
                 * GotoUpCamPosition_button_Click(null, null);
                 * for (int tries = 5; tries > 0; tries--) {
                 *   if (GoToLocation_m(UpCamera, Shapes.ShapeTypes.Circle, 1.8, 0.5, out X, out Y)) {
                 *       Properties.Settings.Default.UpCam_PositionX = Cnc.CurrentX + X;
                 *       Properties.Settings.Default.UpCam_PositionY = Cnc.CurrentY - Y;
                 *       UpcamPositionX_textBox.Text = Properties.Settings.Default.UpCam_PositionX.ToString("0.00", CultureInfo.InvariantCulture);
                 *       UpcamPositionY_textBox.Text = Properties.Settings.Default.UpCam_PositionY.ToString("0.00", CultureInfo.InvariantCulture);
                 *
                 *   }
                 * }
                 */
            }

            //restore settings
            cnc.SlackCompensation = savedSlackCompensation;
            vp.MarkA.Clear();
            //return value
            return((pixels.Count < 2) ? null : new PartLocation(Math.Abs(XmmPerPixel), Math.Abs(YmmPerPixel)));
        }