Skip main navigation

Histogram equalisation in Fiji and OpenCV

A description of image contrast equalisation

Contrast Enhancement

Often, image data can be improved by increasing the spread of pixel intensities across the possible range of attainable values. This is known as contrast enhancement

A simple method of contrast enhancement is linear contrast enhancement, in which every pixel is multiplied by a common factor, with possibly a constant term added (or subtracted). If, for example you had an image with pixel intensities ranging from 5 to 132, you could double the value of every pixel, then subtract 10. This would leave an image ranging from 0 to 254 in intensity, using a wider range, in fact nearly the full range of intensity values.

Histogram equalisation

A more sophisticated method of contrast enhancement commonly used in image analysis is known as histogram enhancement, which we describe, and show how to perform using Fiji, in this article.

Linear contrast enhancement only alters the overall spread of pixel values, it doesn’t affect the relative values of pixels to one another. If there was a dominant peak of intensities in the original image that peak will still be there in the contrast adjusted image.

Histogram equalisation, on the other hand, actively seeks to spread out pixel values, not just in terms of the overall range, but also relative to one another. In crude terms, it will try and spread-out pixel intensities over a broader range of values than in the original image. This can have the effect of improving the definition and visibility of objects in the image. In terms of the image histogram it will try and squash and spread out the peaks of high pixel counts over a wider range of values, while narrowing or collecting together regions of the range that have low pixel counts.

A diagram of an image histogram before and after equalisation. The pre-equalisation histogram has a central peak (shown red) with flat regions with lower counts either side (shown black). In the post-equalisation histogram the black regions are narrower but higher, and equal in height to the red region, which is flat, but wider than before.

The ‘T’ in the image above indicates that we need to find a transformation from one image to the equalised version. So how do we do that?
As mentioned in the accompanying video, if each value in an image histogram is normalised (divided) by the total number of pixels, it becomes a probability density function showing the likelihood of a given pixel taking a particular value.
A property of probability density functions is that the sum of values within them adds up to one. So if you go from 0 to 255 (in the case of an 8-bit image) adding up and plotting the sum of all the probabilities as you go, you will get a line going from zero to one. this line is known as the cumulative distribution function.
The cumulative distribution function effectively shows the probability for that specific image that any particular pixel will have an intensity lower than the point on the line at which you are looking. So towards the left (or zero intensity) of the plot, the probability is close to zero that a pixel will have a lower intensity, while as you approach the right of the plot (or maximum intensity) the probability approaches one.
In images where most of the pixels are within a narrow range of intensities the cumulative distribution function will have a sigmoidal or s-shaped curve, as in the example above.
How histogram equalisation works is by finding a transformation for every pixel intensity to a new intensity so that the cumulative distribution function instead resembles a straight line.

As you can see the peak in the original histogram is spread out over the full range of possible intensities. You’ll also notice that are gaps in the new histogram. There are some values in the new distribution that have no pixels associated with them.

The nature of this transformation is such that its possible some pixel intensities in the original image are aggregated together in the new image. In other words, some data can be lost so care should be taken.

For the mathematically inclined you can find more details of the calculations involved in the links below, but it’s not important for the purposes of this course to read or understand them in full. We will, however, show you how to perform histogram equalisation in Fiji and in Python using OpenCV in the sections below.

Histogram equalisation in Fiji

As is often the case, its easy to do histogram equalisation in Fiji, you just need to know where to look. In this case its just Process -> Enhance Contrast. This should open a window like the one shown below.

An image of a plant growing on some bricks in Fiji, with the histogram and the window for Enhance contrast shown.

Tick the box marked ‘Equalize histogram’, then OK, and you’re done.

The same image post-histogram equalisation, with the new histogram shown alongside.

You can use histogram equalisation on 16-bit as well as 8-bit images in Fiji. Try it out on the sample image ‘M51 Galaxy’ under File -> Open Samples for example.

Histogram equalisation in Python/OpenCV

Histogram equalisation is equally straightforward using Python thanks to OpenCV. First we need to import the OpenCV library and open an image. Let’s assume we have an image called ‘leaves_on_brick.png’ in our current folder (you can download the image attached below and try this for yourself).


Then to open the image and store it with the name img we need the following:

import cv2
img = cv2.imread('leaves_on_brick.png',0)

We need to remember to add the zero to ensure OpenCV opens the image as grayscale data, otherwise the histogram equalisation won’t work.
Then to make a new image (we name hist_equal) with histogram equalisation it’s as simple as:

hist_equal = cv2.equalizeHist(img)

To display the images before and after histogram equalisation we can add the following code:

cv2.imshow('original image',img)
cv2.imshow('equalised image',hist_equal)

The image of leaves on brick pre- and post-histogram equalisation in OpenCV.

The last two lines just tell Python to wait for the user to hit a key before closing the image windows again.

This article is from the free online

Introduction to Image Analysis for Plant Phenotyping

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education