Combining Color Images as one

SO I have three separate green, blue, and red images. I want to display these three separate images as one combined image, while displaying them each individually as well, like in the picture below. However, my code is not working and only returns an error message instead. Could someone help me out? Thanks!
Bimage = zeros(256, 256, 3, 'uint8');
Bimage(:, :, 3) = repmat(255:-1:0, 256, 1);
imagesc(Bimage)
Gimage = zeros(256, 256, 3, 'uint8');
Gimage(:, :, 2) = repmat(0:1:255, 256, 1);
imagesc(Gimage)
Rimage = zeros(256, 256, 3, 'uint8');
Rimage(:, :, 1) = repmat(0:1:255, 256, 1);
R2= imrotate(Rimage,90); %Rotate image by 90 degrees
imagesc(R2)
subplot(2,2,1), mesh(Bimage); title('Blue Image');
subplot(2,2,2), mesh(Gimage); title('Green Image');
subplot(2,2,3), mesh(R2); title('Red Image');
subplot(2,2,4), mesh(Bimage,Gimage,R2); title('Rainbow Image');

 Accepted Answer

Bimage = zeros(256, 256, 1, 'uint8');
Bimage(:, :, 3) = repmat(255:-1:0, 256, 1);
Bimage=Bimage(:,:,3);
imagesc(Bimage)
Gimage = zeros(256, 256, 1, 'uint8');
Gimage(:, :, 2) = repmat(0:1:255, 256, 1);
Gimage=Gimage(:,:,2);
imagesc(Gimage)
Rimage = zeros(256, 256, 1, 'uint8');
Rimage(:, :, 1) = repmat(0:1:255, 256, 1);
R2=imrotate(Rimage,90); %Rotate image by 90 degrees
R2=R2(:,:,1);
imagesc(R2)
subplot(2,2,1), imshow(Bimage); title('Blue Image');
subplot(2,2,2), imshow(Gimage); title('Green Image');
subplot(2,2,3), imshow(R2); title('Red Image');
subplot(2,2,4), imshow(cat(3,Bimage,Gimage,R2)); title('Rainbow Image');

5 Comments

Thank you! Could you possibly explain the R2=R2(:,:,1) line and the cat function?
First of all, I think that function "imshow" is better than "mesh" for image visualization.
Each base color, red, blue, and green, can be expressed as two dimensional matrix, and you can express mixed color by making three dimensional matrix using base color matrix.
If my answer help your work, please accept my answer!
I have accepted it so thank you! If I wanted to rotate the green image so that it is brightest in the bottom left corner and darkest in the top right, is that possible with my code?
To express amount of base color, two dimensional matrix is enough. In your code,
Bimage(:, :, 3) = repmat(255:-1:0, 256, 1);
This is used to express lack of blue as dark color.
And, cat function is used to merge matrix. Color image is considered as 3 dimensional matrix, image(x,y,c), in matlab. x and y are spatial coordinate, c is each base color.
image_data=cat(3,R2,Gimage,Bimage);
imshow(image_data)
Like this, we can stack base color matrix using cat function.
Sorry, my english is not good.
Yes, you can. You already use rotation in red color matrix using function "imrotate"
Gimage = zeros(256, 256, 1, 'uint8');
Gimage(:, :, 2) = repmat(0:1:255, 256, 1);
Gimage=Gimage(:,:,2);
Gimage=imrotate(Gimage,-90);
imagesc(Gimage)
Change code like this.

Sign in to comment.

More Answers (1)

Try this:
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
greenImage = uint8(repmat(0:255, 256, 1));
redImage = imrotate(greenImage, 90);
blueImage = imrotate(greenImage, 180);
subplot(2, 2, 1);
imshow(redImage);
title('Red Image', 'FontSize', fontSize);
subplot(2, 2, 2);
imshow(greenImage);
title('Green Image', 'FontSize', fontSize);
subplot(2, 2, 3);
imshow(blueImage);
title('Blue Image', 'FontSize', fontSize);
% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redImage, greenImage, blueImage);
subplot(2, 2, 4);
imshow(rgbImage);
title('Color Image', 'FontSize', fontSize);

1 Comment

Thank you for answering! Your way of doing it has interested me and I will definitely look into it! thank you!!

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!