Browse Source

changed the cdf to be the cumulative histogram

pull/644/head
popow 8 years ago
parent
commit
5b1b8b11d3
  1. 34
      src/ImageSharp/Processing/Contrast/HistogramEqualizationProcessor.cs

34
src/ImageSharp/Processing/Contrast/HistogramEqualizationProcessor.cs

@ -35,37 +35,34 @@ namespace SixLabors.ImageSharp.Processing.Contrast
} }
} }
int min = luminanceLevels - 1; // calculate the cumulative distribution function which will be the cumulative histogram
for (int i = 0; i < histogram.Length - 1; i++) int[] cdf = new int[luminanceLevels];
int histSum = 0;
for (int i = 0; i < histogram.Length; i++)
{ {
if (histogram[i] != 0) histSum += histogram[i];
{ cdf[i] = histSum;
min = i;
break;
}
} }
int max = 0; int cdfMin = 0;
for (int i = histogram.Length - 1; i > 0; i--) for (int i = 0; i < histogram.Length; i++)
{ {
if (histogram[i] != 0) if (histogram[i] != 0)
{ {
max = i; cdfMin = cdf[i];
break; break;
} }
} }
// calculate the cumulative distribution function int[] lut = new int[luminanceLevels];
double[] cdf = new double[luminanceLevels];
double sum = 0.0d;
for (int i = 0; i < histogram.Length; i++) for (int i = 0; i < histogram.Length; i++)
{ {
double p = (double)histogram[i] / numberOfPixels; lut[i] = cdf[i] - cdfMin;
sum += p;
cdf[i] = sum;
} }
// apply the cdf to each pixel of the image // apply the cdf to each pixel of the image
double numberOfPixelsMinusCdfMin = (double)(numberOfPixels - cdfMin);
int luminanceLevelsMinusOne = luminanceLevels - 1;
for (int y = 0; y < source.Height; y++) for (int y = 0; y < source.Height; y++)
{ {
Span<TPixel> row = source.GetPixelRowSpan(y); Span<TPixel> row = source.GetPixelRowSpan(y);
@ -74,8 +71,9 @@ namespace SixLabors.ImageSharp.Processing.Contrast
TPixel sourcePixel = row[x]; TPixel sourcePixel = row[x];
sourcePixel.ToRgb24(ref rgb); sourcePixel.ToRgb24(ref rgb);
int luminance = (int)((.2126F * rgb.R) + (.7152F * rgb.G) + (.0722F * rgb.B)); int luminance = (int)((.2126F * rgb.R) + (.7152F * rgb.G) + (.0722F * rgb.B));
byte luminanceEqualized = (byte)(cdf[luminance] * (luminanceLevels - 1)); double luminanceEqualized = (lut[luminance] / numberOfPixelsMinusCdfMin) * luminanceLevelsMinusOne;
row[x].PackFromRgba32(new Rgba32(luminanceEqualized, luminanceEqualized, luminanceEqualized)); luminanceEqualized = Math.Round(luminanceEqualized);
row[x].PackFromRgba32(new Rgba32((byte)luminanceEqualized, (byte)luminanceEqualized, (byte)luminanceEqualized));
} }
} }
} }

Loading…
Cancel
Save