function [fitresult, gof] = OneForThreev2(bg,guess,filename) %input a guess for the spatial frequency of the pattern in 1/pixel and a file name close all; %STC1 = videoinput('winvideo'); Two adaptors are %available, choose the one that is applicable based on the matlab version; %for 2014b, use stimaqadaptor, winvideo for 2013 STC1 = videoinput('stimaqadaptor'); img = getsnapshot(STC1); %gets the image from camera Mean = mean(img(:)) Min = min(img(:)) %img = img(:,:)-min(min(img)); img = img(:,:)-bg; %dark current subtraction %img2 = uint8(zeros(1200,1600)); %this func. gives a %perfect pattern for analysis. %for i=1:1600 % img2(:,i) = 255*((sin(i*pi/100))^2); %end %for i=1:1200 % img(i,:) = img(i,:)*(sin(i*pi/100))^2; %end %max(img2(:)); Max = max(img(:)) Mean = mean(img(:)) %max and mean pixel brightness values Min = min(img(:)) imgscb = img; %for i=200:398 %Use this func. if you want a scalebar on %saved image (50um with standard lens) % imgscb(980:1000,i) = 255; %end scrsz = get(0,'ScreenSize'); figure(1), imshow(img); %Gives a figure of the image with scalebar hold on; plot([200;398],[1000;1000], '-k', 'LineWidth', 3, 'Color', 'w'); hold off; text(300,1050, '50µm', 'HorizontalAlignment','center','Color','w','FontWeight','bold','FontSize',16) set(1, 'Position', [1 scrsz(4)*2/5 scrsz(3)/2 scrsz(4)/2]); imggaus = uint8(zeros(1200,1600)); for i = 1:1600 imggaus(:,i) = img(:,i)*exp(-((i-800)^2)/(2*380^2));%Gaussian windowing end %figure(4), imshow(imggaus) ftrans = fftshift(fft2(imggaus)); %Takes FFT of the image and converts it to be viewable Submatrix = ftrans(401:800,601:1000); %the region of interest doughnut = Submatrix; %Doughnut functions allow the image levels to be set doughnut(195:205,195:205) = 0; %to allow the regions around the central peaks to be seen top = max(abs(doughnut(:))); doughnut(125:275,125:275) = 0; top2 = max(abs(doughnut(:))); ftdisp = mat2gray(abs(Submatrix), [100 top2*1.5]); %sets the image brightness scale figure(2), imshow(ftdisp) %Displays the FFT Filter1 = fspecial('gaussian', 9, 1.5); %Gaussian blurr filter %Temp = imfilter(ftdisp, Filter1); %d = uint16( ftdisp.*2^16./(max(ftdisp(:)))); d = uint32(1000000*(abs(Submatrix)/top)); %Defines new FFT in unit 16 forma. ***CARE HERE*** for levels edg = 20; %Number of edge pixels to be avoided in peak finding max(max(d)) d = conv2(single(d),Filter1,'same') ; %Blurrs the image to prevent peak finding noise sim = size(d); [y,x] = find(d(edg:sim(1)-edg,edg:sim(2)-edg)); %Forms an array of all coordinates to search Point = zeros([100, 3]); %Array to recieve peak values and locations Point(1,1:2) = 201; %picks out the DC peak j = 2; %2 because DC peak is already found x = x+edg-1; %avoiding edges y = y+edg-1; for i = 1:length(x) %Time to find peaks, obsolete methods commented if abs(x(i)-200) <= 5 && abs(y(i)-200) <= 5 continue; %Avoiding DC again elseif d(y(i),x(i)) == max(max(d(y(i)-3:y(i)-1,x(i)-3:x(i)+3))) || d(y(i),x(i)) == max(d(y(i),x(i)-3:x(i)-1)) %Avoiding finding double peaks continue; elseif (d(y(i),x(i))>=21000) && (d(y(i),x(i))) == max(max(d(y(i)-7:y(i)+7,x(i)-7:x(i)+7))) %Peak definition ***IMPORTANT*** Point(j,1:3) = [x(i),y(i),0]; j=j+1; end end for i = 1:100 %Finding the intensity of the peaks starts here if Point(i,1) == 0 %This bit stops this section when no more points are recorded Waypoint = Point(1:i-1,:); %New array to keep it tidy break; end Intensity = 0; Peak = max(max(abs(ftrans(Point(i,2)+395:Point(i,2)+405,Point(i,1)+595:Point(i,1)+605))));%Searches for peak within 10 square pixels for l = 1:10 for m = 1:10 if abs(ftrans(Point(i,2)+395+m,Point(i,1)+595+l)) > Peak*(1/(2.718)^2) %Definition of pixel to be included in intensity calc Intensity = Intensity+(abs(ftrans(Point(i,2)+395+m,Point(i,1)+595+l)))^2; end end end Point(i,3) = Intensity/(10^15); if i == 100 Waypoint = Point(1:i-1,:); end end %Waypoint DC = max(Waypoint(1,3)) %define DC peak value and find fringe pattern peak value Scan=0; for i=2:length(Waypoint) if Waypoint(i,3) >= Scan && ((Waypoint(i,1)-201)^2+(Waypoint(i,2)-201)^2) <= 1225 %OR Waypoint(i,3) >= Scan && Waypoint(i,2) >= 195 && Waypoint(i,2) <= 205 && Waypoint(i,1) >= Scan = 4*Waypoint(i,3); end end Scan/4 %if max(Waypoint(1:ceil((length(Waypoint(:,3))/2)-1),3)) >= max(Waypoint(ceil((length(Waypoint(:,3))/2)):length(Waypoint),3))%If the max val in first half(excluding middle) is greater % PAT = 4*max(Waypoint(ceil(length(Waypoint(:,3))/2):length(Waypoint),3));%Take it %else % PAT = 4*max(Waypoint(1:ceil((length(Waypoint(:,3))/2)-1),3));%if it wasnt greater, take the max of the second half %end Fou = Scan/DC %Normalised value is calculated for i=120:200 ftdisp(375:380,i) = 255; %Scale bars in 20 / 100 um^-1 end for j=200:260 ftdisp(j,20:25) = 255; end text(760,800, '20 (100µm)-1', 'HorizontalAlignment','center','Color','w','FontWeight','bold','FontSize',12) set(2, 'Position',[scrsz(3)/2 scrsz(4)*2/5 scrsz(3)/2 scrsz(4)/2]); hold on plot(Waypoint(:,1),Waypoint(:,2),'*r'); %display peaks hold off line = zeros(1,length(img(1,:))); %Binning fit method begins here for i=1:length(img(1,:)) line(i) = sum(img(:,i))/(length(img(:,1))); %line is a vector containing the average of each row of pixels end [xData, yData] = prepareCurveData( [], line );%prepare the data for the fit ft = fittype( 'a0+a1*sin(x*b1+c1)^2' ); opts = fitoptions( 'Method', 'NonlinearLeastSquares' ); opts.Display = 'Off'; opts.Lower = [0 5 0 0]; opts.StartPoint = [30 200 guess 10]; [fitresult, gof] = fit( xData, yData, ft, opts );%do tha fit Vis = fitresult.a1/(fitresult.a0+fitresult.a1)%calculation of visibility from fit coefficients figure(3), set(3, 'Position', [1 10 scrsz(3)/2 scrsz(4)/2], 'Name', 'Sine Squared Fit');%display the fit axes1 = axes('Parent',3,'GridLineStyle','none','FontSize',14); xlim(axes1,[0 1600]); box(axes1,'on'); grid(axes1,'off'); hold(axes1,'on'); h = plot( fitresult, xData, yData, '--*b'); ylim(axes1,[0 65]); legend( h, 'Original Data', 'a0+a1*sin^2(x*b1+c1)', 'Location', 'NorthEast' ); ylabel( 'Avg. Intensity' ); grid on if exist('filename', 'var') == 1 Names = 'Visibility %f\r\nFourier %f\r\na0 %f\r\na1 %f\r\nb1 %f\r\nc1 %f\r\n';%the rest simply saves the data how I like Peaks = 'Peak x coord y coord Intensity\r\n'; for i=1:length(Waypoint) Peaks = strcat(Peaks, '%f %f %f\r\n'); end Data = [Vis, Fou, fitresult.a0, fitresult.a1, fitresult.b1, fitresult.c1]; %Coords = Coords.';Transposes Coords Wpt = Waypoint.'; FID = fopen(strcat('C:\Matlab\Data\050117\data_', filename, '.txt'), 'w'); fprintf(FID, Names, Data); fprintf(FID, Peaks, Wpt); fclose(FID); imwrite(img,strcat('C:\Matlab\Data\archive\050117\Origin_', filename,'.jpg'),'jpg') save(strcat('C:\Matlab\Data\archive\050117\BaseFT_', filename,'.txt'),'ftrans','-ascii') imwrite(imgscb,strcat('C:\Matlab\Data\050117\Img_', filename,'.jpg'),'jpg') imwrite(ftdisp,strcat('C:\Matlab\Data\050117\FT_', filename,'.jpg'),'jpg') print('-f3',strcat('C:\Matlab\Data\050117\Fit_', filename),'-djpeg','-r0') else end