# How to create an RGB image?

447 views (last 30 days)
Zeff020 on 26 May 2017
Edited: DGM on 20 Feb 2023
What I have done thus far to create RGB images is to just load a white image and seperate the red green and blue components from this image and combine them in different ways to get different colors. No this feels kind of redundant because I keep thinking that MatLab must have a built-in RGB system in which you can just write a one line code from which the output will be any of the colors within RGB. Is there a simple function for this?
Walter Roberson on 26 May 2017
I am not clear as to what the desired inputs would be, or the desired outputs?

MathReallyWorks on 26 May 2017
Edited: MathReallyWorks on 26 May 2017
Hello,
As far as I know, there is no direct function to do this. But if you want you can create a user defined function to randomly select any of the colors within RGB.
If you want to create RGB image from some kind of matrix, try this:
image=zeros(300,400,3); %initialize
image(:,1:100,1)=0.5; %Red (dark red)
image(:,101:200,1)=1; %Red (maximum value)
image(1:100,:,2)=rand(100,400); %Green
figure, imshow(image)

Jan on 26 May 2017
No, there is no such command. Accessing the channels is so easy, that it is not worth to create a dedicated function:
RGB = rand(64, 48, 3); % RGB Image
R = RGB(:, :, 1);
G = RGB(:, :, 2);
B = RGB(:, :, 3);
IMG1 = cat(3, G, R, B);
Now the channels swapped.
Walter Roberson on 26 May 2017
Looking at https://www.mathworks.com/matlabcentral/answers/342073-is-there-a-color-database-existent perhaps the request is for the ability to input a color name and get an rgb value out.
Or perhaps the request is just for something like
swath = @(r,g,b,rows,cols) = cat(3, r * ones(rows, cols), g * ones(rows, cols), b * ones(rows, cols) )
or equivalently
swath = @(r,g,b,rows,cols) = repmat( cat(3,r,g,b), rows, cols, 1)
Either of these would produce a rows x cols matrix of solid color with components r, g, and b.

Tariq Hussain on 25 Jun 2019
Edited: DGM on 20 Feb 2023
r=zeros(400);
g=r;
b=r;
r(1:200,1:200)=255;
g(1:200,201:end)=255;
b(201:400,1:200)=255;
r(201:end,201:end)=255;
rgbimg=cat(3,r,g,b);
figure,imshow(rgbimg)

DGM on 23 Apr 2022
If all you're after is a uniform colored field, it can be rather simple. You don't have to get stuck trying to intuit your way away from primary-secondary color schemes by channel filling/deletion. Just pick any arbitrary color tuple and replicate it.
% uniform color fields with class specification
s0 = [50 50]; % output image size
% uint8
cp1 = repmat(permute(uint8([57 177 242]),[1 3 2]),s0);
% same thing, but double
cp2 = repmat(permute([57 177 242]/255,[1 3 2]),s0);
Maybe you're not sure how to select the color tuple you want without seeing it. There are system-level and web-based color pickers that you could use. There are plenty on the File Exchange. MIMT has cpicktool() (HSL/LAB picker) and colorpicker() (sample colors from an image). Stephen has some excellent tools that may be of use.
Of course, it's not terribly clear if a solid color is what you're after. If you're trying to colorize an image, this might be useful:
Otherwise, there might be other options in this thread: