Contrast Enhancement of Grayscale Images Using Morphological Operators | by Shivanee Jaiswal | Towards Data Science

Contrast Enhancement is a very common image processing technique for enhancing features in low contrast images. Several methods like Contrast Stretching, Histogram Equalization, Adaptive Histogram Equalization, Contrast-Limited Adaptive Histogram Equalization or CLAHE, etc. have been used for enhancing the contrast of images. In this article, we will look at another method of Contrast Enhancement which is performed using a combination of Morphological Transformations.
Morphological Transformations or Morphological Operators are simple image transformations that are usually applied on binary images, but can be applied to grayscale images as well. There are various types of Morphological Transformations like Erosion, Dilation, Opening, Closing, Gradient, Top Hat and the Black Hat. The two main components of these transformations are the input image and a kernel which is known as Structuring Element (SE). Before we jump on to the different types of Morphological Transformations in detail, let us understand the Structuring Element.
The Structuring Element (SE) is the neighborhood around each pixel that is examined while performing the morphological operations. Structuring Element can be of different shapes and sizes, and changing it can significantly impact the performance of the transformation.
There are three shapes of the Structuring Element provided by OpenCV — Rectangular, Elliptical and Cross-Shaped. The figure below shows these three shapes.
The different types of Morphological Operators are:
Erosion — According to the OpenCV Documentation, the Erosion operator works similar to soil erosion. It “erodes”, or in simpler words, removes the boundaries of the foreground object in the image. The pixels at the boundary of the foreground object are removed. It reduces the size of the object in the image.Dilation — Dilation works exactly opposite to erosion. It “dilates” or expands the boundaries of the foreground object which in turn increases the size of the object in the image. This is useful if we want to join broken parts of the foreground object.Opening — The Opening operation is used in noise removal. It is done by first applying the erosion operation, followed by the dilation operation. The erosion operation first removes all the small noisy areas from the image and then dilation is applied to restore the original size of the object.Closing — The Closing is the opposite of Opening, which means that dilation is applied first followed by erosion. This is useful for joining or filling small spots that are present in the foreground object, while retaining the size of the object.Gradient — Morphological Gradient of an image is obtained by subtracting the erosion result from the dilation result. The result of the Gradient operation is the outline of the foreground object in the image.Top Hat / White Hat— Top Hat Transform, also known as the White Hat Transform, is obtained by removing or subtracting the Opening of the image from the original image. This operator gives us the bright features in the image that are smaller than the Structuring Element.Black Hat / Bottom Hat— The Black Hat Transform, also known as the Bottom Hat Transform, is obtained by removing or subtracting the Closing of the image from the original image. This operator gives us the dark features in the image that are smaller than the Structuring Element.
Erosion — According to the OpenCV Documentation, the Erosion operator works similar to soil erosion. It “erodes”, or in simpler words, removes the boundaries of the foreground object in the image. The pixels at the boundary of the foreground object are removed. It reduces the size of the object in the image.
Dilation — Dilation works exactly opposite to erosion. It “dilates” or expands the boundaries of the foreground object which in turn increases the size of the object in the image. This is useful if we want to join broken parts of the foreground object.
Opening — The Opening operation is used in noise removal. It is done by first applying the erosion operation, followed by the dilation operation. The erosion operation first removes all the small noisy areas from the image and then dilation is applied to restore the original size of the object.
Closing — The Closing is the opposite of Opening, which means that dilation is applied first followed by erosion. This is useful for joining or filling small spots that are present in the foreground object, while retaining the size of the object.
Gradient — Morphological Gradient of an image is obtained by subtracting the erosion result from the dilation result. The result of the Gradient operation is the outline of the foreground object in the image.
Top Hat / White Hat— Top Hat Transform, also known as the White Hat Transform, is obtained by removing or subtracting the Opening of the image from the original image. This operator gives us the bright features in the image that are smaller than the Structuring Element.
Black Hat / Bottom Hat— The Black Hat Transform, also known as the Bottom Hat Transform, is obtained by removing or subtracting the Closing of the image from the original image. This operator gives us the dark features in the image that are smaller than the Structuring Element.
Note: The Top Hat and the Black Hat transforms are more suited for grayscale images.
Contrast Enhancement, in simple words, requires the following to be done:
Making the bright regions in the image brighter.Making the dark regions in the image darker.
Making the bright regions in the image brighter.
Making the dark regions in the image darker.
As we had seen earlier, the result of the Top Hat Transform is an image consisting of all the bright features in the input image and the result of the Black Hat Transform is an image consisting of all the dark features in the input image. Thus, for the purpose of Contrast Enhancement, we will need the Top and the Black Hat Transforms of the input image.
After obtaining the Top and Black Hat Transforms of the input image, we will add the Top Hat Transform to the input image in order to make its bright regions brighter, and subtract the Black Hat Transform from the input image to make its dark regions darker.
The step mentioned above can be represented as an equation shown below:
where R is the result image, I is the input image, T and B are the Top Hat and the Black Hat transforms respectively.
The flowchart below depicts the steps we will follow to enhance the contrast.
We will implement this Contrast Enhancement technique using Python and OpenCV. We will need to first install opencv-python using pip.
pip install opencv-python
After installing OpenCV, we will import the library in our code.
import cv2 as cv
We will use the below image for our code, which is taken from the COVID-19 Image Repository on GitHub.
The image data in this repository has been collected from the Institute for Diagnostic and Interventional Radiology, Hannover Medical School, Hannover, Germany and are licensed under the Creative Commons Attribution 3.0 Unported.
I have scaled down the image by 50% to reduce the size of the image.
To read this image, we will use the imread function by OpenCV.
filename = # path to the image fileimg = cv.imread(filename,0)
Now that we have our image, we will obtain the Top and the Black Hat Transforms of this image. Before this, we need to construct our Structuring Element or the kernel. We can use the getStructuringElement function provided by OpenCV for this purpose.
kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE,(5,5))
In the above snippet, we have constructed an elliptical Structuring Element of size (5,5). You can experiment by changing these parameters and observe the effect on the output.
The next step is to get the transforms of the input image using the kernel we constructed in the previous step.
# Top Hat TransformtopHat = cv.morphologyEx(img, cv.MORPH_TOPHAT, kernel)# Black Hat TransformblackHat = cv.morphologyEx(img, cv.MORPH_BLACKHAT, kernel)
Once we have our transforms, we will apply the equation that we had seen earlier.
res = img + topHat - blackHat
We can see the contrast of the input image has improved a bit. Some of the minute features, that were not prominent in the input image, are now visible. However, this technique also adds some noise to the image if the Structuring Element is not chosen carefully.
For example, the image below shows the output when an elliptical Structuring Element was chosen of size (15,15). The minute features in the lungs and the edges of the bones are now more prominent and clear than the earlier output, but we can see some noisy areas in the output image, near the boundaries of the body, i.e. the white patches in the background.
As you go on increasing the size of the Structuring Element, the foreground features will become more prominent but the background will start becoming more and more noisy. The output below with Structuring Element of size (35,35) has more noisy area in the background.
Thus, we saw how to enhance the contrast of grayscale images using a combination of Top Hat and Black Hat Morphological Operations. Some of the minute features were greatly enhanced and became more prominent in our output image as seen above.
This method might not work as efficiently as the original Contrast Stretching method due to the noise it introduces in the image, as we go on increasing the size of our Structuring Element.As the Structuring Element is basically the size of the neighborhood to consider while applying the transformations, the output will also depend on the size of the input image. For example, a (35,35) kernel for an image of size 1000 x 1000 will form a smaller region as compared to a (35,35) kernel for a 250 x 250 image. Thus, resizing the image will also affect the output of this method.As Top and Black Hat Transforms give us the brighter and darker features respectively that are smaller than the Structuring Element, this method of Contrast Enhancement will work well if the size of the features to enhance are smaller than the size of our chosen Structuring Element. Thus, it becomes difficult to accurately enhance the contrast of larger features, as greater sizes of our kernel will introduce larger noisy areas in our image background.
This method might not work as efficiently as the original Contrast Stretching method due to the noise it introduces in the image, as we go on increasing the size of our Structuring Element.
As the Structuring Element is basically the size of the neighborhood to consider while applying the transformations, the output will also depend on the size of the input image. For example, a (35,35) kernel for an image of size 1000 x 1000 will form a smaller region as compared to a (35,35) kernel for a 250 x 250 image. Thus, resizing the image will also affect the output of this method.
As Top and Black Hat Transforms give us the brighter and darker features respectively that are smaller than the Structuring Element, this method of Contrast Enhancement will work well if the size of the features to enhance are smaller than the size of our chosen Structuring Element. Thus, it becomes difficult to accurately enhance the contrast of larger features, as greater sizes of our kernel will introduce larger noisy areas in our image background.
Thank you for reading! :) You can connect with me on LinkedIn if you have any questions.
OpenCV: Morphological Transformations https://docs.opencv.org/3.4/d9/d61/tutorial_py_morphological_ops.htmlKushol R., Nishat R. M., Rahman A. B. M. A., Salekin M. M., “Contrast Enhancement of Medical X-Ray Image Using Morphological Operators with Optimal Structuring Element,” arXiv:1905.08545v1 [cs.CV] 27 May 2019Hinrich B. Winther, Hans Laser, Svetlana Gerbel, Sabine K. Maschke, Jan B. Hinrichs, Jens Vogel-Claussen, Frank K. Wacker, Marius M. Höper, Bernhard C. Meyer, “COVID-19 Image Repository,” DOI: 10.6084/m9.figshare.12275009
OpenCV: Morphological Transformations https://docs.opencv.org/3.4/d9/d61/tutorial_py_morphological_ops.html
Kushol R., Nishat R. M., Rahman A. B. M. A., Salekin M. M., “Contrast Enhancement of Medical X-Ray Image Using Morphological Operators with Optimal Structuring Element,” arXiv:1905.08545v1 [cs.CV] 27 May 2019
Hinrich B. Winther, Hans Laser, Svetlana Gerbel, Sabine K. Maschke, Jan B. Hinrichs, Jens Vogel-Claussen, Frank K. Wacker, Marius M. Höper, Bernhard C. Meyer, “COVID-19 Image Repository,” DOI: 10.6084/m9.figshare.12275009