Browse Source

small improvements

pull/673/head
popow 8 years ago
parent
commit
adb06efe29
  1. 7
      src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationProcessor.cs
  2. 2
      src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor.cs
  3. 4
      src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationOptions.cs
  4. 7
      src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs

7
src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistEqualizationProcessor.cs

@ -26,11 +26,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
/// or 65536 for 16-bit grayscale images.</param>
/// <param name="clipHistogram">Indicating whether to clip the histogram bins at a specific value.</param>
/// <param name="clipLimit">The histogram clip limit. Histogram bins which exceed this limit, will be capped at this value.</param>
/// <param name="gridSize">The grid size of the adaptive histogram equalization.</param>
/// <param name="gridSize">The grid size of the adaptive histogram equalization. Minimum value is 4.</param>
public AdaptiveHistEqualizationProcessor(int luminanceLevels, bool clipHistogram, int clipLimit, int gridSize)
: base(luminanceLevels, clipHistogram, clipLimit)
{
Guard.MustBeGreaterThan(gridSize, 8, nameof(gridSize));
Guard.MustBeGreaterThanOrEqualTo(gridSize, 4, nameof(gridSize));
this.GridSize = gridSize;
}
@ -84,14 +84,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
}
// Calculate the cumulative distribution function, which will map each input pixel in the current grid to a new value.
cdf.Clear();
int cdfMin = this.ClipHistogramEnabled ? this.CalculateCdf(cdf, histogramCopy) : this.CalculateCdf(cdf, histogram);
float numberOfPixelsMinusCdfMin = pixelsInGrid - cdfMin;
// Map the current pixel to the new equalized value
int luminance = this.GetLuminance(source[x, y], this.LuminanceLevels);
float luminanceEqualized = cdf[luminance] / numberOfPixelsMinusCdfMin;
targetPixels[x, y].PackFromVector4(new Vector4((float)luminanceEqualized));
targetPixels[x, y].PackFromVector4(new Vector4(luminanceEqualized));
// Remove top most row from the histogram, mirroring rows which exceeds the borders.
Span<TPixel> rowSpan = this.GetPixelRow(source, x - halfGridSize, y - halfGridSize, this.GridSize);

2
src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor.cs

@ -36,10 +36,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
int numberOfPixels = source.Width * source.Height;
Span<TPixel> pixels = source.GetPixelSpan();
// Build the histogram of the grayscale levels.
using (IBuffer<int> histogramBuffer = memoryAllocator.AllocateClean<int>(this.LuminanceLevels))
using (IBuffer<int> cdfBuffer = memoryAllocator.AllocateClean<int>(this.LuminanceLevels))
{
// Build the histogram of the grayscale levels.
Span<int> histogram = histogramBuffer.GetSpan();
for (int i = 0; i < pixels.Length; i++)
{

4
src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationOptions.cs

@ -26,9 +26,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
/// <summary>
/// Gets or sets the histogram clip limit. Histogram bins which exceed this limit, will be capped at this value.
/// Defaults to 80.
/// Defaults to 60.
/// </summary>
public int ClipLimit { get; set; } = 80;
public int ClipLimit { get; set; } = 60;
/// <summary>
/// Gets or sets the size of the grid for the adaptive histogram equalization. Defaults to 32.

7
src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs

@ -101,9 +101,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization
}
int addToEachBin = (int)Math.Floor(sumOverClip / (double)this.LuminanceLevels);
for (int i = 0; i < histogram.Length; i++)
if (addToEachBin > 0)
{
histogram[i] += addToEachBin;
for (int i = 0; i < histogram.Length; i++)
{
histogram[i] += addToEachBin;
}
}
}

Loading…
Cancel
Save