/// <summary> /// Generate a greyscale histogram chart from the bitmap /// </summary> /// <param name="b">Image to use</param> /// <returns>Histogram chart</returns> /*public static Bitmap GenerateGreyscaleHistogram(Bitmap b) * { * ImagerBitmap i = new ImagerBitmap(b.Clone() as Bitmap); * double[] histogram = new double[256]; * string[] lbls = new string[256]; * for (int column = 0; column < i.Bitmap.Width; column++) * { * for (int row = 0; row < i.Bitmap.Height; row++) * { * histogram[i.GetGreyPixel(column, row)]++; * } * } * i.UnlockBitmap(); * for (int j = 0; j < 256; j++) * { * if ((j % 30) == 0) * { * lbls[j] = j.ToString(); * } * else * { * lbls[j] = string.Empty; * } * } * XYChart c = new XYChart(b.Width + 60, b.Height + 60); * c.setPlotArea(40, 40, b.Width - 20, b.Height - 20); * c.addTitle("Greyscale Historgram", "Arial Bold", 10).setBackground( * Chart.metalColor(0x9999ff), -1, 1); * c.addBarLayer(histogram); * c.xAxis().setLabels(lbls); * return c.makeImage() as Bitmap; * }*/ /// <summary> /// Generate a histogram chart from the bitmap /// </summary> /// <param name="b">Image to use</param> /// <returns>Histogram chart</returns> /* public static Bitmap GenerateHistogram(Bitmap b) * { * ImagerBitmap i = new ImagerBitmap(b.Clone() as Bitmap); * double[] histogram = new double[64]; * string[] lbls = new string[64]; * for (int column = 0; column < i.Bitmap.Width; column++) * { * for (int row = 0; row < i.Bitmap.Height; row++) * { * histogram[i.GetRGBHistogramValue(column, row)]++; * } * } * i.UnlockBitmap(); * for (int j = 0; j < 64; j++) * { * if ((j % 10) == 0) * { * lbls[j] = j.ToString(); * } * else * { * lbls[j] = string.Empty; * } * } * XYChart c = new XYChart(b.Width + 60, b.Height + 60); * c.setPlotArea(40, 40, b.Width - 20, b.Height - 20); * c.addTitle("Color Historgram", "Arial Bold", 10).setBackground( * Chart.metalColor(0x9999ff), -1, 1); * c.addBarLayer(histogram); * c.xAxis().setLabels(lbls); * return c.makeImage() as Bitmap; * }*/ /// <summary> /// Generate a Chart of a Scan line /// </summary> /// <param name="b">Image to use</param> /// <param name="row">Row to use</param> /// <returns>Scan Line Chart</returns> /*public static Bitmap GenerateScanLineChart(Bitmap b, int row) * { * ImagerBitmap i = new ImagerBitmap(b.Clone() as Bitmap); * double[] luminance = new double[b.Width]; * string[] lbls = new string[b.Width]; * double max = double.MinValue; * double min = double.MaxValue; * for (int column = 0; column < i.Bitmap.Width; column++) * { * luminance[column] = i.GetGreyPixel(column, row); * if (luminance[column] > max) max = luminance[column]; * if (luminance[column] < min) min = luminance[column]; * if ((column % 40) == 0) * { * lbls[column] = column.ToString(); * } * else * { * lbls[column] = string.Empty; * } * } * i.UnlockBitmap(); * XYChart c = new XYChart(b.Width + 60, b.Height + 60); * c.setPlotArea(40, 40, b.Width - 20, b.Height - 20); * c.addTitle(string.Format("Pixel Luminance for Scan Line {0}, STF {1:0.00}", * row.ToString(), * (max - min) / (max + min)), "Arial Bold", 10).setBackground( * Chart.metalColor(0x9999ff), -1, 1); * c.addLineLayer(luminance); * c.xAxis().setLabels(lbls); * return c.makeImage() as Bitmap; * }*/ /// <summary> /// Generate a Chart of a Vertical Scan line /// </summary> /// <param name="b">Image to use</param> /// <param name="column">Column to use</param> /// <returns>Scan Line Chart</returns> /*public static Bitmap GenerateVerticalScanLineChart(Bitmap b, int col) * { * ImagerBitmap i = new ImagerBitmap(b.Clone() as Bitmap); * double[] luminance = new double[b.Height]; * string[] lbls = new string[b.Height]; * for (int row = 0; row < i.Bitmap.Height; row++) * { * luminance[row] = i.GetGreyPixel(col, row); * if ((row % 40) == 0) * { * lbls[row] = row.ToString(); * } * else * { * lbls[row] = string.Empty; * } * } * i.UnlockBitmap(); * XYChart c = new XYChart(b.Width + 60, b.Height + 60); * c.setPlotArea(40, 40, b.Width - 20, b.Height - 20); * c.addTitle("Pixel Luminance for Vertical Scan Line " + col.ToString(), * "Arial Bold", 10).setBackground( * Chart.metalColor(0x9999ff), -1, 1); * c.addLineLayer(luminance); * c.xAxis().setLabels(lbls); * return c.makeImage() as Bitmap; * }*/ public static void GenerateExcelFile(Bitmap b) { ImagerBitmap i = new ImagerBitmap(b.Clone() as Bitmap); string fileName = "output.txt"; int f = 0; using (StreamWriter sw = new StreamWriter(fileName, false)) { sw.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}", "Index", "Row", "Column", "Red", "Green", "Blue", "Gray"); for (int column = 0; column < i.Bitmap.Width; column++) { for (int row = 0; row < i.Bitmap.Height; row++) { f++; Color c = i.GetPixel(column, row); int g = i.GetGreyPixel(column, row); sw.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}", f, row, column, c.R, c.G, c.B, g); } } } i.UnlockBitmap(); System.Diagnostics.Process.Start(fileName); }
/// <summary> /// Make the image Grey scale /// </summary> /// <param name="b">Image to Process</param> /// <returns>Filtered Image</returns> public static Bitmap GetGreyScaleBitmap(Bitmap b) { ImagerBitmap i = new ImagerBitmap(b.Clone() as Bitmap); for (int column = 0; column < i.Bitmap.Width; column++) { for (int row = 0; row < i.Bitmap.Height; row++) { int val = i.GetGreyPixel(column, row); i.SetPixel(column, row, Color.FromArgb(val, val, val)); } } i.UnlockBitmap(); return(i.Bitmap.Clone() as Bitmap); }
public static Bitmap GetBlackAndWhiteBitmap2(Bitmap b) { ImagerBitmap i = new ImagerBitmap(b.Clone() as Bitmap); double[] histogram = new double[256]; for (int column = 0; column < i.Bitmap.Width; column++) { for (int row = 0; row < i.Bitmap.Height; row++) { histogram[i.GetGreyPixel(column, row)]++; } } double k = b.Width * b.Height; double half = k / 2; int middle = 0; while (k > half) { k = k - histogram[middle]; middle++; } for (int column = 0; column < i.Bitmap.Width; column++) { for (int row = 0; row < i.Bitmap.Height; row++) { int val = (i.GetPixel(column, row).R + i.GetPixel(column, row).G + i.GetPixel(column, row).B) / 3; if (val > middle) { val = 255; } else { val = 0; } i.SetPixel(column, row, Color.FromArgb(val, val, val)); } } i.UnlockBitmap(); return(i.Bitmap.Clone() as Bitmap); }
/// <summary> /// Make the image black and white /// </summary> /// <param name="b">Image to Process</param> /// <returns>Filtered Image</returns> public static Bitmap GetBlackAndWhiteBitmap(Bitmap b) { ImagerBitmap i = new ImagerBitmap(b.Clone() as Bitmap); double[] histogram = new double[256]; for (int column = 0; column < i.Bitmap.Width; column++) { for (int row = 0; row < i.Bitmap.Height; row++) { histogram[i.GetGreyPixel(column, row)]++; } } //find the position of the max value on the left int leftK = 0; for (int k = 0; k < 128; k++) { if (histogram[k] > histogram[leftK]) { leftK = k; } } //find the position of the max value on the right int rightK = 0; for (int k = 128; k < 256; k++) { if (histogram[k] > histogram[rightK]) { rightK = k; } } //find the min value between the 2 local maxes int localMin = rightK; for (int k = leftK; k < rightK; k++) { if (histogram[k] < histogram[localMin]) { localMin = k; } } for (int column = 0; column < i.Bitmap.Width; column++) { for (int row = 0; row < i.Bitmap.Height; row++) { int val = (i.GetPixel(column, row).R + i.GetPixel(column, row).G + i.GetPixel(column, row).B) / 3; if (val > localMin) { val = 255; } else { val = 0; } i.SetPixel(column, row, Color.FromArgb(val, val, val)); } } i.UnlockBitmap(); return(i.Bitmap.Clone() as Bitmap); }