
This article introduces a method for automatic color enhancement in images using histogram analysis. It covers histogram normalization, custom gamma correction, and application in various color spaces like HSV and YCrCb. The approach is simple, fast, and suitable for low-resource devices.
- You can access the complete, functional code for this article in the Google Colab notebook.
- There're also more interesting results.
- All source images are taken from google search request
bad white balance photo.
The primary concept of the suggested method revolves around channel-specific normalization of histograms and gamma correction, utilizing parameters derived from the histogram data. This method is grounded in the assumption that an image exhibiting accurate white balance and optimal exposure should display an average color tone that approximates medium grey. Furthermore, it is essential that all channels comprehensively encompass the entire spectrum of potential values, which ranges from 0 to 255 in an 8-bit image.
To begin, we will undertake a manual implementation of this process. The initial step involves the normalization of the histogram:

After ensuring that each channel in our image spans its entire possible range, we advance to the next phase: channel-wise gamma correction. This process involves adjusting the gamma values in a way that aligns the median of each channel's histogram with the 0.5 point. Essentially, this operation aims to center the distribution of pixel values around a middle grey, which is crucial for achieving a balanced exposure and contrast in the overall image. This adjustment is a pivotal part of the process, enhancing the visual quality and accuracy of the image's color representation.

Resulting image:
Resulting image's histogram:
Automatically finding parameters for histogram normalization and gamma correction using histogram analysis is a sophisticated and effective approach. This method involves analyzing the image's histogram, which is a graphical representation of the distribution of pixel intensities, to determine optimal adjustment parameters.
Here's a summary of how the process unfolds:
Histogram Analysis: Initially, we examine the histogram for each color channel, focusing on median value and edge values.
Identifying Parameters for Adjustments:
Application of Automated Adjustments:
Iterative Refinement (where necessary): Some advanced versions of this method may adopt an iterative approach, repeatedly fine-tuning the parameters to secure the most favorable outcomes.
To commence the automated adjustment process, the first crucial step is identifying the edges of the histogram values. This step involves determining the minimum and maximum intensity values that effectively represent the true range of the image data.

Based on the outcomes observed earlier, it becomes evident that employing a zero threshold does not yield accurate results. To address this, a more nuanced approach involves the application of a threshold derived from a percentage of the histogram's maximal value. For instance, setting a threshold at 1% of the maximum value could offer a more precise determination of the histogram's edges. This method allows for a finer distinction of the true range of pixel intensities, especially in scenarios where a zero threshold fails to adequately capture the nuances of the image's contrast and brightness levels. Such a threshold adjustment is instrumental in accurately stretching or normalizing the histogram, ultimately enhancing the overall image quality.

Now we should find a median index in the histogram and offset it to 0.5 point.

Resulting image:
Resulting image's histogram:
We achieved positive outcomes by employing horizontal thresholds on histogram values.
Certain images exhibit areas of extreme darkness (black) and brightness (white), which can disproportionately influence the median and edge values of histograms. To counteract this, we can utilize horizontal thresholds that effectively disregard pixels that are excessively bright or dark.
Below is comparing both of versions (left is the 1st):
By excluding the black and white areas, we prevent the undue shifting of the blue median. Consequently, this second version of the process eliminates certain artifacts, resulting in a smoother and more natural outcome.
Previously, we utilized a gamma correction function that was based on an exponential formula:
$
g(x, a) = x^{\exp(a)}
$
The initial gamma correction function, which operates based on an exponential formula, encounters issues in areas near 0 and 1. To address this, I have identified an alternative function (represented in green):
$
g(x, a)=\frac{x^a+1-(1-x)^\frac{1}{a}}{2}
$
Presented below are the comparative results of applying this new function against the previous version, showcasing the differences between version 2 and version 3:
Following are multiple examples showcasing the application of this approach in various color spaces. These illustrations might spark new ideas or insights into potential uses of this technique.
The approach yields promising outcomes when applied to the (H)SV color space; however, it falls short in rectifying issues related to incorrect white balance.
The results depicted above demonstrate that employing autocorrection in the (H)SV color space can enhance the saturation of an image.
Outlined below is a cascading sequence that integrates RGB, (H)S(V), and again RGB autocorrections. In this process, the RGB correction utilizes the third version, which includes a custom gamma, while the HSV adjustment employs the second version for a softer effect. Additionally, I apply a saturation level of 0.5 to minimize the occurrence of artifacts.
Resulting image:
Resulting image's histogram:
The simplicity of this histogram-based method for automatic color enhancement offers several advantages over more complex alternatives that rely on machine learning (ML) and deep analysis:
Less Computational Complexity: Unlike ML-based methods that often require significant computational resources for training and inference, the histogram approach is computationally lightweight. This makes it faster and more efficient, particularly for applications with limited processing power.
No Training Required: ML models require large datasets for training and may suffer from biases present in these datasets. The histogram method, in contrast, does not require training and is thus free from these concerns. It operates solely based on the statistical properties of the image itself.
Ease of Implementation: Implementing and understanding histogram-based techniques is generally easier than setting up complex ML pipelines. This simplicity can be a significant advantage for developers with limited resources or expertise in ML.
Predictable and Consistent Results: Since this method relies on straightforward mathematical operations, it offers more predictable and consistent outcomes. ML models, especially those involving deep learning, can sometimes produce unpredictable results due to their 'black box' nature.
Broad Applicability: The histogram method can be applied to a wide range of images without needing customization or retraining, unlike ML models which might need retraining or fine-tuning when applied to different types of images.
Transparency and Control: The process and logic behind histogram adjustments are transparent and can be easily modified or controlled. In contrast, ML models, particularly deep learning ones, often lack this level of transparency and control.
Low Resource Requirement: Histogram-based approaches are suitable for devices with limited memory and processing power, such as smartphones and embedded systems, where running complex ML models might not be feasible.
While ML-based methods can be powerful and offer their own set of advantages, particularly in handling highly complex or specific tasks, the simplicity, efficiency, and transparency of the histogram-based method make it a preferred choice for many general-purpose image enhancement tasks.
For practical implementation, you can find a code snippet that executes the procedures discussed in this article here
George Ostrobrod, 2019 (edited 2024)