Add contrast limiting adaptive histogram equalization (CLAHE) operator (#2726)

This commit is contained in:
Brad Parham
2021-05-23 18:36:04 +02:00
committed by GitHub
parent b69a54fc75
commit 4b6b6189bf
19 changed files with 245 additions and 6 deletions

View File

@@ -216,6 +216,9 @@ const Sharp = function (input, options) {
gammaOut: 0,
greyscale: false,
normalise: false,
claheWidth: 0,
claheHeight: 0,
claheMaxSlope: 3,
brightness: 1,
saturation: 1,
hue: 0,

View File

@@ -350,6 +350,46 @@ function normalize (normalize) {
return this.normalise(normalize);
}
/**
* Perform contrast limiting adaptive histogram equalization (CLAHE)
*
* This will, in general, enhance the clarity of the image by bringing out
* darker details. Please read more about CLAHE here:
* https://en.wikipedia.org/wiki/Adaptive_histogram_equalization#Contrast_Limited_AHE
*
* @param {Object} options
* @param {number} options.width - integer width of the region in pixels.
* @param {number} options.height - integer height of the region in pixels.
* @param {number} [options.maxSlope=3] - maximum value for the slope of the
* cumulative histogram. A value of 0 disables contrast limiting. Valid values
* are integers in the range 0-100 (inclusive)
* @returns {Sharp}
* @throws {Error} Invalid parameters
*/
function clahe (options) {
if (!is.plainObject(options)) {
throw is.invalidParameterError('options', 'plain object', options);
}
if (!('width' in options) || !is.integer(options.width) || options.width <= 0) {
throw is.invalidParameterError('width', 'integer above zero', options.width);
} else {
this.options.claheWidth = options.width;
}
if (!('height' in options) || !is.integer(options.height) || options.height <= 0) {
throw is.invalidParameterError('height', 'integer above zero', options.height);
} else {
this.options.claheHeight = options.height;
}
if (!is.defined(options.maxSlope)) {
this.options.claheMaxSlope = 3;
} else if (!is.integer(options.maxSlope) || options.maxSlope < 0 || options.maxSlope > 100) {
throw is.invalidParameterError('maxSlope', 'integer 0-100', options.maxSlope);
} else {
this.options.claheMaxSlope = options.maxSlope;
}
return this;
}
/**
* Convolve the image with the specified kernel.
*
@@ -368,7 +408,7 @@ function normalize (normalize) {
*
* @param {Object} kernel
* @param {number} kernel.width - width of the kernel in pixels.
* @param {number} kernel.height - width of the kernel in pixels.
* @param {number} kernel.height - height of the kernel in pixels.
* @param {Array<number>} kernel.kernel - Array of length `width*height` containing the kernel values.
* @param {number} [kernel.scale=sum] - the scale of the kernel in pixels.
* @param {number} [kernel.offset=0] - the offset of the kernel in pixels.
@@ -594,6 +634,7 @@ module.exports = function (Sharp) {
negate,
normalise,
normalize,
clahe,
convolve,
threshold,
boolean,