
%% Create a single particle trajectory 
D=0.1;          %(micron^2/s)
deltat=.05;     %TimeStep (s)
pixelsize=.1;   %(micron)
sz=128;
T=200;          %Time Frames
Dim=2;          %Number of Dimensions

%This creates a trajectory 
displacements=sqrt(2*D*deltat)*randn(T,2);
x=cumsum(displacements(:,1));
y=cumsum(displacements(:,2));

figure  %This opens a new, blank figure 
plot(x,y,'r','linewidth',2)
xlabel('X (micron)','fontsize',14)
ylabel('Y (micron)','fontsize',14)
axis equal

%% Saving a Figure

%Lets save this figure
%one way:
print(gcf,'-r0','-dpng','DifFig.png')
%Note 'gcf' means 'get current figure'). 

%Figures can also referenced by a figure handle
h=figure  %This opens a new, blank figure 
a=plot(x,y,'g','linewidth',2)
xlabel('X (micron)','fontsize',14)
ylabel('Y (micron)','fontsize',14)
axis([-2 2 -4 4])
print(h,'-r0','-dpng','DifFig_h.png')
set(gca,'fontsize',12)
%Note 'gca' means 'get current axes'). 

%another way
saveas(h,'DifFig_Saveas','fig')
saveas(h,'DifFig_Saveas','png')

%Make a specific figure current
figure(h)

%% Make a SPT type simulated data set and save various ways...

lambda=.600;    %micron
NA=1.4;
d=deltaim([sz sz])
sigma=0.25*lambda/NA/pixelsize;      %pixels
Nphotons=200;
bg=2;
centerim=Nphotons*gaussf(d,sigma)

%make an empty dipimage image
series=newim([sz sz T]);

%use the shift function to shift 'centerim'
for ii=0:T-1
    series(:,:,ii)=shift(centerim,x(ii+1),y(ii+1));
end
%add background
series=series+bg;

%corrupt with 'Poisson' noise (photon shot noise)
nseries=noise(series,'poisson')

%save image from display using 'writedisplayavi' with global scaling

%This performs the same function:
nseries_uint8=dip_image(stretch(nseries),'uint8')
writeavi(nseries_uint8,'nseries',round(1/deltat),'none')

%printing a dipimage figure window
h_di=1
dipshow(h_di,nseries)% show the image in figure 'h_di'
diptruesize(h_di,'tight'); %makes data pixels match screen pixels
print(h_di,'-dpng','-r0','dip_figureout.png')

%saving a dipimage figure using dipimage functions
nseries_uint8_1=squeeze(nseries_uint8(:,:,0))
%'squeeze' removes extra dimentions
%Note the output must be 8-bit
writeim(nseries_uint8_1,'dip_Frameout.jpg','jpeg') 

%save using matlab type variable

%this saves and loads all variables
save('nseries_save')
clear 
whos
load('nseries_save')
whos

%this saves and loads only indicated variables
save('nseries_save','nseries','lambda')

%this load will overwrite 'nseries' and 'lambda'
load('nseries_save')

%this load won't overwrite 'nseries' and 'lambda'
in=load('nseries_save')
in
nseries2=in.nseries


%% Saving a movie with text

fig=dipshow(nseries)

diptruesize(fig,100)
set(fig,'DoubleBuffer','on');
aviobj = avifile('MovieWwithText.avi','fps',round(1/deltat),'compression','none')
for tt=0:size(nseries,3)-1
    frm=nseries(:,:,tt);
    dipshow(fig,frm)
    diptruesize(fig,'tight')
    s=sprintf('%g',tt)
    text(5,24,s,'color',[1 1 1])
    frame = getframe(gcf);
    aviobj = addframe(aviobj,frame);
end
aviobj = close(aviobj);
close(fig)


%% Making a movie using a matlab figure

h=figure  %This opens a new, blank figure 
plot(x,y,'r','linewidth',2)
xlabel('X (micron)','fontsize',14)
ylabel('Y (micron)','fontsize',14)

set(h,'DoubleBuffer','on');
aviobj = avifile('MATLABfigureMovie.avi','fps',round(1/deltat),'compression','none')
set(gca,'nextplot','replace','Visible','off')
for ii=1:T
    figure(h)
    plot(x(1:ii),y(1:ii),'linewidth',2)
    hold on
    plot(x(ii),y(ii),'rO','linewidth',3)
    axis equal
    axis([-3 3 -3 3])
    xlabel('X (micron)','fontsize',14)
    ylabel('Y (micron)','fontsize',14)
    frame = getframe(gcf);
    aviobj = addframe(aviobj,frame);
    %pause(.1)
    hold off
end
aviobj = close(aviobj);

%% Color images in Dipimage

red=shift(gaussf(deltaim,10),[20,20])
green=shift(gaussf(deltaim,10),[-20,-20])
blue=gaussf(deltaim,10)

co=joinchannels('RGB',red,green,blue)

size(co)

%get a color channel

r=co{1}     %Note bracket style

%make an empty colorimage
temp=newim;
cempty=joinchannels('RGB',temp,temp,temp)

%write into a color image
cempty{1}(:,:)=red












