Hello everyone,
I have images in single bands ( Red, Blue and Green) and I need to combine them to obtain an RGB image.
R = imread('red.png');
G = imread('green.png');
B = imread('blue.png');
RGB = cat(3,R,G,B);
I wrote this code: how can I write and save the so obtained RGB image?
Thanks for your kind help.

 Réponse acceptée

Stephen23
Stephen23 le 2 Fév 2022
Modifié(e) : Stephen23 le 2 Fév 2022

0 votes

I am guessing that the image files are actually Truecolor RGB (with all channels identical) rather than true Grayscale (a sadly all-too-common mixup made by many apps and users), in which case this should work:
RGB = cat(3,R(:,:,1),G(:,:,1),B(:,:,1));
imwrite(RGB, 'rgb.png');
Note that this does not scale/weight the channels!

5 commentaires

Claudio Esposito
Claudio Esposito le 2 Fév 2022
Thank you both!
Claudio Esposito
Claudio Esposito le 2 Fév 2022
Excuse me sir, I have another issue, which is the following.
I have obtained the following image:
The fact is that if I write : RGB = cat(3,G(:,:,1),R(:,:,1),B(:,:,1)), I obtain this image instead:
How can I fix this problem?
Walter Roberson
Walter Roberson le 2 Fév 2022
Modifié(e) : Walter Roberson le 2 Fév 2022
Why are you writing green then red then blue instead of red then green then blue?
Claudio Esposito
Claudio Esposito le 2 Fév 2022
I was just trying what would have happen because R-G-B seems too reddish purple in colour.
Stephen23
Stephen23 le 2 Fév 2022
"I was just trying what would have happen because R-G-B seems too reddish purple in colour."
You might need to consider this:

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 2 Fév 2022

0 votes

imwrite(RGB, 'rgb.png');

1 commentaire

Claudio Esposito
Claudio Esposito le 2 Fév 2022
Thank you for your replying.
I already tried that, but Matlab displays this error:
Error using writepng (line 23)
PNG image data must be either MxN or MxNx3.
Error in imwrite (line 566)
feval(fmt_s.write, data, map, filename, paramPairs{:});
Error in RGB_conversion (line 14)
imwrite(RGB, 'rgb.png')

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by