let us consider following code -which reads image, applies histogram equalization procedure and display both result :

import cv2
import numpy as np
img = cv2.imread('original.png', cv2.IMREAD_GRAYSCALE)
assert img is not None, "file could not be read, check with os.path.exists()"
equ = cv2.equalizeHist(img)
res = np.hstack((img,equ)) #stacking images side-by-side
cv2.imshow("both image",res)

result is : enter image description here

we can see clearly that contrast of the image was increased, but as you know almost every part of the AI , Machine learning or computer vision tasks is evaluated by some criteria, for example in deep learning we have notion of loss function(like a binary_crossentropy, mean_squared_error and etc), so my question is : is there any measurment scale whch will be suitable to the contrast increasment ? so if i would like to ask : by how much or how large changement has been made after histogram equalization, is there any score for this? sure i can calculate difference between two image as they have same dimension and calculate square of difference(therefore squared distance between two image) but is it correct? maybe difference between two histogram(difference between two distribution) will be right action to be done?if so could you tell me please how?


1 Answer 1


There are multiple ways to do so. In the following, I will list some approaches (there will be a suggestion at the end if you cannot decide for an approach):

Types of differences

There are 3 main types of differences I would like to start with:

  1. Direct difference between the images (e.g. mean squared difference between pixels, as you mentioned)
  2. Difference between the histogram
  3. Difference between contrast measures.

The problem with approach 1 is, that there can be two images with the same difference to the original image, but one has increased, the other one decreased constrast. Just imagine to darken dark pixels and brighten bright pixels in one image (this increases the contrast) and on the other hand brighten dark pixels and darken bright pixels (this decreases contrast). Both will have the same (absolute / square) difference to the original image.

Approach 2 gives you a global view on images. The question is how to measure the difference of histograms (see below.) The problem is, that a different histogram does not necessarily mean that the constrast is different. Image a dark image with gray values between 0 and 127 (on a scale from 0 to 255). If you light up the image, the (now it has values 128 to 255), the contrast does not change, just the brightness.

Approach 3 allows to directly compare contrasts. It has the potential to take into account local distribution of pixels (yet, I will focus in the following on global contrast definitions). The key is how to measure contrast.

Difference between histograms

There are different ways to compare histograms / distributions:

  • One could compute the bin-wise differences,
  • There is the Kullback-Leibler-Divergence
  • Another way is the earth-mover- / Wasserstein-distance. While the former two do not measure well by how much pixels are darkened / brightened, the earth-mover distance does exactly this.

Imagine a binary image with pixels of gray value 0 or 1 (on the scale up to 255). One method to increase contrast lead to an image with values 0 and 5, another method produces values 0 and 255. Of the three mentioned methods, only Wasserstein detects that the second method creates a larger improvement.

Suggestion: Use Wasserstein Distance to compare histograms with respect to contrast.

How to measure contrast of an image.

There exist difference ways to measure the contrast of an image, e.g.

  • The Michelson Contrast $$\frac{I_{max}-I_{min}}{I_{max}+I_{min}}$$ only consideres the darkest and brightest values.
  • The RMS contrast measures the standard deviation of the gray values in the image: $$\sqrt{\frac{1}{NM}\sum_{i=1}^M\sum_{j=1}^N(I_{ij}-\bar{I})^2}$$ with then $I_{ij}$ being a single pixels value and $\bar{I}$ being the average gray value of the image. This takes into account all pixels of the image.

I would start with the RMS Contrast, but it depends a bit what exactly you are interested in.


All discussed approaches are dealing with the global contrast. There are adaptive / local methods that act differently, depending on the surrounding of a pixel. Image an image that has bright and dark areas. A slightly dark pixel might be brightened in dark areas whereas a slightly dark pixel might be darkened in bright areas to increase contrast.

To measure the contrast in such situations, one needs a contrast measure that takes into account the local contrast. RMS contrast over small areas might be a start for that.

  • $\begingroup$ thank you sir or your answer, i just come up to some ideas , still need to transform this idea into professional thinking, one question -how the The RMS contrast measure is calculated? $\endgroup$ Commented Apr 23, 2023 at 11:51
  • 1
    $\begingroup$ I updated the answer to contain the equation for the RMS Contrast $\endgroup$
    – Broele
    Commented Apr 23, 2023 at 13:54
  • $\begingroup$ thank you sir, it is really helpfull for me $\endgroup$ Commented Apr 23, 2023 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.