MATLAB Answers

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

1 view (last 30 days)
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

Travis Albee
Travis Albee on 6 Apr 2017
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.
Travis Albee
Travis Albee on 6 Apr 2017
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.

Accepted Answer

Joseph Cheng
Joseph Cheng on 6 Apr 2017
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

Travis Albee
Travis Albee on 6 Apr 2017
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!