/* "For image enhancement purposes, it is often useful to apply a remapping g(x) that makes the histogram “flat”, i.e., remaps the grey values so that each value occurs an equal number of times in the image. This is called histogram equalization. The idea behind it is that the available image contrast is used optimally." - Digital and medical Imgae Processing by Twan maintz, update 11/2005, page 59 */ /// <summary> /// Apply the histogram equalization /// </summary> /// <param name="image"></param> public override void Apply(Image image) { //h(v) = default pixel distribution int[] h = new int[Image.TotalGrayValues]; for (int x = 0; x < image.Size.Width; x++) { for (int y = 0; y < image.Size.Height; y++) { int pixelColor = image.GetPixelColor(x, y); h[pixelColor] = h[pixelColor] + 1; //Add one to this bin } } //c(v) = added pixel distribution int[] c = new int[Image.TotalGrayValues]; int counter = 0; for (int i = 0; i < Image.TotalGrayValues; i++) { //We are checking the amount of pixels of the current gray value, and add those to the total counter. counter += h[i]; //We set the new value to the c(v) list c[i] = counter; } //We calculate the desired pixesl per bin: float totalPixels = image.Size.Width * image.Size.Height; float idealPixelsPerBin = totalPixels / ((float)Image.TotalGrayValues); //g(v) = the new mapping for the image's pixels. So this array holds the new positions. int[] g = new int[Image.TotalGrayValues]; for (int i = 0; i < Image.TotalGrayValues; i++) { int result = (int)(c[i] / idealPixelsPerBin + 0.5f) - 1; //these two magic numbers are part of the formula g[i] = Math.Max(0, result); //Make all negative numbers zero } //Remap image: int[,] resultAfterRemap = new int[image.Size.Width, image.Size.Height]; for (int x = 0; x < image.Size.Width; x++) { for (int y = 0; y < image.Size.Height; y++) { int originalPixel = image.GetPixelColor(x, y); //Get pixel at coordinate int remapPixel = g[originalPixel]; //Find in remap list, which pixel this value should get resultAfterRemap[x,y] = remapPixel; //Place this new value pixel in the temporary list } } image.SetPixels(resultAfterRemap);//Overwrite image with new values }
private int CalculateEmptyRowsFromRight(Image image) { //CalculateEmptyRowsFromRight: for (int x = image.Size.Width; x > 0; x--) { for (int y = 0; y < image.Size.Height; y++) { if (image.GetPixelColor(x - 1 /* -1 to get right index */, y) != Image.White) { return image.Size.Width - x; } } } return 0; }
private int CalculateEmptyRowsFromLeft(Image image) { //CalculateEmptyRowsFromLeft: for (int x = 0; x < image.Size.Width; x++) { for (int y = 0; y < image.Size.Height; y++) { if (image.GetPixelColor(x, y) != Image.White) { return x; } } } return image.Size.Width; }
private int CalculateEmptyRowsFromBottom(Image image) { //CalculateEmptyRowsFromBottom: for (int y = image.Size.Height; y > 0; y--) { for (int x = 0; x < image.Size.Width; x++) { if (image.GetPixelColor(x, y - 1 /* -1 to get right index */) != Image.White) { return image.Size.Height - y; } } } return 0; }
public static Image Apply(Image img1, Image img2) { //if (img1.Size.Width != img2.Size.Width || img1.Size.Height != img2.Size.Height) throw new Exception("Images dont match in size.."); //int[,] currentPixelsImg1 = img1.GetPixels(); //int[,] currentPixelsImg2 = img2.GetPixels(); //int[,] newPixels = new int[img1.Size.Width, img1.Size.Height]; for (int x = 0; x < img1.Size.Width; x++) { for (int y = 0; y < img1.Size.Height; y++) { if (img1.GetPixelColor(x, y) == img2.GetPixelColor(x, y)) { img1.SetPixelColor(x, y, 0); } else { img1.SetPixelColor(x, y, 255); } } } return img1; }
/// <summary> /// Trim image: /// Use the information while trimming to determine the x and y values on original canvas /// </summary> /// <param name="image"></param> private void Trim(Image image) { int amountOfEmptyRowsTop = Math.Max(CalculateEmptyRowsFromTop(image) - 1, 0); //Removing one, so we always keep 1 empty space around the images. int amountOfEmptyRowsBot = Math.Max(CalculateEmptyRowsFromBottom(image) - 1, 0); int amountOfEmptyRowsLeft = Math.Max(CalculateEmptyRowsFromLeft(image) - 1, 0); int amountOfEmptyRowsRight = Math.Max(CalculateEmptyRowsFromRight(image) - 1, 0); //Create new image: int newImageHeight = image.Size.Height - amountOfEmptyRowsTop - amountOfEmptyRowsBot; int newImageWidth = image.Size.Width - amountOfEmptyRowsLeft - amountOfEmptyRowsRight; int[,] newImage = new int[newImageWidth, newImageHeight]; for (int y = 0; y < newImageHeight; y++) { for (int x = 0; x < newImageWidth; x++) { newImage[x, y] = image.GetPixelColor(x + amountOfEmptyRowsLeft, y + amountOfEmptyRowsTop); } } Image image2 = new Image(newImage, new System.Drawing.Size(newImageWidth, newImageHeight)); //Set local variables: this.image = image2; this.xOnOriginalImage = amountOfEmptyRowsLeft; this.yOnOriginalImage = amountOfEmptyRowsTop; }