How to use histogram equalization for a given image, without including the black background ?

Illustration
Rebekah - 2020-10-05T12:55:31+00:00
Question: How to use histogram equalization for a given image, without including the black background ?

I have images with no background (which i obtained via image masking) that I'm trying to upload on Matlab. However, Matlab keeps (automatically) adding a black background to my images. This poses a problem when I try to use histogram equalization, with the function histeq(), as the dark background seems to affect the process of color adjustment. Is there a way to use histogram equalization on the image itself without accounting for the background?

Expert Answer

Profile picture of Prashant Kumar Prashant Kumar answered . 2025-11-20

To perform histogram equalization on an image while excluding the black background in MATLAB, you can follow these steps:

1. Read the Image:
   Load the image into MATLAB.

   
   img = imread('yourimage.webp');
   

2. Identify Non-Background Pixels:
   Create a mask to identify non-background pixels. Assuming the black background has pixel value 0.

   
   mask = img > 0;
   

3. Apply Histogram Equalization:
   Perform histogram equalization on the non-background pixels.

   
   equalized_img = img;  % Copy the original image
   equalized_img(mask) = histeq(img(mask));
   

4. Display the Result:
   Visualize the original and equalized images.

  
   subplot(1, 2, 1);
   imshow(img);
   title('Original Image');
   
   subplot(1, 2, 2);
   imshow(equalized_img);
   title('Equalized Image');
  

This process enhances the contrast of the image while leaving the black background unchanged.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!