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

127 ビュー (過去 30 日間)
Rageeni sah
Rageeni sah 2014 年 4 月 23 日
コメント済み: DGM 2023 年 2 月 23 日
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
Image Analyst
Image Analyst 2020 年 7 月 11 日
Call imclearborder():
mask = imclearborder(mask);

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

その他の回答 (1 件)

Hazwani Mohmad Ramli
Hazwani Mohmad Ramli 2020 年 12 月 15 日
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.

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by