Example #1
0
        public static void DrawWaitingCircle(Graphics g, Rectangle rect, ImageAlignmentType imageAlignment, Color foreColor, Pen borderPen, int value)
        {
            int   baseSize = Math.Min(Math.Min(rect.Width, rect.Height), 28);
            float size     = baseSize / 5.0F;
            float halfSize = size / 2.0F;
            float radius   = (baseSize / 2.0F) - halfSize;
            float centerX;
            float centerY = rect.Top + (float)rect.Height / 2;

            value %= 8;

            if (imageAlignment == ImageAlignmentType.Center)
            {
                centerX = rect.Left + (float)rect.Width / 2;
            }
            else
            {
                centerX = rect.Top + radius + halfSize;
            }

            SmoothingMode oldSmoothingMode = g.SmoothingMode;

            g.SmoothingMode = SmoothingMode.AntiAlias;

            for (int i = 7; i >= 0; i--)
            {
                int   iAdjusted = ((16 - value - i) % 8);
                int   alpha     = (int)(255.0F * ((i + 1) / 8.0F));
                Color drawColor = Color.FromArgb(alpha, foreColor);
                using (SolidBrush brush = new SolidBrush(drawColor))
                {
                    g.FillEllipse(
                        brush,
                        centerX - (float)Math.Sin(iAdjusted * Math.PI / 4) * radius - halfSize,
                        centerY - (float)Math.Cos(iAdjusted * Math.PI / 4) * radius - halfSize,
                        size,
                        size);
                }

                if (borderPen != null)
                {
                    g.DrawEllipse(
                        borderPen,
                        centerX - (float)Math.Sin(iAdjusted * Math.PI / 4) * radius - halfSize,
                        centerY - (float)Math.Cos(iAdjusted * Math.PI / 4) * radius - halfSize,
                        size,
                        size);
                }

                halfSize *= 0.85f;
                size     *= 0.85f;
            }

            g.SmoothingMode = oldSmoothingMode;
        }
Example #2
0
        public Bitmap GetAlignedImage(Bitmap imageToAlign, Bitmap referenceImage, ImageAlignmentType imageAlignmentType)
        {
            if (imageAlignmentType == ImageAlignmentType.CROP || imageAlignmentType == ImageAlignmentType.MAP)
            {
                Tuple <int, int> offsets = ImageFeatureDetector.GetXYOffsets(imageToAlign, referenceImage);

                if (imageAlignmentType == ImageAlignmentType.CROP)
                {
                    return(GetCroppedImage(imageToAlign, offsets.Item1, offsets.Item2, referenceImage.Width, referenceImage.Height));
                }

                Bitmap bmp = new Bitmap(referenceImage.Width, referenceImage.Height);
                using (Graphics g = Graphics.FromImage(bmp))
                    using (Brush brush = new SolidBrush(Color.Black))
                    {
                        g.FillRectangle(brush, 0, 0, bmp.Width, bmp.Height);
                        g.DrawImage(imageToAlign, -offsets.Item1, -offsets.Item2);
                    }

                return(bmp);
            }

            using (Image <Bgr, byte> alignImg = new Image <Bgr, byte>(imageToAlign))
                using (Image <Bgr, byte> refImg = new Image <Bgr, byte>(referenceImage))
                    using (Mat alignMat = alignImg.Mat)
                        using (Mat refMat = refImg.Mat)
                        {
                            using (VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch())
                            {
                                MatchingTechnique matchingTechnique;
                                if (imageAlignmentType == ImageAlignmentType.FASTWARP)
                                {
                                    matchingTechnique = MatchingTechnique.FAST;
                                }
                                else if (imageAlignmentType == ImageAlignmentType.FULLWARP)
                                {
                                    matchingTechnique = MatchingTechnique.ORB;
                                }
                                else
                                {
                                    throw new NotImplementedException();
                                }

                                ImageFeatureDetector.FindMatches(alignMat, refMat, out VectorOfKeyPoint modelKeyPoints, out VectorOfKeyPoint observedKeyPoints, matches, out Mat mask, out Mat homography, matchingTechnique, 1.0f);

                                try
                                {
                                    using (Mat result = new Mat())
                                    {
                                        Features2DToolbox.DrawMatches(alignMat, modelKeyPoints, refMat, observedKeyPoints, matches, result, new MCvScalar(255, 0, 0), new MCvScalar(0, 0, 255), mask);
                                        result.Save(@"D:\Downloads\Draw.jpg");
                                    }

                                    using (Mat warped = new Mat())
                                    {
                                        if (homography == null)
                                        {
                                            throw new Exception("Could not determine homography between images.");
                                        }

                                        CvInvoke.WarpPerspective(alignMat, warped, homography, refMat.Size);

                                        return(warped.ToBitmap());
                                    }
                                }
                                catch (Exception)
                                {
                                    throw;
                                }
                                finally
                                {
                                    mask.Dispose();
                                    homography.Dispose();
                                }
                            }
                        }
        }
Example #3
0
        public static void DrawWaitingCircleWithGradient(Graphics g, Rectangle rect, ImageAlignmentType imageAlignment, Color foreColor, Pen borderPen, int value)
        {
            int   baseSize    = Math.Min(Math.Min(rect.Width, rect.Height), 28);
            float innerRadius = baseSize * 2 / 3 + (Math.Max(0, baseSize - 20) * 0.5f);
            float size        = (baseSize - innerRadius) / 2;

            if (Math.Min(rect.Width, rect.Height) <= 28 && innerRadius > 20)
            {
                innerRadius -= 2;
                size--;
            }

            float halfRadius    = innerRadius / 2;
            int   numOfLines    = 22 + (int)(Math.Max(0, baseSize - 14) * 1.4f); // more lines for bigger circle
            int   lineThickness = 2 + Math.Max(0, baseSize - 14) / 6;            // and thicker lines for bigger circles

            value %= numOfLines;

            float centerX;
            float centerY = rect.Top + (float)rect.Height / 2;

            if (imageAlignment == ImageAlignmentType.Center)
            {
                centerX = rect.Left + (float)rect.Width / 2;
            }
            else
            {
                centerX = rect.Left + baseSize / 2;
            }

            SmoothingMode oldSmoothingMode = g.SmoothingMode;

            g.SmoothingMode = SmoothingMode.AntiAlias;

            for (int i = numOfLines - 1; i >= 0; i--)
            {
                int adjusted = (i - value + numOfLines) % numOfLines;
                int alpha    = (int)(255.0F * ((float)(adjusted + 1) / numOfLines));

                Color drawColor = Color.FromArgb(alpha, foreColor);

                double angle = (Math.PI * 2 * i) / numOfLines;

                // because of StartCap and EndCap, we have to subtract 1px
                PointF startPoint = new PointF(
                    centerX + (float)((halfRadius + 1) * Math.Cos(angle)),
                    centerY + (float)((halfRadius + 1) * Math.Sin(angle)));

                PointF endPoint = new PointF(
                    centerX + (float)((halfRadius + size - 1) * Math.Cos(angle)),
                    centerY + (float)((halfRadius + size - 1) * Math.Sin(angle)));

                using (Pen pen = new Pen(drawColor, lineThickness))
                {
                    pen.StartCap = LineCap.Round;
                    pen.EndCap   = LineCap.Round;

                    g.DrawLine(pen, startPoint, endPoint);
                }
            }

            g.SmoothingMode = oldSmoothingMode;
        }