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

조회 수: 121 (최근 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