How to create rgb image in matlab?

378 views (last 30 days)
How to create rgb image in matlab?

Accepted Answer

Michael Haderlein
Michael Haderlein on 4 Feb 2015
Edited: Michael Haderlein on 4 Feb 2015
Do you want to read or to create an image?
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)
Daniel Daza
Daniel Daza on 10 Mar 2018
Actually, sometimes not specifying the type can lead to odd results. If you create a matrix with zeros, compute something with it and attempt to plot the results, imshow will probably show something not expected. Even if the results of the computation are fine, this might lead to think that there is a problem in them when actually it's because of the way imshow handles images with different data types. So I would recommend being explicit about image data types whenever it's possible.

Sign in to comment.

More Answers (1)

DGM on 23 Apr 2022
I figure if hundreds of people come here every month looking for the answer to an incredibly vague question, it's about time someone took the question seriously and gave a comically-broad (but hopefully instructive) answer.
So how does one create an RGB image in MATLAB? Well, Michael probably already nailed the core of it (at least well enough for OP's needs). That is, one simply creates an array with the desired color values. It's just a MxNx3 numeric array, after all. Of course, one still has to pay attention to class and data range, but other than that, it can be rather simple.
So let's start simple. Creating uniform color fields is rather simple. You don't have to get stuck trying to break away from primary-secondary color combinations. Just pick a color tuple and assemble it into an image.
% uniform color fields with class specification
s0 = [50 50];
cp1 = repmat(permute(uint8([57 177 242]),[1 3 2]),s0); % uint8
cp2 = repmat(permute([57 177 242]/255,[1 3 2]),s0); % double
Of course, you can do grid-aligned linear gradients by replication as well. You can combine uniform fields and gradients any way you want.
%% any combination of orthogonal linear gradients and solid fields
s0 = [200 200];
gpx = repmat(linspace(0,1,s0(2)),[s0(1) 1]);
gpy = repmat(linspace(0,1,s0(1)).',[1 s0(2)]);
zp = zeros(s0);
op = ones(s0);
grp1 = cat(3,gpx,gpy,op);
grp2 = cat(3,op,gpx,gpy);
grp3 = cat(3,gpx,zp,gpy);
grp4 = cat(3,gpx*0.7,zp+0.2,gpy*0.7);
imshow([grp1 grp2; grp3 grp4]) % display all examples
MATLAB and the File Exchange have plenty of colormap generators to offer. You could use one of those just the same.
%% any ortho gradient defined by a colormap
s0 = [50 50];
gpcm1 = repmat(permute(jet(s0(2)),[3 1 2]),[s0(1) 1]);
gpcm2 = repmat(permute(parula(s0(1)),[1 3 2]),[1 s0(2)]);
imshow(iminv([gpcm1 gpcm2])) % display all examples
If all you need is any RGB image, why can't it be random? You don't have to settle for lumpy white noise either!
%% RGB noise (white)
s0 = [200 200];
rnw = rand([s0 3]);
% RGB noise (blue)
rnb = rand([s0 3]);
maskdiff = mat2gray(rnb-imgaussfilt(rnb,2));
for c = 1:3
rnb(:,:,c) = adapthisteq(maskdiff(:,:,c));
imshow([rnw rnb]) % display all examples
Maybe you appreciate randomness in smaller doses.
%% sparse random pixels and lines of random color
s0 = [200 200];
thresh = 0.1;
rp1 = rand([s0 3]) .* (rand(s0)<thresh);
rl1 = repmat(rand(s0(1),1,3) .* (rand(s0(1),1)<thresh),[1 s0(2)]);
rl2 = repmat(rand(1,s0(2),3) .* (rand(1,s0(2))<thresh),[s0(1) 1]);
imshow([rp1 rl1 rl2]) % display all examples
A simple filter can make things more interesting.
%% random spots of random color
s0 = [200 200];
thresh = 0.01;
rp0 = rand([s0 3]) .* (rand(s0)<thresh);
rs1 = imdilate(rp0,strel('disk',7,0));
rs2 = imdilate(rp0,strel('diamond',7));
rs3 = imdilate(rp0,strel('rectangle',[3 5]));
rs4 = imdilate(rp0,strel('line',7,60));
imshow([rs1 rs2; rs3 rs4]) % display all examples
Of course, composition opens up just as many opportunities as simple filters do.
%% combine any of the above with basic composition methods
s0 = [200 200];
imshow(grp1) % set up axes to create ROI
R = images.roi.Polygon(gca) % create ROI to create mask
R.Position = [45 66;112 30;178 40;138 94;187 116;104 187;
36 170;92 151;23 119;102 68;21 91;39 27];
hardmask = createMask(R);
softmask = imgaussfilt(double(hardmask),5);
imshow([hardmask softmask])
% logical composition
comp1 = grp1;
comp1(repmat(hardmask,[1 1 3])) = grp2(repmat(hardmask,[1 1 3]));
% uncorrected linear composition (sRGB)
comp2 = grp1.*(1-softmask) + grp2.*softmask;
% linear composition (linear RGB)
comp3 = lin2rgb(rgb2lin(grp1).*(1-softmask) + rgb2lin(grp2).*softmask);
imshow([comp1 comp2 comp3]) % display all examples
So there you have a handful of ways to create RGB images within the scope of MATLAB and IPT. None of the above operations are terribly complicated, though there are FEX tools which do offer some conveniences. MIMT was built originally for creating and processing abstract image content, so it may be aptly suited to creating demonstrably indescribable "RGB images".
Let's start again with uniform color fields. This is a bit more succinct.
% solid color fields with class specification
s0 = [50 50];
cp3 = colorpict([s0 3],[138 20 255],'uint8');
cp4 = colorpict([s0 3],[0.54 0.08 1],'double');
And how about some slightly more complicated random stuff?
%% random lines/walks
s0 = [200 200];
mrl1 = randlines(s0,2,'mode','normal','sparsity',0.5);
mrl2 = imlnc(randlines(s0,2,'mode','walks','sparsity',0.5));
mrl3 = imlnc(randlines(s0,2,'mode','walks','sparsity',0.95));
imshow([mrl1 mrl2 mrl3])
%% pseudo-perlin noise
s0 = [200 200];
pn1 = perlin([s0 3],'correl',0); % uncorrelated channels (more colorful)
pn2 = perlin([s0 3],'correl',0.5);
pn3 = perlin([s0 3],'correl',2); % increasing correlation (more gray)
imshow([pn1 pn2 pn3])
Grid-aligned gradients were easy to make with replication, but that's really not very flexible.
%% 2-point linear and radial gradients
s0 = [100 100];
a = [0 255 0];
b = [255 0 0];
% lingrad()/radgrad() support multiple ease curve options
lg1 = lingrad([s0 3],[0 0; 1 1],[a; b],'linear','srgb');
lg2 = lingrad([s0 3],[0 0; 1 1],[a; b],'cosine','srgb');
lg3 = lingrad([s0 3],[0 0; 1 1],[a; b],'linear','linrgb');
lg4 = lingrad([s0 3],[0 0; 1 1],[a; b],'cosine','linrgb');
rg1 = radgrad([s0 3],[0.5 0.5],0.5,[a; b],'linear','srgb');
rg2 = radgrad([s0 3],[0.5 0.5],0.5,[a; b],'cosine','srgb');
rg3 = radgrad([s0 3],[0.5 0.5],0.5,[a; b],'linear','linrgb');
rg4 = radgrad([s0 3],[0.5 0.5],0.5,[a; b],'cosine','linrgb');
imshow([lg1 lg2 lg3 lg4; rg1 rg2 rg3 rg4])
%% multipoint gradients
colors = lines(7)*255; % pick some colors
lgmp1 = lingrad([s0 3],[0 0; 1 1],colors,'linear','srgb');
rgmp1 = radgrad([s0 3],[0.5 0.5],0.5,colors,'linear','srgb');
imshow([lgmp1 rgmp1])
Composition is made much easier with purpose-built tools. Class handling isn't a problem anymore.
%% compositions, but using MIMT tools instead
s0 = [200 200];
cbmask = freecb(s0,20); % checkerboard mask
pnmask = perlin(s0); % perlin noise mask
comp1 = replacepixels(grp2,grp1,cbmask); % binary mask
comp2 = replacepixels(grp2,grp1,pnmask,'linear'); % grayscale mask
comp3 = replacepixels(grp2,grp1,rs2,'linear'); % mask can be RGB too!
imshow([comp1 comp2 comp3])
Of course, if composition can be part of "creating" the image, then why can't blending be part of the process?
%% blending is an option too
s0 = [200 200];
% imblend() supports blend modes from GIMP/Krita/PS/etc
blend1 = imblend(rs2,grp1,1,'softlight');
blend2 = imblend(rs2,grp1,1,'grainmerge');
blend3 = imblend(rs2,grp1,1,'darkenrgb');
imshow([blend1 blend2 blend3])
I think you get the point.
If instead of creating an RGB image from scratch, you want to convert an existing grayscale image to a color image, this question may be of help:

Community Treasure Hunt

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

Start Hunting!