MATLAB Answers

0

How do I piece together a 2-dim image using 3 given images, each representing R, G, B?

Asked by Travis Albee on 6 Apr 2017
Latest activity Commented on by Travis Albee on 6 Apr 2017
I was given 3 images, all are 2-dim arrays. One represents R (red), G (green), and B (blue). Each of these are of the same image but representing RGB. I am supposed to create a composite image using those three that are given. What is the best way to do this in Matlab? I read in each image, then I did this:
R = red; %changing variable name G = green;
B = blue;
I_rgb = cat(2,R,G,B);
imagesc(I_rgb)
I tried setting the dim = 3 but Matlab didn't like it because, I assume, the images are all 2-dim. The way I have it, instead of getting back one image, I get back the 3 images side by side.

  2 Comments

Nevermind everyone. I got it to work. I set the Dim = 3 and concatenated the image properly. I must of had old data loaded and once I used clear all it worked.
Alas though, I have a new question...the image is of a city (as taken from a high altitude). I have to change the color of the man-made structures to yellow and the water to bright blue. What's a good way to do this in Matlab?

Sign in to comment.

1 Answer

Answer by Joseph Cheng
on 6 Apr 2017
 Accepted Answer

you should be using cat(3,red,green,blue) because you want to stack in the 3rd dimension.
x = randi(3,3,3)
x =
3 3 3
1 1 2
3 3 2
cat(3,x,x,x)
your statement "I tried setting the dim = 3 but Matlab didn't like it" doesn't tell us much on why matlab didn't like it. my suspicion is that all 3 are not the same size.

  1 Comment

Thanks Joseph. In regards to the last statement in your comment, apologize for not being more clear. In my code, I used I_rgb = cat(2, R, G, B). I changed the 2 to a 3 and the code mashed the images together correctly. But I do have another question, which I wrote above and am hoping you can help on.
Here is my question: The image is of a city (as taken from a high altitude...looks like a satellite image). I have to change the color of the man-made structures to yellow and the water to bright blue. What's a good way to do this in Matlab?

Sign in to comment.