How to convert a binary image to a gray scale image?

85 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2023-2-23
grayscaleimage = 0 + logicalimage;
or
grayscaleimage = uint8(255).*uint8(logicalimage);
or
grayscaleimage = zeros(size(logicalimage), 'uint8') ;
grayscaleimage(logicalimage) = uint8(255) ;
DGM
DGM 2023-2-23
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.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2014-4-23
Assuming you meant a color image, there is an rgb2gray() function but it won't get rid of background. There are many, many types of backgrounds (uniform, colored, textured, gradient, etc.). So to give you a good answer, we'll need to see your image. Please attach it.
If you really meant a binary image, like a true/false, 1/0, logical image, then you can convert it to a uint8 gray scale image just by doing
uint8Image = uint8(255 * binaryImage);
Though that won't get rid of any background.
  11 个评论
sinan salim
sinan salim 2020-7-11
i need to extract the small object in the image of retina mask,,like make all the image black only the small spot white ..so pls how can do it..thanks in advance

请先登录,再进行评论。

更多回答(1 个)

Hazwani Mohmad Ramli
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 个评论
Image Analyst
Image Analyst 2020-12-15
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
DGM 2023-2-23
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with Image Processing Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by