Rebekah asked . 2020-10-05

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?

matlab , image processing, histogram equalization

Expert Answer

Prashant Kumar answered . 2024-04-26 23:47:00

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.


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.