/// <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); }
/// <summary> /// Calculates the gradient for each pixel in this image then returns a bitmap /// in which the color corresponds to the gradient. /// </summary> /// <returns></returns> public virtual void GenerateGradientBitmap(ProgressCallback callback) { Reset(); directBitmapReader = new DirectBitmapReader(grayBitmap.Bitmap); bitmap = new Bitmap(grayBitmap.Bitmap.Width, grayBitmap.Bitmap.Height); using (DirectBitmapWriter dbw = new DirectBitmapWriter(bitmap)) { for (int i = 1; i < Bitmap.Height - 1; i++) { if (callback != null) { callback(i * 100 / (Bitmap.Height - 2)); } for (int j = 1; j < Bitmap.Width - 1; j++) { byte grad = GetPixelGradient(j, i); dbw.SetGrayPixel(j, i, grad); if (grad > maximumGradientValue) { maximumGradientValue = grad; } } } } }
public virtual void Reset() { maximumGradientValue = -1; if (bitmap != null) { bitmap.Dispose(); bitmap = null; } directBitmapReader = null; }
/// <summary> /// Calculates the gradient for each pixel in this image then returns a bitmap /// in which the color corresponds to the gradient. /// </summary> /// <returns></returns> public virtual void GenerateGradientBitmap(ProgressCallback callback) { Reset(); directBitmapReader = new DirectBitmapReader(grayBitmap.Bitmap); bitmap = new Bitmap(grayBitmap.Bitmap.Width, grayBitmap.Bitmap.Height); using (DirectBitmapWriter dbw = new DirectBitmapWriter(bitmap)) { for (int i = 1; i < Bitmap.Height - 1; i++) { if (callback != null) callback(i * 100 / (Bitmap.Height - 2)); for (int j = 1; j < Bitmap.Width - 1; j++) { byte grad = GetPixelGradient(j, i); dbw.SetGrayPixel(j, i, grad); if (grad > maximumGradientValue) maximumGradientValue = grad; } } } }
/// <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; }