How to convert a binary image to a gray scale image?
Show older comments
Hello, I am working on Image processing.Can anyone tell me how can we convert a binary image to a gray scale image such that it leads to elimination of background which is unwanted content for the given image.
Thanks in advance
3 Comments
Rekha B
on 23 Feb 2023
How to convert a logical binary image to a gray scale image?
same question how to convert logical binary image to gray scale image plz help me regard this.
Walter Roberson
on 23 Feb 2023
grayscaleimage = 0 + logicalimage;
or
grayscaleimage = uint8(255).*uint8(logicalimage);
or
grayscaleimage = zeros(size(logicalimage), 'uint8') ;
grayscaleimage(logicalimage) = uint8(255) ;
Or just
mask = im2uint8(mask);
or
mask = im2double(mask);
that way the class of the input doesn't matter.
Of course, the original question wasn't actually about converting a binary mask into a grayscale image, but about some notion of background removal, which isn't clear was necessary.
% a grayscale (MxNx1) uint8 image and a logical mask
inpict = imread('cameraman.tif');
mask = imread('cmantifmk.png')>128;
% apply the mask to the image using logical addressing
% this requires the mask to be a proper logical-class image
outpict = inpict;
outpict(~mask) = 0;
imshow(outpict)
% apply the mask to the image using multiplicative composition
% mask can be logical or numeric class, so long as it's properly-scaled
% mask can be binarized (hard) or any antialiased/graduated mask
% inpict can be grayscale (I) or RGB (assuming we're using R2016b+)
outpict = im2uint8(im2double(inpict).*im2double(mask));
imshow(outpict)
There are other ways to do the same or similar, but maybe that's enough to drive a search query.
Accepted Answer
More Answers (1)
Hazwani Mohmad Ramli
on 15 Dec 2020
0 votes
Anyone please help me.
I want to export this image with grascale format. If I write imshow, they will appear in binary format. what sould I do?
2 Comments
Image Analyst
on 15 Dec 2020
This doesn't look like an Answer to Rageeni's question. Please attach your original image, plus this screenshot, in a new discussion thread, not here. We'll solve it there. And explain what you mean, because I'm not sure what you mean by gray scale and binary. On disk, while's it's in the PNG file, it's binary - 8 bits per pixel in a special format. Once it's read into MATLAB it's probably uint8 or uint16, which is gray scale. Inside MATLAB a binary image means a logical image with values of true or false (1 or 0) and appears just pure black and pure white (meaning no grays in between).
DGM
on 23 Feb 2023
If a nominally-grayscale image appears binarized in imshow, then it's either not what you think it is due to some prior mistake, or it's simply improperly-scaled floating-point. Since nobody knows what the actual images or code was, it's impossible to know for sure.
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
