How to Perform Contrast Enhancement of Color Image in MATLAB?


As we know, MATLAB is a powerful to perform image processing and signal processing. We can use it to alter different parameters of an image like brightness, contrast, saturation, and more. In this article, I will explain how we can perform contrast enhancement of a color image using MATLAB.

What is a Contrast Enhancement?

In digital image processing, the process of improving the colors and visual quality of an image is called contrast enhancement. This technique improves the quality of an image by changing the brightness and color concentration among different regions of the image.

  • Contrast enhancement is used to make an image more visually appealing so that the human eye can easily distinct among different objects within the image.

  • A colored image is usually rendered in the RGB color space. Hence, the contrast enhancement of the color images is done by varying the contrast of each color channels i.e., RGB (Red, Green, and Blue) individually.

  • In MATLAB, there are various techniques that we can use to enhance contrast of an image. Some of them are as histogram equalization, contrast limited adaptive histogram equalization, histogram stretching, enhancement filters, gamma correction, etc

  • Contrast enhancement of color image is widely used in various applications such as image procession, photography, remote imaging, medical imaging, etc.

Let us now learn to enhance the contrast of a color image using MATLAB.

Contrast Enhancement of Color Image in MATLAB

In MATLAB, there are various techniques available to improve the contrast of a colored image. But here we will cover three commonly used techniques of contrast enhancement namely, "Naïve Algorithm", "Standard Algorithm", and "using HSV color space".

  • In the Naïve algorithm, the contrast enhancement of a colored image is achieved by directly adjusting the value of pixels of RGB color channels to change their intensity.

  • In the standard algorithm, the contrast enhancement is performed by using histogram equalization technique.

  • In the case of HSV color space, the contrast enhancement of a color image is performed by adjusting the intensity values while keeping the saturation and hue components constant. The HSV color space method enhances the contrast of a colored image without changing its colors.

Let us now all these methods of contrast enhancement in detail with the help of examples.

Contrast Enhancement of Color Image Using Naïve Algorithm

In the Naïve algorithm, the contrast enhancement of an image is performed directly by changing the pixel values of color channels of the image.

Here is the step-by-step process for enhancing the contrast of a colored image using the Naïve algorithm in MATLAB.

  • Step (1) − Read the input color image with the help of "imread" function.

  • Step (2) − Separate the RGB color channels from the image.

  • Step (3) − Specify a factor to enhance the contrast of each channel of the image.

  • Step (4) − Perform the contrast enhancement of the image using the enhancement factor.

  • Step (5) − Adjust the pixel value of each channel to ensure that they must be within the valid range of colors i.e., [0, 255].

  • Step (6) − Obtain the enhanced color image by combining all the enhanced color channels.

  • Step (7) − Display the results.

We can follow these seven steps to enhance the contrast of a color image using the Naïve algorithm in MATLAB.

Example

Here is an example to demonstrate the code implementation to perform contrast enhancement of a color image using the Naïve algorithm in MATLAB.

% MATLAB code for contrast enhancement using Naïve algorithm
% Read the input color image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425269.jpg');

% Separate the RGB color channels of the image
r_channel = img(:, :, 1);
g_channel = img(:, :, 2);
b_channel = img(:, :, 3);

% Specify the enhancement factors for each color channel
r_factor = 1.25;
g_factor = 1.25;
b_factor = 1.25;

% Multiply pixel values with enhancement factors to perform contrast enhancement
enhanced_r_channel = r_channel * r_factor;
enhanced_g_channel = g_channel * g_factor;
enhanced_b_channel = b_channel * b_factor;

% Adjust pixel values to make them within the valid range
enhanced_r_channel = min(max(enhanced_r_channel, 0), 255);
enhanced_g_channel = min(max(enhanced_g_channel, 0), 255);
enhanced_b_channel = min(max(enhanced_b_channel, 0), 255);

% Obtain enhanced color image by combining all the enhanced channels
enhanced_color_img = cat(3, uint8(enhanced_r_channel), uint8(enhanced_g_channel), uint8(enhanced_b_channel));

% Display the original and enhanced color images
subplot(1, 2, 1);
imshow(img);
title('Original Color Image');

subplot(1, 2, 2);
imshow(enhanced_color_img);
title('Enhanced Color Image');

Output

When you run this code, it will produce the following output −

Contrast Enhancement of Color Image Using Standard Algorithm

We can also perform contrast enhancement of a color image using the standard algorithm in MATLAB. In MATLAB, the standard algorithm uses the histogram equalization method to improve contrast levels in a colored image.

The step-by-step process to perform contrast enhancement using the standard algorithm is explained below.

  • Step (1) − Read the input color image using the "imread" function.

  • Step (2) − Separate the RGB color channels from the image.

  • Step (3) − Obtain the enhanced color channels by using the histogram equalization technique.

  • Step (4) − Obtain the enhanced color image by combining all the enhanced channels.

  • Step (5) − Display the results.

Example

Let us now see an example to understand the code implementation for contrast enhancement using the standard algorithm in MATLAB.

% MATLAB code to perform contrast enhancement using standard algorithm
% Read the input color image
img = imread('your_image.jpg');

% Separate RGB color channels from the image
r_channel = img(:, :, 1);
g_channel = img(:, :, 2);
b_channel = img(:, :, 3);

% Enhance each channel using the histogram equalization
enhanced_r_channel = histeq(r_channel);
enhanced_g_channel = histeq(g_channel);
enhanced_b_channel = histeq(b_channel);

% Obtain the enhanced color image by combining all the enhanced channels
enhanced_color_img = cat(3, enhanced_r_channel, enhanced_g_channel, enhanced_b_channel);

% Display the original and enhanced color images
subplot(1, 2, 1);
imshow(img);
title('Original Color Image');

subplot(1, 2, 2);
imshow(enhanced_color_img);
title('Enhanced Color Image');

Output

When you run this code, it will produce the following output −

Contrast Enhancement of Color Image Using HSV Colorspace Method

In MATLAB, we can also enhance the contrast of a colored image using the HSV color space. In this method, the contrast of the image is enhanced without changing the hue and saturation components of the image.

The steps involved to perform the contrast enhancement using the HSV color space method are explained below.

  • Step (1) − Read the input image using the "imread" function.

  • Step (2) − Convert the image from RGB color space to HSV color space.

  • Step (3) − Separate the V (value) component from the image to perform contrast enhancement.

  • Step (4) − Apply histogram equalization to V component to improve contrast of the image.

  • Step (5) − Replace the original V component with the enhanced V component.

  • Step (6) − Convert the image from HSV color space to RGB color space.

  • Step (7) − Display the enhanced color image.

Example

The following example demonstrates how we can use the HSV color space method to perform contrast enhancement in a color image.

% MATLAB code to perform contrast enhancement using HSV color space
% Read the input color image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425269.jpg');

% Convert the RGB image to HSV image
hsv_img = rgb2hsv(img);

% Separate the V component from the image
V = hsv_img(:, :, 3);

% Use histogram equalization to perform contrast enhancement
enhanced_V = histeq(V);

% Replace the original V component with the enhanced V component in the image
hsv_img(:, :, 3) = enhanced_V;

% Convert the enhanced image from HSV to RGB color space
enhanced_color_img = hsv2rgb(hsv_img);

% Display the original and enhanced color images
subplot(1, 2, 1);
imshow(img);
title('Original Color Image');

subplot(1, 2, 2);
imshow(enhanced_color_img);
title('Enhanced Color Image');

Output

When you run this code, it will produce the following output −

Conclusion

In conclusion, we can use MATLAB to perform contrast enhancement of a color image. MATLAB provides various methods to enhance the contrast levels of an image. In this tutorial, I explained the step-by-step process of contrast enhancement with the help of example programs. In all these examples, I have used a sample image that does not require contrast enhancement. For better result, please replace this image with your image.

Updated on: 06-Oct-2023

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements