/// <summary> /// Build a bitmap from an error historgram /// </summary> /// <param name="data">the error histogram</param> /// <param name="height">the heigh of the bitmap to be created</param> /// <returns></returns> internal static Bitmap getBmpFromErrHist(float[] data, int height) { Bitmap bmp = new Bitmap(1, 1); if (data != null) { bmp = new Bitmap(data.Length, height); Graphics grp = Graphics.FromImage(bmp); grp.FillRectangle(Brushes.Black, 0, 0, bmp.Width, bmp.Height); data[0] = 0; // reset 0 as it does not count for the error float [] minmax = IPUtil.GetBounds(data); Pen dpen = new Pen(Color.FromArgb(200, 100, 200, 0)); for (int j = 0; j < data.Length; j++) { int nbc = (int)(height * data[j] / minmax[1]); if (nbc == 0 && data[j] != 0) { nbc = 1; } grp.DrawLine(dpen, j, height, j, height - nbc); } // draw % max of pixels in error grp.DrawString((100 * minmax[1]).ToString(), new Font("arial", 8.0f), new SolidBrush(Color.FromArgb(200, 255, 255, 0)), 10, 10); grp.Dispose(); } return(bmp); }
/// <summary> /// Buid an Histogram for a bitmap /// </summary> /// <param name="bmp">The bitmap to be analyzed</param> /// <returns>an array of float containing the frequency found for each luminance</returns> internal static float[] getFHistogram(Bitmap bmp) { float[] hist = new float[256]; if (bmp != null && bmp.Width * bmp.Height != 0) { for (int i = 0; i < bmp.Width; i++) { for (int j = 0; j < bmp.Height; j++) { int lum = IPUtil.RGB2Yint(bmp.GetPixel(i, j)); hist[lum] += 1.0f; } } float nbp = bmp.Width * bmp.Height; for (int j = 0; j < hist.Length; j++) { hist[j] /= nbp; } } return(hist); }
/// <summary> /// Retrieve the histogram of 2 Bitmap /// </summary> /// <param name="bmp1">the first bitmap</param> /// <param name="bmp2">the second bitmap</param> /// <returns>the bihistogram of the bitmap</returns> internal static float[,] getFBiHistogram(Bitmap bmp1, Bitmap bmp2) { float[,] hist = new float[256, 256]; if (bmp1 != null && bmp1.Width * bmp1.Height != 0) { if (bmp2 != null && bmp2.Width * bmp2.Height != 0) { if (bmp1.Width == bmp2.Width && bmp1.Height == bmp2.Height) { int wi = bmp1.Width; int he = bmp1.Height; for (int i = 0; i < wi; i++) { for (int j = 0; j < he; j++) { int lum1 = IPUtil.RGB2Yint(bmp1.GetPixel(i, j)); int lum2 = IPUtil.RGB2Yint(bmp2.GetPixel(i, j)); hist[lum1, lum2] += 1.0f; } } float nbp = wi * he; nbp *= nbp; int w = hist.GetUpperBound(0); int h = hist.GetUpperBound(1); for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { hist[i, j] /= nbp; } } } } } return(hist); }
/// <summary> /// Build an histogram based on a multidimentinal array (2 or 3) /// </summary> /// <param name="tval">the multidimentional array containing the data</param> /// <returns>an array of float representing the histogram</returns> internal static float[] getFHistogram(object tval) { float[] hist = new float[256]; float [,] data = tval as float[, ]; if (data != null) { int w = data.GetUpperBound(0); int h = data.GetUpperBound(1); float [] bd = IPUtil.GetBounds(data); Console.WriteLine("miinimaxx " + bd[0] + " " + bd[1]); bd[1] = 2000; bd[1] -= bd[0]; for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { int idx = (int)(255.0 * (data[i, j] - bd[0]) / bd[1]); if (idx > 255) { idx = 255; } if (idx < 0) { idx = 9; } hist[idx] += 1.0f; } } float nbp = w * h; for (int j = 0; j < hist.Length; j++) { hist[j] /= nbp; } } float [,,] ddata = tval as float[, , ]; if (ddata != null) { int w = ddata.GetUpperBound(1); int h = ddata.GetUpperBound(2); float [] bd = IPUtil.GetBounds(ddata); for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { float val = RGBfloat2Yfloat(ddata[0, i, j], ddata[1, i, j], ddata[2, i, j]); int idx = (int)(255.0 * (val - bd[0]) / bd[1]); if (idx > 255) { idx = 255; } if (idx < 0) { idx = 9; } hist[idx] += 1.0f; } } float nbp = w * h; for (int j = 0; j < hist.Length; j++) { hist[j] /= nbp; } } mHist.Add(hist); return(hist); }