How to convert binary image to RGB image in matlab?

How to convert binary image to RGB image in matlab?I have converted an RGB image into binary using J1=im2bw(J1);.Now I want to convert it back to RGB image.

1 Comment

It is of course impossible (for a computer or a human) to recreate information that has been purposefully deleted.

Sign in to comment.

Answers (2)

Hi,
you need to replicate the bw image for each color channel, convert to uint8 and multiply by 255:
J1 = 255 * repmat(uint8(J1), 1, 1, 3));
Note that of course it will still be black and white only ;-).
Titus

7 Comments

Thank you sir.Is there any way to get color image back.
Or can you suggest any method to convert RGB to binary image and afterwards back to RGB so that I can get the original color image back.
No, not really. The black&white image does not contain any color info anymore, so where should it come from? Suppose you do the following:
x = rand(1,5)
x =
0.1576 0.9706 0.9572 0.4854 0.8003
% now convert to bw:
xbin = round(x)
xbin =
0 1 1 0 1
Taking these zeros and ones there is no way to recover the lost information.
Please explain what your goal is!
My aim is to encrypt an RGB image by performing XOR with another image I to generate a share S.Then during decryption should perform XOR with the generated share S and the image I to obtain the RGB image back.Can you please suggest any method.
As answered in your other question xor is the wrong tool.
But by using Bitxor also I didnt obtain back the RGB image correctly.
Please comment in that other question. bitxor is what you want, if you do not get the right result, you must have made an error in your code, so please post it.

Sign in to comment.

function [RGB_Image] = convertBinImage2RGB(BinImage)
[fil, col] = size(BinImage);
RGB_Image = zeros(fil,col, 3);
[posX , posY] = find(BinImage==1);
numIter = size(posX,1)*size(posX,2);
for ii = 1 : numIter
RGB_Image(posX(ii),posY(ii), 1:3) = 255;
end % for
end % function

3 Comments

Or just
function [RGB_Image] = convertBinImage2RGB(BinImage)
RGB_Image = uint8( BinImage(:,:,[1 1 1]) * 255 );
with no loop.
Please could you tell what the below line does?
BinImage(:,:,[1 1 1]) * 255 )
it multiples all row and columns by 255 i understood this.
but what does [1 1 1] does?
BinImage(:,:,K)
selects all of the 3D panes of BinImage that match the values in K, in order, right?
Well, there is no restriction against duplicating an index. Just the same way that you could say BinImage(:,:,[1 2]) or something like BinImage(:,:,[3 2 1]) if you had reason to (and the array was the right size), you can also ask for BinImage(:,:,[1 1 1]) which means to take the first pane of BinImage three times. Those would, of course, all be the same. So BinImage(:,:,[1 1 1]) for a 2D array BinImage would be the same as repmat(BinImage, 1, 1, 3), three copies of the 2D array BinImage, arranged as a 3D array.
So overall the code makes three stacked copies of BinImage, multiplies the result by 255 (getting out values that are either 0 or 255 in each location), and then uint8() to convert those all to uint8(0) and uint8(255) . An RGB image that is full black (where the 0's are), or full white (where the 255 are)

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 5 Jun 2015

Commented:

on 5 Feb 2022

Community Treasure Hunt

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

Start Hunting!