convert binary image to RGB image

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

3 Commenti

fereshte
fereshte il 16 Giu 2014
Modificato: fereshte il 16 Giu 2014
i use this code but failed!
binaryImage=imread('binary.bmp');
RGB = cat(3, binaryImage, binaryImage, binaryImage);
figure;
imshow(RGB);
Joseph Cheng
Joseph Cheng il 16 Giu 2014
Modificato: Joseph Cheng il 16 Giu 2014
What failed? code seems fine. What are you looking for as a result?
fereshte
fereshte il 17 Giu 2014
Modificato: fereshte il 17 Giu 2014
same binary image but with main colors.i want to processing on colored extraction parts and dont need to binary image same heel image i want convert binary image to colored image

Accedi per commentare.

Risposte (2)

Youssef  Khmou
Youssef Khmou il 16 Giu 2014
Modificato: Youssef Khmou il 16 Giu 2014

1 voto

The technique you used failed because the function does not provide a visualization of three dimensional logical matrices, besides i do not think the binary to RGB transformation is possible because there are not enough information about the channels, thus RGB to binary is possible .
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.

9 Commenti

i get this error: image is not a binary image.
fereshte
fereshte il 17 Giu 2014
Modificato: fereshte il 17 Giu 2014
I have a guestion a few days ago to extract a part of footprint image.is it possible in the code that outputs be colored at all stages instead of binary image?
I don't know what that means.
If the image is not binary, then the code throws an error. Why is it not binary like you said? Maybe all you have to do is to threshold the grayscale version of it.
binaryImage = rgb2gray(binaryImage)>128;
i run it again and get this:
??? Undefined function or variable 'fullFileName'.
Error in ==> test1 at 3 if ~exist(fullFileName, 'file')
You should know that MATLAB is case sensitive and learn how to interpret these error messages so you can fix them. In my untested code I didn't use consistent capitalization. Change the line to
fullFileName = fullfile(pwd, baseFileName);
Like I said, it is untested so there may be other errors that hopefully you can fix. If not, let me know. I don't have time to do fully tested and validated demo programs for every question I answer so sometimes people have to fix the code. If it does run perfectly (because I've actually tested it in MATLAB), then I usually attach those m-files rather than put them in the message body.
fereshte
fereshte il 17 Giu 2014
Modificato: fereshte il 18 Giu 2014
sorry thanks for youre help.my image is binary. why give me an error that not a binary image!!!!!!!!!!!!!!!
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.
Maybe it would help if @fereshte can upload the BMP image in question to the forums ? That way you can reduce some of the back-n-forth and known limitations of MATLAB code from the answers forum
The images were uploaded, back on June 17th. There's no more back and forth - I guess it's not a problem for him anymore (3 months later).

Accedi per commentare.

Categorie

Tag

Richiesto:

il 16 Giu 2014

Commentato:

il 28 Set 2015

Community Treasure Hunt

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

Start Hunting!

Translated by