Esempio n. 1
0
        /// <summary>
        /// Detects circles on gasket image
        /// </summary>
        private static void DetectCircles(Image gasketImage, out Circle2D[] detectedCircles, out Segment2D[] conntectingSegments,
                                          CoordinateSystem2D localSystem)
        {
            using (CircleFittingMap
                   circleMap1 = new CircleFittingMap(),
                   circleMap2 = new CircleFittingMap())
            {
                AVL.CreateCircleFittingMap(new ImageFormat(gasketImage),
                                           new CircleFittingField(largeExpectedCircle, 35.0f),

                                           localSystem, 16, 5, InterpolationMethod.Bilinear,
                                           circleMap1);

                AVL.CreateCircleFittingMap(new ImageFormat(gasketImage),
                                           new CircleFittingField(smallExpectedCircle, 35.0f),
                                           localSystem, 16, 5, InterpolationMethod.Bilinear,
                                           circleMap2);

                Circle2D?largeCircle, smallCircle;

                AVL.FitCircleToEdges(gasketImage,
                                     circleMap1,
                                     scanParams,
                                     Selection.Best, 0.1f, CircleFittingMethod.GeometricLandau,
                                     out largeCircle);

                AVL.FitCircleToEdges(gasketImage,
                                     circleMap2,
                                     scanParams,
                                     Selection.Best, 0.1f, CircleFittingMethod.GeometricLandau,
                                     out smallCircle);

                conntectingSegments = new Segment2D[2];

                if (largeCircle.HasValue && smallCircle.HasValue)
                {
                    detectedCircles        = new[] { largeCircle.Value, smallCircle.Value };
                    conntectingSegments[0] = new Segment2D(detectedCircles[0].Center, detectedCircles[1].Center);
                }
                else
                {
                    detectedCircles = new Circle2D[0];
                }
            }
        }
        /// <summary>
        /// Loads, process and returns next image
        /// </summary>
        /// <returns>Badge image with measurement results drawn on it</returns>
        public void GetNextMeasuredImage(Image image)
        {
            if (imagePaths == null || !imagePaths.Any())
            {
                throw new ApplicationException("Image directory is undefined or no image could be found.");
            }

            if (currentImageIndex >= imagePaths.Length)
            {
                // start from beginning
                currentImageIndex = 0;
            }

            //mr:: 这里的Image是 AvlNet.Image
            AVL.LoadImage(imagePaths[currentImageIndex], false, image);

            // Reset last distance
            lastDistance = float.NaN;

            //mr:: 生成本地坐标系
            // Localize badge and get its local coordinate system
            var localSystem = GetBadgeLocalCoordinateSystem(image);


            using (var circleMap = new CircleFittingMap())
            {
                Circle2D?leftCircle, rightCircle;

                // Create fitting map for leftmost circle
                //mr:: 生成拟合Map
                AVL.CreateCircleFittingMap(
                    new ImageFormat(image),
                    new CircleFittingField(leftExpectedCircle, 10.0f),
                    localSystem, //mr:: 如果给定了local坐标系, 则 CircleFittingField就是基于local坐标系的.
                    20,
                    3,
                    InterpolationMethod.Bilinear,
                    circleMap);

                // Fit circle on image
                //mr:: 开始拟合(提供 拟合Map, EdgeScanParams等参数)
                AVL.FitCircleToEdges(image,
                                     circleMap,
                                     scanParams,
                                     Selection.Best,
                                     0.1f,
                                     CircleFittingMethod.GeometricLandau,
                                     out leftCircle);

                // Create fitting map for rightmost circle
                AVL.CreateCircleFittingMap(new ImageFormat(image),
                                           new CircleFittingField(rightExpectedCircle, 10.0f),
                                           localSystem, 20, 3,
                                           InterpolationMethod.Bilinear,
                                           circleMap);

                //Fit circle on image
                AVL.FitCircleToEdges(image, circleMap, scanParams,
                                     Selection.Best, 0.1f,
                                     CircleFittingMethod.GeometricLandau,
                                     out rightCircle);

                DrawResults(image, leftCircle, rightCircle);
            }

            ++currentImageIndex;
        }