# Create a color image in Matlab with sinusoidal patterns

I want to create a color image in Matlab whose components have sinusoidal patterns (okay even if they have different frequencies).

To create a black and white image I did:

fs = 0.08;
W = 256;
H = 128;

img1 = zeros(128, 256);
for m = 0 : H-1
for n = 0 : W-1
img1(m + 1, n + 1) = sin(2*pi*fs*m);
end
end

colormap(gray(256));
imshow(img1);
title('img1');

• SE.DSP wishes you a happy new year 2017, with a kind reminder that your question and its answers may require some action (votes, acceptance, etc.) – Laurent Duval Dec 31 '16 at 17:07

Simply do it for each color channel, I mean for Red,Green and blue, for each form a separate matrix and then concatenate those, to form a 3D color image.

fs = 0.08;   //FOR RED CHANNEL
W = 256;
H = 128;

img1 = zeros(128, 256);
for m = 0 : H-1
for n = 0 : W-1
img1(m + 1, n + 1) = sin(2*pi*fs*m);
end
end

fs = 0.16;    //FOR Green CHANNEL

img2 = zeros(128, 256);
for m = 0 : H-1
for n = 0 : W-1
img2(m + 1, n + 1) = sin(2*pi*fs*m);
end
end

fs = 0.01;    //FOR Blue CHANNEL

img3 = zeros(128, 256);
for m = 0 : H-1
for n = 0 : W-1
img3(m + 1, n + 1) = sin(2*pi*fs*m);
end
end

Color=zeros(128,256,3);
Color(:,:,1)=img1;
Color(:,:,2)=img2;
Color(:,:,3)=img3;
imshow(Color,[]);


It is not necessary to loop over each pixel, as Matlab as matrix capabilities (using meshgrid for instance). This is a version where each channel has a different frequency, angle and phase, and the whole image assigns different weights to each channel.

clear all;close all
W = 256; H = 128;  nChannel = 3;
weightRGB = [3,2,6];weightRGB = weightRGB/sum(weightRGB);
angleRGB = 2*pi*[1/7,1/6,-1/13];
freqRGB = [1/12,1/8,7/33];
phaseRGB = 2*pi*rand(1,3);
labelRGB = {'Red','Green','Blue'};
Xx = [0:W-1];
Yy = [0:H-1];
[X,Y] = meshgrid(Xx,Yy);

img = zeros(H, W,nChannel);
for indexRGB = 1:nChannel
img(:,:,indexRGB) = weightRGB(indexRGB)*(sin(2*pi*freqRGB(indexRGB).*.((X.*cos(angleRGB(indexRGB) )+ Y.*sin(angleRGB(indexRGB))))+phaseRGB(indexRGB))+1)/2;
end
figure(1);
for indexRGB = 1:nChannel
subplot(2,2,indexRGB)
imagesc(img(:,:,indexRGB));colormap gray
xlabel(labelRGB(indexRGB))
end
subplot(2,2,4)
imagesc(img);
xlabel('Color image')


This is what I use in lectures to illustrate Fourier interpretation of oriented textures. The result here is: