Ejemplo n.º 1
0
        /// <summary>
        /// First Calibration Phase
        /// Performing:
        /// 1. Find and Sort Calibration marks
        /// 2. Calculate and set transformation matrix based on calibration marks
        /// 3. Calculate Ball radius and possible error
        /// </summary>
        /// <param name="frame"></param>
        private void Begin(IFrame frame)
        {
            //ignore first frames
            if (_skippedFrames < FRAMES_TO_SKIP)
            {
                _skippedFrames++;
            }
            else
            {
                try
                {
                    _skippedFrames = 0;

                    Log.Print("***** Starting calibration Phase I... *****", eCategory.Info, LogTag.IMAGE);
                    Image <Gray, byte> image = frame.Image.Clone();

                    //Remove Noise from picture
                    image = CalibrationUtils.PrepareFrame(image);
                    ComputerVisionMonitors[eComputerVisionMonitor.MonitorA].ShowFrame(image);

                    //Find Calibration Marks
                    List <CircleF> circles = FindCalibrationMarks(image);
                    CalibrationUtils.VerifyMarksFound(circles);

                    //Sort Calibration Marks and set value to property
                    SortCalibrationMarks(circles);

                    ShowAllCalibrationMarks();
                    StringBuilder str = new StringBuilder("4 calibration Marks found and sorted: \n\t\t\t\t");
                    foreach (var mark in ImagingData.CalibrationMarks)
                    {
                        str.Append(String.Format("{0}:[{1}x{2}] ", mark.Key, mark.Value.Center.X, mark.Value.Center.Y));
                    }
                    Log.Print(str.ToString(), eCategory.Info, LogTag.IMAGE);

                    CalibrationUtils.SetTransformationMatrix(AXE_X_LENGTH, AXE_Y_LENGTH, ImagingData.CalibrationMarks);
                    Log.Print("Homography matrix calculated.", eCategory.Info, LogTag.IMAGE);

                    //Calculate Ball Radius and Error
                    int    ballRadius;
                    double ballError;
                    CalibrationUtils.CalculateBallRadiusAndError((float)BALL_RADIUS_MM, out ballRadius, out ballError);

                    ImagingData.BallRadius      = ballRadius;
                    ImagingData.BallRadiusError = ballError;

                    Log.Print(String.Format("Expected ball radius is {0} +/- {1}",
                                            ImagingData.BallRadius, ImagingData.BallRadiusError), eCategory.Info, LogTag.IMAGE);

                    CurrentState = eCalibrationState.Performing;
                    Log.Print("***** Finished calibration Phase I *****", eCategory.Info, LogTag.IMAGE);
                }
                catch (CalibrationException ex)
                {
                    Log.Print(String.Format("Calibration failed in first phase. Will retry after [{0}] frames. Reason: {1}",
                                            FRAMES_TO_SKIP, ex.Message), eCategory.Warn, LogTag.IMAGE);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Second Calibration Phase
        /// Performing:
        /// 1. Find Calibration Marks to verify image is fine
        /// 2. Update Table Image Processing Coverage
        /// 3. Recalculate Homography matrices
        /// </summary>
        /// <param name="frame"></param>
        private void Finalize(IFrame frame)
        {
            //ignore first frames
            if (_skippedFrames < FRAMES_TO_SKIP)
            {
                _skippedFrames++;
            }
            else
            {
                try
                {
                    _skippedFrames = 0;

                    Log.Print("***** Starting calibration Phase II... *****", eCategory.Info, LogTag.IMAGE);
                    Image <Gray, byte> image = frame.Image.Clone();

                    //Remove Noise from picture
                    image = CalibrationUtils.PrepareFrame(image);
                    ComputerVisionMonitors[eComputerVisionMonitor.MonitorA].ShowFrame(image);

                    //Find Calibration Marks
                    List <CircleF> circles = FindCalibrationMarks(image);
                    CalibrationUtils.VerifyMarksFound(circles);

                    //Update coverage
                    ImagingData.CalibrationMarks = CalibrationUtils.UpdateCoverage(ImagingData.CalibrationMarks);
                    Log.Print("Coverage and marks updated.", eCategory.Info, LogTag.IMAGE);

                    //Show table border marks
                    Marks.DrawTableBorders(ImagingData.CalibrationMarks);

                    //Recalculate Homography Matrix
                    CalibrationUtils.SetTransformationMatrix(AXE_X_LENGTH, AXE_Y_LENGTH, ImagingData.CalibrationMarks);
                    Log.Print("Homography matrix re-calculated.", eCategory.Info, LogTag.IMAGE);

                    CurrentState = eCalibrationState.Finished;
                    Log.Print("***** Finished calibration Phase II *****", eCategory.Info, LogTag.IMAGE);
                }
                catch (CalibrationException ex)
                {
                    Log.Print(String.Format("Calibration failed in second phase. Will retry after [{0}] frames. Reason: {1}",
                                            FRAMES_TO_SKIP, ex.Message), eCategory.Warn, LogTag.IMAGE);
                }
            }
        }