Question
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?
Related Questions
Expert Answer
Prashant Kumar
PhD Expert
Answered Nov 20, 2025
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.
Have a different question? Ask here