-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_file_loop_new.m
More file actions
64 lines (53 loc) · 2.71 KB
/
Copy pathread_file_loop_new.m
File metadata and controls
64 lines (53 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
% For real-time alignment of MLA, used in coupled with the Miniscope DAQ software.
% Updated 01-16-2024.
clear
%%
indir = 'E:\Data\2024_01_19\15_50_10\MiniLFMv2';
PeakDist = 18; % MLA lenslet pitch (40um / 2.2um = 18px)
px_pitch = 2.2; % [um]
im_size = [1944,2592]; % image/sensor size[px]
% initialize output variable
peak_pos_x = [PeakDist:PeakDist:im_size(2)];
peak_pos_y = [PeakDist:PeakDist:im_size(1)]';
peakdata.pos(:,1) = repmat(peak_pos_y,length(peak_pos_x),1);
peakdata.pos(:,2) = repelem(peak_pos_x,length(peak_pos_y));
peakdata.XWidth = ones(size(peakdata.pos,1),1);
%%
s = scatter(peakdata.pos(:,2), peakdata.pos(:,1), 5, peakdata.XWidth * px_pitch, 'filled'); % initialize figure
set(gcf,'position',[100,250,im_size(2)/3,im_size(1)/3]);
xlim([0,im_size(2)]);
ylim([0,im_size(1)]);
colorbar;
clim([0,8]);
box on;
while true
% Find the latest image
filelist = dir([indir filesep '*.avi']);
idx = find([filelist.datenum] == max([filelist.datenum]),1,'last');
latestfile = fullfile(indir, filelist(idx).name);
im_file = fullfile(indir, 'current_frame.tiff');
% Read the last frame and convert to Tiff
disp(['Convert and open: ' latestfile]);
system(['C:\vazirilab\ffmpeg\bin\ffmpeg -hide_banner -loglevel error -y -i ' latestfile ' ' im_file]); % Use ffmpeg to convert avi to tiff file.
frame = imread(im_file);
try
peakdata = FastPeakFind_new(frame); % Find peaks and calculate peak widths
% 2-px wide 2200-px long horizontal line to determine the alignment with maximum peak number.
[Lroi_y,Lroi_x] = find(peakdata.idx(im_size(1)/2-PeakDist:im_size(1)/2+PeakDist, 100:100+2*PeakDist));
LP_y = Lroi_y(2)+im_size(1)/2-PeakDist-1; % convert to original image coordinate
LP_x = Lroi_x(2)+100-1;
aline = sum(peakdata.idx(LP_y:LP_y+1, LP_x:LP_x+2200));
%plot([LP_x,LP_x+2200], [LP_y,LP_y],'r-', 'Marker','*','MarkerSize',12,'LineWidth',2);
% Update image in real time
set(s,'XData',peakdata.pos(:,2),'YData',peakdata.pos(:,1),'CData',peakdata.XWidth * px_pitch);
drawnow
title({[filelist(idx).name ', perfect alignment has 121 peaks, currently ' sprintf('%d',sum(aline))],...
['Peak value : ' sprintf('Avg = %0.2f',mean(peakdata.int)), ...
sprintf(' STD = %0.2f',std(single(peakdata.int)))], ['Peak width : ' ...
sprintf('Avg = %0.2f',mean(peakdata.XWidth*px_pitch)), ...
sprintf(' STD = %0.2f',std(peakdata.XWidth*px_pitch)) '[um]']});
catch ME
disp(ME);
title({[filelist(idx).name ', an error occurs in findpeaks']});
end
end