Пример #1
0
        protected PointF GetPositionFromBinary(Emgu.CV.Image <Emgu.CV.Structure.Gray, Byte> binaryImage, bool drawPositionInfo)
        {
            int maxNumberOfObects = 50;
            int minObjectArea     = 30 * 30;
            int maxObjectArea     = (int)(binaryImage.Size.Width * binaryImage.Size.Height / 1.05);

            ratPosition = new IncorrectPosition();
            //Point positionNotFound = new Point(50000, 50000);
            //var positionNotFound = new PointF(float.NaN, float.NaN);
            Emgu.CV.Image <Emgu.CV.Structure.Gray, Byte> temporaryBinaryImage = binaryImage.Clone();
            Emgu.CV.Util.VectorOfVectorOfPoint           contours             = new Emgu.CV.Util.VectorOfVectorOfPoint();
            Emgu.CV.Mat hierarchy = new Mat();
            CvInvoke.FindContours(temporaryBinaryImage, contours, hierarchy, RetrType.Ccomp, ChainApproxMethod.ChainApproxSimple);
            if (contours.Size > 0)
            {
                int numberOfObjects = contours.Size;
                //var h = hierarchy.GetInputOutputArray();
                if (numberOfObjects < maxNumberOfObects)
                {
                    float referenceArea = 0;
                    //index = hierarchy.GetData(new int[] { index, 0 })[0]
                    for (int index = 0; index < numberOfObjects; index++)
                    {
                        IInputArray contour = contours.GetInputArray().GetMat(index);
                        MCvMoments  moment  = CvInvoke.Moments(contour);
                        float       area    = (float)moment.M00;

                        if (area > minObjectArea && area < maxObjectArea && area > referenceArea)
                        {
                            ratPosition = new CorrectPosition(
                                x: (float)moment.M10 / area,
                                y: (float)moment.M01 / area);
                            referenceArea = area;
                        }
                    }
                }
            }
            if (drawPositionInfo)
            {
                DrawPositionInfo();
            }

            var positionToSend = new PointF
                                 (
                x: -2 * (ratPosition.Value.X - binaryImage.Size.Width / 2) / binaryImage.Size.Width,
                y: -2 * (ratPosition.Value.Y - binaryImage.Size.Height / 2) / binaryImage.Size.Height
                                 );

            return(positionToSend);
        }
Пример #2
0
        public static Emgu.CV.Image <TColor, TDepth> DrawBoxOutline <TColor, TDepth>(this Emgu.CV.Image <TColor, TDepth> image,
                                                                                     System.Drawing.Rectangle boundingBox,
                                                                                     TColor?outlineColor = null)
            where TColor : struct, Emgu.CV.IColor
            where TDepth : new()
        {
            if (image is null)
            {
                throw new System.ArgumentNullException(nameof(image));
            }

            var outputImage = image.Clone();

            outputImage.Draw(rect: boundingBox,
                             color: outlineColor ?? (TColor)Settings.OutlineColor,
                             thickness: Settings.OutlineThickness);
            return(outputImage);
        }