/// <summary> /// Recomputes image histogram and draws the result in the given raster image. /// </summary> /// <param name="input">Input image.</param> /// <param name="param">Textual parameter.</param> public static void ComputeHistogram(Bitmap input, ImageHistogramHsvMode mode, int hue = OptimalHue) { // 1. Histogram recomputation. double[] occ = new double[hue]; int width = input.Width; int height = input.Height; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Color col = input.GetPixel(x, y); double H, S, V; Arith.ColorToHSV(col, out H, out S, out V); int h = (int)Math.Round(H * (hue - 1) / 360.0); if (mode == ImageHistogramHsvMode.Distribution) { occ[h]++; } else if (mode == ImageHistogramHsvMode.Vals) { occ[h] += V; } else if (mode == ImageHistogramHsvMode.Power) { occ[h] += V * S; } //histArray[mode == 0 ? col.R : (mode == 1 ? col.G : (mode == 2 ? col.B : Y))]++; } } double Sum = occ.Sum(); histArray = new double[hue]; for (int i = 0; i < hue; i++) { histArray[i] = occ[i] / Sum; } }
public static void ComputeHistogram(Bitmap input, string param) { param = param.Trim(); bool ColorPalete = (param[0] == ColorPaleteCommnad); //ColorPalete bool Histogram = (param[0] == HistogramCommnad); //Histogram bool Values = (param[0] == ValuesCommnad); //Values bool Powers = (param[0] == PowersCommnad); //Powers bool total = ColorPalete || Histogram || Values || Powers; bool onlyOne = BoolMath.OnlyOne(ColorPalete, Histogram, Values, Powers); if (!total) { MessageBox.Show("Missing starting command"); mode = ImageHistogramHsvMode.None; return; } if (!onlyOne) { MessageBox.Show("To many commands"); mode = ImageHistogramHsvMode.None; return; } if (ColorPalete) { mode = ImageHistogramHsvMode.Standart; param = param.Substring(param.IndexOf(ColorPaleteCommnad) + 1); Dictionary <string, string> rec = Util.ParseKeyValueList(param, ' '); int hue = ImageHistogramHsvExist.OptimalHue; int saturation = ImageHistogramHsvExist.OptimalSaturation; Util.TryParse(rec, "Hue", ref hue); RangeMath.InRangeOrDefault(ref hue, ImageHistogramHsvExist.MinHue, ImageHistogramHsvExist.MaxHue, ImageHistogramHsvExist.OptimalHue); Util.TryParse(rec, "Sat", ref saturation); RangeMath.InRangeOrDefault(ref saturation, ImageHistogramHsvExist.MinSaturation, ImageHistogramHsvExist.MaxSaturation, ImageHistogramHsvExist.OptimalSaturation); if (rec.ContainsKey("Colors") && rec["Colors"] == "Dynamic") { ImageHistogramHsvExist.DynamicColors = true; } else if (rec.ContainsKey("Colors") && rec["Colors"] == "Discrete") { ImageHistogramHsvExist.DynamicColors = false; } else { ImageHistogramHsvExist.DynamicColors = true; } ImageHistogramHsvExist.ComputeHistogram(input, hue, saturation + 1); } else if (Histogram || Powers || Values) { if (Histogram) { mode = ImageHistogramHsvMode.Distribution; param = param.Substring(param.IndexOf(HistogramCommnad) + 1); } if (Values) { mode = ImageHistogramHsvMode.Vals; param = param.Substring(param.IndexOf(ValuesCommnad) + 1); } if (Powers) { mode = ImageHistogramHsvMode.Power; param = param.Substring(param.IndexOf(PowersCommnad) + 1); } Dictionary <string, string> rec = Util.ParseKeyValueList(param, ' '); int hue = ImageHistogramHsvOcc.OptimalHue; float zoom = ImageHistogramHsvOcc.OptimalZoom; Util.TryParse(rec, "Hue", ref hue); RangeMath.InRangeOrDefault(ref hue, ImageHistogramHsvOcc.MinHue, ImageHistogramHsvOcc.MaxHue, ImageHistogramHsvOcc.OptimalHue); if (!Util.TryParse(rec, "Zoom", ref zoom) && rec.ContainsKey("Zoom") && (rec["Zoom"] == "A" || rec["Zoom"] == "Auto")) { ImageHistogramHsvOcc.AutoZoom = true; } else { ImageHistogramHsvOcc.AutoZoom = false; } ImageHistogramHsvOcc.Zoom = zoom; ImageHistogramHsvOcc.ComputeHistogram(input, mode, hue + 1); } }