MATLAB Answers

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

21 ビュー (過去 30 日間)
M J
M J 2020 年 6 月 30 日
回答済み: Walter Roberson 2020 年 6 月 30 日
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?
Thank you very much.
Regards

  0 件のコメント

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2020 年 6 月 30 日
However, Matlab keeps (automatically) adding a black background to my images.
I am not aware of even one image file format that supports "ragged" arrays, where the location of each block of pixels is specified and there are "holes" everywhere else. The closest I can think of at the moment is that some movie formats such as H.264 can specify blocks of data that are to overlay whatever was previously in those locations -- but those formats always start with an implied background rather than "holes".
For image files, what is used instead is either alpha data or else using color mapping in which particular values can be indicated as transparent. But transparent does not mean that the pixels are not there: transparent means that the background shows through.
I think it very likely that what you have is image files that have alpha data specified, and that you are either failing to read in the alpha data, or else you are not telling MATLAB to use the alpha data when it displays the image.
For example,
[img, ~, transp] = imread('https://upload.wikimedia.org/wikipedia/commons/a/ac/NewTux.png');
alphadata = im2double(transp);
subplot(1,2,1)
h = imshow(img);
h.AlphaData = alphadata;
Alpha data is not necessarily just 0 (transparent) or maximum (completely opaque) so you need to decide whether for histogram equalization you want anything non-zero to be included, or anything non-maximum to be excluded.
mask = alphadata >= 0.5;
And now you can
masknd = repmat(mask,[1 1 size(img,3)]);
masked_pixels = img(masknd); %extract into vector
masked_pixels = reshape(masked_pixels, [nnz(mask), ones(1,ndims(img)-2), size(img,3)]); %reshape to rgb if needed
equalized_masked = histeq(masked_pixels);
eq_img = img;
eq_img(masknd) = equalized_masked;
subplot(1,2,2)
h = imshow(eq_img);
h.AlphaData = alphadata;
I suggest you consider using double(mask) as the AlphaData for imshow purposes. In particular, look at the shadows underneath the penguin: they are semi-transparent with alpha less than 1/2 so they are not included in equalization, but they do show up when using alphadata as the transparency mask -- which is done to be consistent with the transparency data stored in the original image.

  0 件のコメント

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by