106 views (last 30 days)

how convert binary image to RGB image? my original image and binary image attached. thanks

Image Analyst
on 17 Jun 2014

Your code is almost okay but not quite robust enough. And you didn't scale it by multiplying by 255 so that you can see it (otherwise it will have values of just 0 or 1 which will be so dark as to be virtually invisible). If you're going to display an RGB image like that the image has to be a uint8 image in the range of 0-255. So a more robust way to do it is this (untested):

baseFileName = 'binary.bmp';

fullfileName = fullfile(pwd, baseFileName);

if ~exist(fullFileName, 'file')

message = sprintf('Image file %s was not found!', fullFileName);

uiwait(warndlg(message));

return;

end

binaryImage = imread(fullFileName);

[rows, columns, numberOfColorChannels] = size(binaryImage)

maxValue = max(binaryImage(:))

minValue = min(binaryImage(:))

if numberOfColorChannels > 1 || minValue < 0 || maxValue > 1

message = sprintf('Image is not a binary Image!');

uiwait(warndlg(message));

return;

end

grayImage = 255 * uint8(binaryImage);

RGB = cat(3, grayImage, grayImage, grayImage);

imshow(RGB);

Try that and tell me how it goes.

Image Analyst
on 18 Jun 2014

A true binary image is a logical image that can have values of true or false. You probably have an image that you think is binary because it has pixels that appear only black or white. This is possible if the image is floating point with values of 0 and 1, or grayscale with values of 0 and 1 if you used imshow(grayImage, []), or grayscale with values of 0 and 255 if you used imshow(grayImage), or color with RGB values of [0,0,0] and [255,255,255]. I suspect, since it's a BMP image, that you have the last case - a color image. You can convert to a binary image (I think I told you this in your duplicate question already):

binaryImage = binaryImage(:,:,2) > 128;

Note that in the above fix, binaryImage started out as an RGB image but we extracted the green channel (which looks the same as the other two color channels) and thresholded it. Now binaryImage is of type "logical" and have values of true and false so it is a true binary image now.

Muthu Annamalai
on 28 Sep 2015

Image Analyst
on 28 Sep 2015

Sign in to comment.

Youssef Khmou
on 16 Jun 2014

Edited: Youssef Khmou
on 16 Jun 2014

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/134746-convert-binary-image-to-rgb-image#comment_220496

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/134746-convert-binary-image-to-rgb-image#comment_220496

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/134746-convert-binary-image-to-rgb-image#comment_220531

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/134746-convert-binary-image-to-rgb-image#comment_220531

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/134746-convert-binary-image-to-rgb-image#comment_220761

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/134746-convert-binary-image-to-rgb-image#comment_220761

Sign in to comment.