Skip to content

Commit

Permalink
Merge pull request #19 from ThFriedrich/peak_finder_2
Browse files Browse the repository at this point in the history
Peak finder 2, compatibility fix, minor improvements
  • Loading branch information
Thomas Friedrich authored Oct 22, 2020
2 parents a324e2f + df8cd03 commit 7502794
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 22 deletions.
31 changes: 17 additions & 14 deletions functions/PeakFinding/tfm_PeakFinder2.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
% License: Open Source under GPLv3
% Contact: sandra.vanaert@uantwerpen.be
%--------------------------------------------------------------------------
global thr d_min sigma xy hpf gr_cm cdat_0
global thr d_min sigma xy hpf gr_cm cdat_0 ver
% Determine Matlab version for compatibility
ver = version('-release');
ver = str2double(ver(1:4));

obs = obj.obs;
[ny,nx] = size(obs);

Expand Down Expand Up @@ -53,7 +57,9 @@
cdat = cdat_0;
cdat(1:32) = sin(linspace(0,pi,32));
imagesc(hpf.par.wb,cdat); axis off; colormap(gr_cm); caxis([0 1]);
hpf.par.wb.Toolbar = [];
if ver >= 2019
hpf.par.wb.Toolbar = [];
end

% Info panel
hpf.help.pan = uipanel('Parent',hpf.par.pan,'units','normalized','Position',[0.62 0.3 0.38-br 0.6],'ShadowColor',[0 0 0],'ForegroundColor',[0 0 0],'HighlightColor',[0.95 0.95 0.95],'BackgroundColor',[0.8 0.8 0.8]);
Expand All @@ -71,21 +77,15 @@
est_lim_s = mean(nx,ny)*0.1;
SliderSi = uicontrol('Parent',hpf.par.pan,'Style','slider','units','normalized','Position',[br 0.38 0.2 0.15],'Min',3,'Max',est_lim_s);
uicontrol('Parent',hpf.par.pan,'Style','text','String','Estimated Radius (px):','units','normalized','Position',[br 0.8 0.2 0.15],'FontSize',10,'HorizontalAlignment','left','BackgroundColor',[0.8 0.8 0.8]);
% uicontrol('Parent',hpf.par.pan,'Style','text','units','normalized','Position',[br 0.27 0.01 0.1],'String',num2str(3,0),'BackgroundColor',[0.8 0.8 0.8],'HorizontalAlignment','right');
% uicontrol('Parent',hpf.par.pan,'Style','text','units','normalized','Position',[0.2 0.27 0.01 0.1],'String',num2str(est_lim_s,1),'BackgroundColor',[0.8 0.8 0.8],'HorizontalAlignment','left');

%Threshold
SliderTh = uicontrol('Parent',hpf.par.pan,'Style','slider','units','normalized','Position',[br+0.2 0.38 0.2 0.15],'Min',0,'Max',1);
uicontrol('Parent',hpf.par.pan,'Style','text','String','Threshold value:','units','normalized','Position',[br+0.2 0.8 0.2 0.15],'FontSize',10,'HorizontalAlignment','left','BackgroundColor',[0.8 0.8 0.8]);
% uicontrol('Parent',hpf.par.pan,'Style','text','units','normalized','Position',[br+0.2 0.27 0.01 0.1],'String','0','BackgroundColor',[0.8 0.8 0.8],'HorizontalAlignment','right');
% uicontrol('Parent',hpf.par.pan,'Style','text','units','normalized','Position',[0.4 0.27 0.01 0.1],'String','1','BackgroundColor',[0.8 0.8 0.8],'HorizontalAlignment','left');

%Peak Distance
est_lim_d = mean(nx,ny)*0.1;
est_lim_d = mean(nx,ny)*0.2;
SliderDm = uicontrol('Parent',hpf.par.pan,'Style','slider','units','normalized','Position',[br+0.4 0.38 0.2 0.15],'Min',0,'Max',est_lim_d);
uicontrol('Parent',hpf.par.pan,'Style','text','String','Minimum Distance (px):','units','normalized','Position',[br+0.4 0.8 0.2 0.15],'FontSize',10,'HorizontalAlignment','left','BackgroundColor',[0.8 0.8 0.8]);
% uicontrol('Parent',hpf.par.pan,'Style','text','units','normalized','Position',[br+0.4 0.27 0.01 0.1],'String','0','BackgroundColor',[0.8 0.8 0.8],'HorizontalAlignment','right');
% uicontrol('Parent',hpf.par.pan,'Style','text','units','normalized','Position',[0.6 0.27 0.01 0.1],'String','1','BackgroundColor',[0.8 0.8 0.8],'HorizontalAlignment','left');

% Textboxes %
%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -236,7 +236,9 @@ function deleteFigure(hObject,~)
function waitbar_out()
hpf.par.wb.Children.CData = cdat_0;
set(hpf.par.wb,'visible','off')
hpf.par.wb.Colormap = gr_cm;
if ver >= 2019
hpf.par.wb.Colormap = gr_cm;
end
drawnow;
end

Expand All @@ -247,17 +249,18 @@ function waitbar_out()
timerDat.n_tick = 1;
timerObject = timer('TimerFcn',@tick,...
'ExecutionMode','fixedRate',...
'Period',0.01,...
'Period',0.05,...
'UserData', timerDat);
start(timerObject);

function tick(timerObj,event)

timerData = get(timerObj, 'UserData');
im = timerData.im_dat;
im = circshift(im,timerData.n_tick,2);
im = circshift(im,timerData.n_tick*5,2);
timerData.axes.Children.CData = im;
timerData.axes.Colormap = gr_cm;
if ver >= 2019
timerData.axes.Colormap = gr_cm;
end
timerData.n_tick = timerData.n_tick + 1;
set(timerObj, 'UserData', timerData);
drawnow
Expand Down
48 changes: 40 additions & 8 deletions functions/PeakFinding/tfm_find_peaks_2d.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
% License: Open Source under GPLv3
% Contact: sandra.vanaert@uantwerpen.be
%--------------------------------------------------------------------------

% Avoide negative values
img = img+min(img(:));
img = img/max(img(:));

% This ratio corresponds roughly to the the visual column radius
sigma = sigma/3;
Expand All @@ -38,6 +39,7 @@

% Apply Wiener filter
d = wiener2(d,[nkr_w nkr_w]);
d = real(d);

% Apply Median filter to compensate salt/pepper-like artefacts
d = medfilt2(d,[nkr_m nkr_m],'symmetric');
Expand All @@ -46,27 +48,28 @@
d = fcn_anscombe_inv(d);

d = max(min(img(:)), d);

% Signal to noise ratio
psnr = (var(d(:))/var(img(:)-d(:)));
alpha = 1/psnr;

% Perform deconvolution with gaussian, according to:
% Van Dyck, D. (2012). Basics of Image Processing and Analysis. p. 74. https://doi.org/10.5281/zenodo.51511
% Van Dyck, D. (2012). Basics of Image Processing and Analysis. p. 74. https://doi.org/10.5281/zenodo.51511
[ny, nx] = size(d);
[gx,gy]=fcn_real_space_2_F_space(nx,ny);
H = fcn_gauss_2d_fourier_space(gx,gy,sigma);
Y = conj(H)./(abs(H).^2+(alpha));

d_deconv = fftshift(ifft2(fft2(ifftshift(d)).*ifftshift(Y)));
d_deconv = fcn_conv2D(d,Y);
d_deconv = real(d_deconv);


se = strel('disk',round(5*sigma));
se = strel('disk',round(4*sigma));
img_o = imtophat(d_deconv,se);

sc = max(img_o(:));
img_o = img_o/sc;

if nargout > 1
xy = fcn_local_max(img_o,pk_min,d_min);
else
Expand Down Expand Up @@ -180,5 +183,34 @@
end
img_o = max(0, a*x.^2+e+ix.*(b+ix.*(c+ix*d)));


end

function im_o = fcn_conv2D(im1, im2)
n_pix_border = 30;

im1_pad = fcn_smooth_borders(im1,n_pix_border);
im2_pad = fcn_smooth_borders(im2,n_pix_border);

im_o = fftshift(ifft2(fft2(ifftshift(im1_pad)).*ifftshift(im2_pad)));
im_o = im_o(n_pix_border+1:end-n_pix_border,n_pix_border+1:end-n_pix_border);

end


function im_o = fcn_smooth_borders(img,n_pix_border)

img_pad = padarray(img,[n_pix_border n_pix_border],'symmetric');

[ny, nx] = size(img_pad);
[X, Y] = meshgrid(0:nx-1, 0:ny-1);

dxf = nx-(abs(X-nx/2)+(nx/2));
dyf = ny-(abs(Y-ny/2)+(ny/2));

df = min(cat(3,dxf,dyf),[],3);
df = df./n_pix_border;
df(df>=1) = 1;

im_o = img_pad.*df;

end

0 comments on commit 7502794

Please sign in to comment.