Ejemplo n.º 1
0
        /// <summary>
        /// Weight function
        /// 121
        /// 242
        /// 121
        /// </summary>
        private static long GetFunction3x3(Bitmap b, int x, int y, Color c0)
        {
            long f = ColorParser.GetColorDiff2(b.GetPixel(x, y), c0) << 2;

            f += ColorParser.GetColorDiff2(b.GetPixel(x - 1, y - 1), c0);
            f += ColorParser.GetColorDiff2(b.GetPixel(x, y - 1), c0) << 1;
            f += ColorParser.GetColorDiff2(b.GetPixel(x + 1, y - 1), c0);
            f += ColorParser.GetColorDiff2(b.GetPixel(x - 1, y), c0) << 1;
            f += ColorParser.GetColorDiff2(b.GetPixel(x + 1, y), c0) << 1;
            f += ColorParser.GetColorDiff2(b.GetPixel(x - 1, y + 1), c0);
            f += ColorParser.GetColorDiff2(b.GetPixel(x, y + 1), c0) << 1;
            f += ColorParser.GetColorDiff2(b.GetPixel(x + 1, y + 1), c0);
            return(f);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Finds the best fit for line, moving the point in a vertical window
        /// </summary>
        /// <param name="Image">Image to process</param>
        /// <param name="initial">x,y coordinate of start point</param>
        /// <param name="c0">Desired colour under the new location</param>
        /// <param name="window">Search window up or down</param>
        /// <returns>new point</returns>
        public static Point FindLinePointVertical(Bitmap Image, Point initial, Color c0, int window)
        {
            int locX = initial.X;

            if (locX < 0 || locX >= Image.Width)
            {
                return(initial);
            }
            int locY = initial.Y;

            if (locY < 0 || locY >= Image.Height)
            {
                return(initial);
            }
            Color cc0   = Image.GetPixel(locX, locY);
            long  fit0  = ColorParser.GetColorDiff2(cc0, c0);
            int   locY0 = initial.Y - window;

            if (locY0 < 0)
            {
                locY0 = 0;
            }
            int locY1 = initial.Y + window;

            if (locY1 >= Image.Height)
            {
                locY1 = Image.Height - 1;
            }

            long fitUp = fit0;
            int  locUp = locY;

            for (int y = locY - 1; y >= locY0; y--)
            {
                cc0 = Image.GetPixel(locX, y);
                long fit = ColorParser.GetColorDiff2(cc0, c0);
                if (fit > fitUp)
                {
                    break;
                }
                fitUp = fit;
                locUp = y;
            }

            long fitDown = fit0;
            int  locDown = locY;

            for (int y = locY + 1; y <= locY1; y++)
            {
                cc0 = Image.GetPixel(locX, y);
                long fit = ColorParser.GetColorDiff2(cc0, c0);
                if (fit > fitDown)
                {
                    break;
                }
                fitDown = fit;
                locDown = y;
            }

            return(new Point(initial.X, (locUp + locDown) / 2));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Finds the best fit in a box 2window x 2window pixels
        /// </summary>
        /// <param name="Image">Image to process</param>
        /// <param name="initial">x,y coordinate of start point</param>
        /// <param name="c0">Desired colour under the new location</param>
        /// <param name="window">Search window size</param>
        /// <returns>new point</returns>
        public static Point FindBoxPoint(Bitmap Image, Point initial, Color c0, int window)
        {
            int locX = initial.X;

            if (locX < 0 || locX >= Image.Width)
            {
                return(initial);
            }
            int locY = initial.Y;

            if (locY < 0 || locY >= Image.Height)
            {
                return(initial);
            }

            int locX0 = initial.X - window;

            if (locX0 < 0)
            {
                locX0 = 0;
            }
            int locX1 = initial.X + window;

            if (locX1 >= Image.Width)
            {
                locX1 = Image.Width - 1;
            }
            int locY0 = initial.Y - window;

            if (locY0 < 0)
            {
                locY0 = 0;
            }
            int locY1 = initial.Y + window;

            if (locY1 >= Image.Height)
            {
                locY1 = Image.Height - 1;
            }

            List <Point> minimumPoints = new List <Point>();

            minimumPoints.Add(new Point(locX, locY));
            //long minimum = GetFunction3x3(Image, locX, locY, c0);
            long minimum = ColorParser.GetColorDiff2(Image.GetPixel(locX, locY), c0);

            for (int x = locX0; x <= locX1; x++)
            {
                for (int y = locY0; y <= locY1; y++)
                {
                    if (x == locX && y == locY)
                    {
                        continue;
                    }
                    //long w = GetFunction3x3(Image, x, y, c0);
                    long w = ColorParser.GetColorDiff2(Image.GetPixel(x, y), c0);
                    if (w > minimum)
                    {
                        continue;
                    }
                    if (w < minimum)
                    {
                        minimumPoints.Clear();
                        minimum = w;
                    }
                    minimumPoints.Add(new Point(x, y));
                }
            }
            float shiftX = 0f;
            float shiftY = 0f;

            foreach (Point p in minimumPoints)
            {
                shiftX += Convert.ToSingle(p.X);
                shiftY += Convert.ToSingle(p.Y);
            }
            shiftX /= minimumPoints.Count;
            shiftY /= minimumPoints.Count;
            return(new Point((int)shiftX, (int)shiftY));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Finds the best fit for line, moving the point in a horizontal window
        /// </summary>
        /// <param name="Image">Image to process</param>
        /// <param name="initial">x,y coordinate of start point</param>
        /// <param name="c0">Desired colour under the new location</param>
        /// <param name="window">Search window left or right</param>
        /// <returns>new point</returns>
        public static Point FindLinePointHorizontal(Bitmap Image, Point initial, Color c0, int window)
        {
            int locX = initial.X;

            if (locX < 0 || locX >= Image.Width)
            {
                return(initial);
            }
            int locY = initial.Y;

            if (locY < 0 || locY >= Image.Height)
            {
                return(initial);
            }
            Color cc0   = Image.GetPixel(locX, locY);
            long  fit0  = ColorParser.GetColorDiff2(cc0, c0);
            int   locX0 = initial.X - window;

            if (locX0 < 0)
            {
                locX0 = 0;
            }
            int locX1 = initial.X + window;

            if (locX1 >= Image.Width)
            {
                locX1 = Image.Width - 1;
            }

            long fitLeft = fit0;
            int  locLeft = locX;

            for (int x = locX - 1; x >= locX0; x--)
            {
                cc0 = Image.GetPixel(x, locY);
                long fit = ColorParser.GetColorDiff2(cc0, c0);
                if (fit > fitLeft)
                {
                    break;
                }
                fitLeft = fit;
                locLeft = x;
            }

            long fitRight = fit0;
            int  locRight = locX;

            for (int x = locX + 1; x <= locX1; x++)
            {
                cc0 = Image.GetPixel(x, locY);
                long fit = ColorParser.GetColorDiff2(cc0, c0);
                if (fit > fitRight)
                {
                    break;
                }
                fitRight = fit;
                locRight = x;
            }

            return(new Point((locLeft + locRight) / 2, initial.Y));
        }