%{ This is a program to generate the Moire effect of 2 or more patterns, pattern rotation angle and pitch can be changed 12/17/18 by AO for Agira Photonics Adapted from: https://www.mathworks.com/matlabcentral/answers/306128-how-do-i-represent-an-image-and-add-a-moire-pattern https://www.mathworks.com/matlabcentral/answers/155213-i-wish-to-generate-moire-pattern-for-straight-radial-and-circular-patterns-how-to-write-code and used the lcms function from https://www.mathworks.com/matlabcentral/fileexchange/24670-least-common-multiple-set %} clc clear %% GET INPUTS FROM USER num=input('How many patterns are there? Enter an integer from 2 to 4: '); %set up for 2, 3, or 4 overlaid patterns if num > 4 %checking for incorrect entry input('Oops, can''t do that'); end if num < 1 input('Oops, can''t do that'); %checking for incorrect entry end if num==2 pitch1=input('Enter pattern 1 pitch: '); pitch2=input('Keeping your units consistent, enter pattern 2 pitch: '); angle1=input('Enter angle of rotation between patterns: '); m=lcms([pitch1,pitch2]); %m is the least common multiple, to be used later pitch3=0; %defining variables but setting equal to zero for setup for rest of program pitch4=0; angle2=0; angle3=0; if m < 200 %image comes out pixelated and small if the lcm is less than approx 200 while m < 200 %multiply m by 2 until greater than 200 m=2*m; end end end if num==3 %if user input "3" as number of patterns pitch1=input('Enter pattern 1 pitch: '); pitch2=input('Keeping your units consistent, enter pattern 2 pitch: '); angle1=input('Enter angle of rotation between patterns 1 and 2: '); pitch3=input('Keeping your units consistent, enter pattern 3 pitch: '); angle2=input('Enter angle of rotation between patterns 1 and 3: '); %note that pattern 1 is reference of zero degrees m=lcms([pitch1,pitch2,pitch3]); %m is the least common multiple of three patterns pitch4=0; %defining variables but setting equal to zero for setup for rest of program angle3=0; if m < 200 %image comes out pixelated and small if the lcm is less than approx 200 while m < 200 %multiply m by 2 until greater than 200 m=2*m; end end end if num==4 pitch1=input('Enter pattern 1 pitch: '); pitch2=input('Keeping your units consistent, enter pattern 2 pitch: '); angle1=input('Enter angle of rotation between patterns 1 and 2: '); pitch3=input('Keeping your units consistent, enter pattern 3 pitch: '); angle2=input('Enter angle of rotation between patterns 1 and 3: '); pitch4=input('Keeping your units consistent, enter pattern 4 pitch: '); angle3=input('Enter angle of rotation between patterns 1 and 4: '); m=lcms([pitch1,pitch2,pitch3,pitch4]); %m is the least common multiple of the four patterns if m < 200 %image comes out pixelated and small if the lcm is less than approx 200 while m < 200 %multiply m by 2 until greater than 200 m=2*m; end end end %% SET UP VECTOR SPACE rowVector1= (1:m); %single row of m columns period1 = pitch1; %setting up cosine function with same period as pitch 1 cosVector1=(1+cos(2*pi*rowVector1/period1)); Pattern1 = repmat(cosVector1,m, 1); %repmat is repeating pattern of the cosine function values for a total matrix of size m x m rowVector2= (1:m); period2 = pitch2; %setting up cosine function with same period as pitch 2 cosVector2=(1+cos(2*pi*rowVector2/period2)); Pattern2 = repmat(cosVector2, m, 1); rowVector3= (1:m); period3 = pitch3; %setting up cosine function with same period as pitch 3 cosVector3=(1+cos(2*pi*rowVector3/period3)); Pattern3 = repmat(cosVector3, m, 1); rowVector4= (1:m); period4 = pitch4; %setting up cosine function with same period as pitch 4 cosVector4=(1+cos(2*pi*rowVector4/period4)); Pattern4 = repmat(cosVector4, m, 1); %% CONVERT VALUES INTO GRAYSCALE IMAGE subplot(2,2,1); %puts image in top left corner of window imshow(Pattern1, []) title('PATTERN 1'); Rotate2=imrotate(Pattern2,angle1,'crop'); %rotate pattern 2 by angle between patterns 1 and 2, crop to fit display subplot(2,2,2); %puts image in top right of window imshow(Rotate2, []) title('PATTERN 2'); if num==3 %if there are three patterns, third one goes in bottom left corner of window Rotate3=imrotate(Pattern3,angle2,'crop'); %rotate pattern 3 by angle between patterns 1 and 3, crop to fit display subplot(2,2,3); imshow(Rotate3,[]) title('PATTERN 3'); end if num==4 %if there are four patterns Rotate3=imrotate(Pattern3,angle2,'crop'); %rotate pattern 3 by angle between patterns 1 and 3, crop to fit display subplot(2,2,3); %third one goes in bottom left corner of window imshow(Rotate3,[]) title('PATTERN 3'); subplot(2,2,4); %fourth one goes in bottom right corner of window Rotate4=imrotate(Pattern4,angle3,'crop'); %rotate pattern 4 by angle between patterns 1 and 4, crop to fit display imshow(Rotate4,[]) title('PATTERN 4'); end %% MULTIPLY FOR MOIRE IMAGE figure('Name','Moire Pattern'); %creating and titling new figure if num==2 %for two patterns, only need to multiply pattern 1 and pattern 2 moire= Pattern1 .* double(Rotate2); imshow(moire,[]); end if num==3 moire1=Pattern1.*double(Rotate2); moire2=moire1.*double(Rotate3); imshow(moire2,[]); %multiply the three patterns together and show as single image end if num==4 moire1=Pattern1.*double(Rotate2); moire2=moire1.*double(Rotate3); moire3=moire2.*double(Rotate4); imshow(moire3,[]); %multiply the four patterns together and show as single image end