How to create an RGB image?

조회 수: 479 (최근 30일)
Zeff020
Zeff020 2017년 5월 26일
편집: DGM 2023년 2월 20일
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
Walter Roberson
Walter Roberson 2017년 5월 26일
I am not clear as to what the desired inputs would be, or the desired outputs?

댓글을 달려면 로그인하십시오.

답변 (4개)

MathReallyWorks
MathReallyWorks 2017년 5월 26일
편집: MathReallyWorks 2017년 5월 26일
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
Jan 2017년 5월 26일
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.
  댓글 수: 3
Jan
Jan 2017년 5월 26일
편집: Jan 2017년 5월 26일
Do you mean:
IMG1 = cat(3, 0.8*G + 0.2*R, 0.3*R + 0.7*B, 0.1*B + 0.2*G + 0.7*R);
Of course you can get the full RGB spektrum.
What exactly is the problem you want to solve? Which color should be produced based on which input?
Walter Roberson
Walter Roberson 2017년 5월 26일
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
Tariq Hussain 2019년 6월 25일
편집: DGM 2023년 2월 20일
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
DGM 2022년 4월 23일
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!

Translated by