Increase and decrease the brightness of an image in MATLAB


In an image, brightness is an attribute that is interpreted by our eyes based on the amount of light reflected by different regions of the image. Brightness is related to the intensity of light in the image i.e., if an image has more light it will appear brighter. If the image has less light, it will appear as darker.

Therefore, brightness influences the visual quality of an image. However, we have various tools like MATLAB to adjust the brightness of the image and enhance its visual quality.

In a digital image, the brightness is a controlled by pixel values of the image. Therefore, if we want to adjust the brightness of an image, we need to change the values of pixels of different color channels of the image.

If we have to increase the brightness of an image, we require to increase the intensity values of the image pixels.

On the other hand, if we need to decrease the brightness of the image, we require to decrease the intensity values of the image pixels.

MATLAB is a powerful tool to process digital images. It provides various methods to increase and decrease the brightness of an image efficiently. Read this article to learn the methods of increasing and decreasing the brightness of an image using MATLAB.

By Scaling the Pixel Values

In MATLAB, scaling pixel values is the simplest method of increasing and decreasing the brightness of an image.

In this method, we define a scaling factor to adjust the intensity values of pixels of an image.

The steps involved in increasing and decreasing the brightness of an image using the scaling pixel values method are explained below −

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

  • Step 2 − Specify a scaling factor to change the brightness of the image.

  • Step 3 − Multiple the input image with the scaling factor to adjust the brightness of the image.

  • Step 4 − Clip pixel values to the valid range i.e., [0, 255].

  • Step 5 − Convert the modified image to an appropriate format and display using the "imshow" function.

Example

Let us now understand the implementation of these five steps to change the brightness of an image using MATLAB.

% MATLAB code to change brightness of image using scaling factor
% Read the input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');

% Specify a scaling factor
S1 = 1.5;	% Increase brightness
S2 = 0.7;	% Decrease brightness

% Scale the input image to adjust the brightness
brightened_img = img * S1;
darker_img = img * S2;

% Adjust pixel values to the valid range
brightened_img = max(0, min(brightened_img, 255));
darker_img = max(0, min(darker_img, 255));

% Convert to uint8 format to display
brightened_img = uint8(brightened_img);
darker_img = uint8(darker_img);

% Display the input, brightened, and darker images
figure;
subplot(1, 3, 1);
imshow(img);
title('Input Image');

subplot(1, 3, 2);
imshow(brightened_img);
title('Brightened Image');

subplot(1, 3, 3);
imshow(darker_img);
title('Darker Image');

Output

With our input image, we got the following output −

By Adding and Subtracting a Constant

In this method, we add and subtract a constant value to the given image to increase and decrease the brightness of the image respectively.

It is a straightforward method of changing the brightness of an image in MATLAB.

The step-by-step process of increasing and decreasing the brightness of an image by adding and subtracting a constant to the image is explained below −

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

  • Step 2 − Add a constant "X" to the input image to increase its brightness. Or subtract a constant "X" from the input image to decrease its brightness.

  • Step 3 − Convert the resulting image to a suitable format like "uint8" to display.

  • Step 4 − Display the adjusted images using the "imshow" function.

Example

Let us understand this method with the help of an example program in MATLAB.

% MATLAB code to change brightness of image by adding and subtracting a constant
% Read the input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425269.jpg');

% Adjust the brightness of the image
brightened_img  = img + 40;	% Increase brightness
darker_img = img - 40;	% Decrease brightness

% Convert to uint8 format to display
brightened_img = uint8(brightened_img);
darker_img = uint8(darker_img);

% Display the input, brightened, and darker images
figure;
subplot(1, 3, 1);
imshow(img);
title('Input Image');

subplot(1, 3, 2);
imshow(brightened_img);
title('Brightened Image');

subplot(1, 3, 3);
imshow(darker_img);
title('Darker Image');

Output

With our input image, we got the following output −

Using Gamma Correction

In MATLAB, there is another method of increasing and decreasing the brightness of an image named "Gamma Correction". This method changes the intensity values of pixels of an image using a power law transformation.

The step-by-step process to increase and decrease the brightness of an image using the Gamma Correction method is described below −

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

  • Step 2 − Define a gamma factor to change the brightness of the image.

  • Step 3 − Adjust the brightness of the image using the "imadjust" function with the specified gamma factor.

  • Step 4 − Display the brightness adjusted image using the "imshow" function.

Example

The following example demonstrates the practical implementation of this method to increase and decrease the brightness of an image.

% MATLAB code to change brightness of image using gamma correction
% Read the input image
img = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425323.jpg');

% Define a gamma factor to adjust the brightness
g1 = 0.4;		% Increase brightness
g2 = 1.7;		% Decrease brightness

% Adjust the brightness of the image
brightened_img  = imadjust(img, [], [], g1);
darker_img  = imadjust(img, [], [], g2);

% Display the input, brightened, and darker images
figure;
subplot(1, 3, 1);
imshow(img);
title('Input Image');

subplot(1, 3, 2);
imshow(brightened_img);
title('Brightened Image');

subplot(1, 3, 3);
imshow(darker_img);
title('Darker Image');

Output

With this input image, we got the following output:

Conclusion

In conclusion, brightness in an image is a property related to the intensity of light in the image. If an image has more light, it is said to brighter. If it has less light, then it is caller darker. Therefore, increasing and decreasing brightness of an image make it brighter and darker respectively.

MATLAB provides various efficient methods to increase and decrease the brightness of an image. In this tutorial, I explained all the commonly used methods to adjust the brightness of an image using MATLAB.

Updated on: 25-Oct-2023

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements