How to create an RGB image?

865 views (last 30 days)
Zeff020 on 26 May 2017
Answered: DGM on 23 Apr 2022
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?
  1 Comment
Walter Roberson
Walter Roberson on 26 May 2017
I am not clear as to what the desired inputs would be, or the desired outputs?

Sign in to comment.

Answers (4)

MathReallyWorks on 26 May 2017
Edited: MathReallyWorks on 26 May 2017
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
Walter Roberson on 26 May 2017
Looking at 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.

Sign in to comment.

Tariq Hussain
Tariq Hussain on 25 Jun 2019

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:

Community Treasure Hunt

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

Start Hunting!