Histogram equalisation in Fiji and OpenCV
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.
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.
Tick the box marked ‘Equalize histogram’, then OK, and you’re done.
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)
cv2.waitKey(0)
cv2.destroyAllWindows()
The last two lines just tell Python to wait for the user to hit a key before closing the image windows again.
Introduction to Image Analysis for Plant Phenotyping
Introduction to Image Analysis for Plant Phenotyping
Register to receive updates
-
Create an account to receive our newsletter, course recommendations and promotions.
Register for free







