In the field of image processing, brightness adjustment is one of the most common operations. Traditional methods usually use linear or gamma correction to achieve brightness adjustment, but these methods tend to cause loss of detail or color distortion when dealing with excessively bright or dark areas. This article will introduce how to use the hyperbolic cosine function (cosh) to build a nonlinear brightness adjustment model to adjust the brightness of the image more soft and delicately.
The hyperbolic cosine function is defined as:
Its shape resembles a parabola, changing slowly around the zero point, and growing exponentially away from the zero point. Using this feature, we can design a brightness adjustment function to make the middle brightness segment change smoothly, and the dark and bright areas adjust more obvious, but not too drastically.
Suppose the original pixel brightness is , normalized to the [0,1] interval. The goal is to construct a brightness transformation function :
in:
controls the steepness of the curve, the larger the value, the more obvious the nonlinearity.
Controls the adjusted reference brightness point.
This model guarantees:
when When , .
The brightness changes smoothly near the reference point, and is more adjustable when away from the reference point.
After the adjustment is completed, map the result back to [0,1] and then map back to the pixel value.
The following is a function in PHP to simulate the process of cosh transformation of grayscale image brightness. In the example, imagecreatefromjpeg and imagejpeg functions are used to operate pictures.
<?php
function adjustBrightnessCosh($inputFile, $outputFile, $a = 5, $b = 0.5) {
// Read the image
$img = imagecreatefromjpeg('https://gitbox.net/images/sample.jpg');
if (!$img) {
die('Unable to load image');
}
$width = imagesx($img);
$height = imagesy($img);
// Pre-calculated normalized benchmarkscoshvalue
$denominator = cosh($a * $b);
for ($y = 0; $y < $height; $y++) {
for ($x = 0; $x < $width; $x++) {
$rgb = imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$bVal = $rgb & 0xFF;
// Calculate brightness(灰度value)
$I = ($r + $g + $bVal) / (3 * 255);
// Brightness adjustment formula
$L = cosh($a * ($I - $b)) / $denominator;
// Map back[0,1]
// To ensure that there is no overflow,WillLRescaling
$L = min(max($L, 0), 2); // Prevent out of scope
$L = $L / 2;
// NewRGBvalue
$newVal = intval($L * 255);
$newVal = min(max($newVal, 0), 255);
// Recombining colors,Keep gray
$newColor = imagecolorallocate($img, $newVal, $newVal, $newVal);
imagesetpixel($img, $x, $y, $newColor);
}
}
// Save the results
imagejpeg($img, $outputFile);
imagedestroy($img);
}
// Helper functionscoshaccomplish
function cosh($x) {
return (exp($x) + exp(-$x)) / 2;
}
// Call Example
adjustBrightnessCosh('input.jpg', 'output.jpg', 5, 0.5);
?>
The image path in the code uses https://gitbox.net/images/sample.jpg to facilitate replacement tests.
By adjusting parameters a and b, the effect of brightness adjustment can be controlled.
This method is particularly suitable for scenarios where detailed nonlinear processing of light and dark areas is required.
Of course, in actual applications, it can also be extended to channel-by-channel adjustment of color space.