示例#1
0
        /// <summary>
        /// Calculate the gradient of a given pixel.
        /// </summary>
        /// <param name="x">x coordinate of pixel, must be between 1..cols-1</param>
        /// <param name="y">y coordinate of pixel, must be between 1..rows-1</param>
        /// <returns>The gradient of the given pixel, considering x and y directions.</returns>
        public byte GetPixelGradient(int x, int y)
        {
            int  GXxy = 0;           //the y portion
            int  GYxy = 0;           //the x portion
            byte Gxy  = 0;           //final value

            if (directBitmapReader == null)
            {
                directBitmapReader = new DirectBitmapReader(grayBitmap.Bitmap);
            }

            GXxy = ((directBitmapReader.GetGrayPixel(x + 1, y + 1)) - (directBitmapReader.GetGrayPixel(x - 1, y + 1)));
            GXxy = GXxy + (2 * ((directBitmapReader.GetGrayPixel(x + 1, y)) - (directBitmapReader.GetGrayPixel(x - 1, y))));
            GXxy = GXxy + ((directBitmapReader.GetGrayPixel(x + 1, y - 1)) - (directBitmapReader.GetGrayPixel(x - 1, y - 1)));
            GXxy = GXxy / 4;

            GYxy = ((directBitmapReader.GetGrayPixel(x + 1, y + 1)) - (directBitmapReader.GetGrayPixel(x + 1, y - 1)));
            GYxy = GYxy + (2 * ((directBitmapReader.GetGrayPixel(x, y + 1)) - (directBitmapReader.GetGrayPixel(x, y - 1))));
            GYxy = GYxy + ((directBitmapReader.GetGrayPixel(x - 1, y + 1)) - (directBitmapReader.GetGrayPixel(x - 1, y - 1)));
            GYxy = GYxy / 4;

            /* This is how we did this in CS 450, it's faster than doing
             * a square root, but not quite as accurate, hence the clamping.
             * It does bring out more edges, but I don't know if that is
             * good or not - Ryan
             * Gxy = (Math.Abs(GXxy) + Math.Abs(GYxy));
             * Gxy = Math.Max(0, Gxy);
             * Gxy = Math.Min(imgMaxGray, Gxy);
             */

            Gxy = (byte)Math.Sqrt((double)(GXxy * GXxy) + (double)(GYxy * GYxy));
            return(Gxy);
        }