├── README.md ├── angular_spectrum.m ├── angular_spectrumnew.m ├── multiam.m ├── multicom.m ├── multihide.m ├── p1.bmp ├── p2.bmp ├── p3.bmp ├── p4.bmp ├── p5.bmp ├── p6.bmp ├── p7.bmp └── p8.bmp /README.md: -------------------------------------------------------------------------------- 1 | # This code is for paper "Multiple-image encryption and hiding with an optical diffractive neural network" 2 | -------------------------------------------------------------------------------- /angular_spectrum.m: -------------------------------------------------------------------------------- 1 | function img= angular_spectrum( dx, r, obj, z ) 2 | %angular spectrum propagation by circular convolution 3 | %dx:pixel size r:wavelength obj:field before propagation; 4 | %z:propagation distance; img:field after propagation 5 | dy = dx; 6 | du = dx; 7 | [nn,mm] = size(abs(obj)); 8 | 9 | dfx = 1/(dx*nn); 10 | dfy = 1/(dy*mm); 11 | 12 | % % Q operator with dfx, dfy at frequency plane 13 | pha = zeros(nn,mm); 14 | for ii = 1:nn 15 | for jj = 1:mm 16 | pha(ii,jj) = dfx^2*(ii-nn/2-0.5)^2 + dfy^2*(jj-mm/2-0.5)^2; % fx^2 + fy^2 17 | end 18 | end 19 | 20 | % pha = e_pha_dfx; 21 | e_pha = exp(1i*2*pi*z/r.*sqrt(1-r^2.*pha)); 22 | 23 | %shibu=real(e_pha); 24 | %figure; imshow(abs(shibu)); 25 | 26 | tmp = fftshift(fft2(fftshift(obj))); 27 | tmp = tmp.*e_pha; 28 | img = fftshift(ifft2(fftshift(tmp))); 29 | %img=img/(4*mm*nn); 30 | 31 | return -------------------------------------------------------------------------------- /angular_spectrumnew.m: -------------------------------------------------------------------------------- 1 | function img= angular_spectrumnew( dx, r, obj, z ) 2 | %angular spectrum propagation by linear convolution 3 | %dx:pixel size r:wavelength obj:field before propagation; 4 | %z:propagation distance; img:field after propagation 5 | 6 | dy = dx; 7 | du = dx; 8 | [nn,mm] = size(abs(obj)); 9 | 10 | dfx = 1/(2*dx*nn); 11 | dfy = 1/(2*dy*mm); 12 | 13 | 14 | % % Q operator with dfx, dfy at frequency plane 15 | pha = zeros(2*nn,2*mm); 16 | for ii = 1:2*nn 17 | for jj = 1:2*mm 18 | pha(ii,jj) = dfx^2*(ii-nn-0.5)^2 + dfy^2*(jj-mm-0.5)^2; % fx^2 + fy^2 19 | end 20 | end 21 | 22 | % pha = e_pha_dfx; 23 | e_pha = exp(1i*2*pi*z/r.*sqrt(1-r^2.*pha)); 24 | 25 | objnew=zeros(2*nn,2*mm); 26 | objnew(round(nn/2.0+1):round(3.0*nn/2),round(mm/2.0+1):round(3.0*mm/2))=obj; %zero padding 27 | 28 | tmp = fftshift(fft2(fftshift(objnew))); 29 | tmp = tmp.*e_pha; 30 | imgnew = fftshift(ifft2(fftshift(tmp))); 31 | img=imgnew(round(nn/2.0+1):round(3.0*nn/2),round(mm/2.0+1):round(3.0*mm/2));%crop the center part 32 | return -------------------------------------------------------------------------------- /multiam.m: -------------------------------------------------------------------------------- 1 | %This code is a simualtion for a multiplexed optical image security system 2 | %with cascaded phase-only masks. Four pairs of input host images and output 3 | %hidden images are tested. The masks are amplitude-only. 4 | clear; 5 | N=5; %N-1: number of cascaded amplitude-only masks (L=N-1); the last mask is conjugate(P) 6 | size1=512; 7 | size2=512; 8 | size3=4; %Number of input-output image pairs for image hiding 9 | 10 | inputall=zeros(size1,size2,size3); 11 | %Four input host images 12 | inputall(:,:,1)=im2double(imread('p1.bmp')); 13 | inputall(:,:,2)=im2double(imread('p2.bmp')); 14 | inputall(:,:,3)=im2double(imread('p3.bmp')); 15 | inputall(:,:,4)=im2double(imread('p4.bmp')); 16 | 17 | %paramaters in simulating the Fresnel field propagation 18 | dist=0.05;%distance between neighboring amplitude-only masks 19 | lamda=532e-9;%wavelength 20 | psize=20e-6;%pixel size 21 | 22 | ampmask=ones(size1,size2,N);%initial values for the cascaded amplitude-only masks 23 | 24 | targetall=zeros(size1,size2,size3); 25 | %target output results (hidden image displayed in a sub-window in the output imaging plane) 26 | 27 | targetall(129:256,129:256,1)=imresize(im2double(imread('p5.bmp')),[128 128]); 28 | targetall(129:256,257:384,2)=imresize(im2double(imread('p6.bmp')),[128 128]); 29 | targetall(257:384,129:256,3)=imresize(im2double(imread('p7.bmp')),[128 128]); 30 | targetall(257:384,257:384,4)=imresize(im2double(imread('p8.bmp')),[128 128]); 31 | 32 | %display the target output results 33 | imwrite(targetall(:,:,1),'target1.bmp','bmp'); 34 | imwrite(targetall(:,:,2),'target2.bmp','bmp'); 35 | imwrite(targetall(:,:,3),'target3.bmp','bmp'); 36 | imwrite(targetall(:,:,4),'target4.bmp','bmp'); 37 | temp=targetall(:,:,1)+targetall(:,:,2)+targetall(:,:,3)+targetall(:,:,4); 38 | imwrite(temp,'targetall.bmp','bmp'); 39 | reference=temp; 40 | 41 | 42 | %Each input host image is multiplied with a random phase mask 43 | for ii=1:size3 44 | inputall(:,:,ii)=inputall(:,:,ii).*exp(1i*2*pi*rand(size1,size2)); 45 | end 46 | 47 | %Wavefront-matching algorithm for designing the amplitude-only masks 48 | for iter=1:30 %number of iterations in the optimization 49 | iter 50 | for ii=1:N %1:N 51 | summation1=0; 52 | summation2=0; 53 | for mm=1:size3 54 | inputpat=inputall(:,:,mm); 55 | temp1=inputpat; 56 | temp1=angular_spectrum(psize,lamda,temp1,dist); %circular convolution 57 | %diffractive field propagation with angular spectrum method (dist can be positive (forward) or negative (backward)) 58 | if ii>1 59 | for kk=1:(ii-1) 60 | temp1=temp1.*ampmask(:,:,kk); 61 | temp1=angular_spectrum(psize,lamda,temp1,dist);%circular convolution 62 | end 63 | end 64 | 65 | outputpat=targetall(:,:,mm); 66 | temp2=outputpat; 67 | 68 | if ii1 59 | for kk=1:(ii-1) 60 | temp1=temp1.*commask(:,:,kk); 61 | temp1=angular_spectrum(psize,lamda,temp1,dist);%circular convolution 62 | end 63 | end 64 | 65 | outputpat=targetall(:,:,mm); 66 | temp2=outputpat; 67 | 68 | if ii1 57 | for kk=1:(ii-1) 58 | temp1=temp1.*phasemask(:,:,kk); 59 | temp1=angular_spectrum(psize,lamda,temp1,dist);%circular convolution 60 | end 61 | end 62 | 63 | outputpat=targetall(:,:,mm); 64 | temp2=outputpat; 65 | 66 | if ii