How to create an RGB image?
447 views (last 30 days)
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?
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(:,1:100,1)=0.5; %Red (dark red)
image(:,101:200,1)=1; %Red (maximum value)
source: Michael Haderlein's Answer
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.
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
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: