private void button1_Click(object sender, EventArgs e) { Histogram histogram = img.BuildHistogram(); //low bins to 0 out int low_bins = histogram.GetNumBinsWithItems(Convert.ToInt32(img.GetHeight() * img.GetWidth() * .10)); AdvColor low_color = histogram.GetBinColor(low_bins); int high_bins = histogram.GetNumBinsWithItems(Convert.ToInt32(img.GetHeight() * img.GetWidth() * .90)); AdvColor high_color = histogram.GetBinColor(high_bins); AdvColor[,] pixels = img.HistogramStretch(low_color, high_color, histogram, ColorPlane.eGreen); SortedDictionary <AdvColor, int> bins = histogram.GetHistogram(); foreach (KeyValuePair <AdvColor, int> bin in bins) { chart1.Series["Series1"].Points.AddXY(bin.Key.GetColor(ColorPlane.eGreen), bin.Value); } RGBImage img2 = new RGBImage(pixels); img.SaveToImage(pixels, "histo_stretch.png"); Histogram histogram2 = img2.BuildHistogram(); SortedDictionary <AdvColor, int> bins2 = histogram2.GetHistogram(); foreach (KeyValuePair <AdvColor, int> bin in bins2) { chart2.Series["Series1"].Points.AddXY(bin.Key.GetColor(ColorPlane.eGreen), bin.Value); } }
public static YCbCrImage RgbToYCbCr(RGBImage rgbImage) { Stopwatch stopwatch = Stopwatch.StartNew(); ColorChannel luminance = new ColorChannel(rgbImage.GetWidth(), rgbImage.GetHeight()); ColorChannel cbChannel = new ColorChannel(rgbImage.GetWidth(), rgbImage.GetHeight()); ColorChannel crChannel = new ColorChannel(rgbImage.GetWidth(), rgbImage.GetHeight()); int height = rgbImage.GetHeight(); if (height < 192) { for (int y = 0; y < rgbImage.GetHeight(); y++) { for (int x = 0; x < rgbImage.GetWidth(); x++) { YCbCr converted = ConvertRgbtoYCbCr(rgbImage.GetRGBAt(x, y)); luminance.SetPixel(x, y, converted.GetLuminanceChannel()); cbChannel.SetPixel(x, y, converted.GetCbChannel()); crChannel.SetPixel(x, y, converted.GetCrChannel()); } } } else { int threadCount = 16; if (height < 500) { threadCount = 4; } else if (height < 1000) { threadCount = 8; } CountdownEvent e = new CountdownEvent(1); for (int i = 0; i < height; i += height / threadCount) { e.AddCount(); ThreadPool.QueueUserWorkItem( state => { object[] inp = state as object[]; //Console.WriteLine($"Thread with row {(int) inp[0]} to row {(int) inp[1]} started..."); int upper = (int)inp[1]; for (int row = (int)inp[0]; row < upper; row++) { for (int x = 0; x < rgbImage.GetWidth(); x++) { YCbCr converted = ConvertRgbtoYCbCr(rgbImage.GetRGBAt(x, row)); luminance.SetPixel(x, row, converted.GetLuminanceChannel()); cbChannel.SetPixel(x, row, converted.GetCbChannel()); crChannel.SetPixel(x, row, converted.GetCrChannel()); } } e.Signal(); }, new object[] { i, i + (height / threadCount) }); } e.Signal(); e.Wait(); } Console.WriteLine("Finished RGB to YCbCr conversion in " + stopwatch.ElapsedMilliseconds / 1000d + " seconds"); stopwatch.Stop(); return(new YCbCrImage(luminance, cbChannel, crChannel, rgbImage.GetOriginalWidth(), rgbImage.GetOriginalHeight())); }