Skip to content

Commit 7502794

Browse files
author
Thomas Friedrich
authored
Merge pull request #19 from ThFriedrich/peak_finder_2
Peak finder 2, compatibility fix, minor improvements
2 parents a324e2f + df8cd03 commit 7502794

2 files changed

Lines changed: 57 additions & 22 deletions

File tree

functions/PeakFinding/tfm_PeakFinder2.m

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
% License: Open Source under GPLv3
1616
% Contact: sandra.vanaert@uantwerpen.be
1717
%--------------------------------------------------------------------------
18-
global thr d_min sigma xy hpf gr_cm cdat_0
18+
global thr d_min sigma xy hpf gr_cm cdat_0 ver
19+
% Determine Matlab version for compatibility
20+
ver = version('-release');
21+
ver = str2double(ver(1:4));
22+
1923
obs = obj.obs;
2024
[ny,nx] = size(obs);
2125

@@ -53,7 +57,9 @@
5357
cdat = cdat_0;
5458
cdat(1:32) = sin(linspace(0,pi,32));
5559
imagesc(hpf.par.wb,cdat); axis off; colormap(gr_cm); caxis([0 1]);
56-
hpf.par.wb.Toolbar = [];
60+
if ver >= 2019
61+
hpf.par.wb.Toolbar = [];
62+
end
5763

5864
% Info panel
5965
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]);
@@ -71,21 +77,15 @@
7177
est_lim_s = mean(nx,ny)*0.1;
7278
SliderSi = uicontrol('Parent',hpf.par.pan,'Style','slider','units','normalized','Position',[br 0.38 0.2 0.15],'Min',3,'Max',est_lim_s);
7379
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]);
74-
% 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');
75-
% 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');
7680

7781
%Threshold
7882
SliderTh = uicontrol('Parent',hpf.par.pan,'Style','slider','units','normalized','Position',[br+0.2 0.38 0.2 0.15],'Min',0,'Max',1);
7983
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]);
80-
% 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');
81-
% 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');
8284

8385
%Peak Distance
84-
est_lim_d = mean(nx,ny)*0.1;
86+
est_lim_d = mean(nx,ny)*0.2;
8587
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);
8688
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]);
87-
% 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');
88-
% 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');
8989

9090
% Textboxes %
9191
%%%%%%%%%%%%%%%%%%
@@ -236,7 +236,9 @@ function deleteFigure(hObject,~)
236236
function waitbar_out()
237237
hpf.par.wb.Children.CData = cdat_0;
238238
set(hpf.par.wb,'visible','off')
239-
hpf.par.wb.Colormap = gr_cm;
239+
if ver >= 2019
240+
hpf.par.wb.Colormap = gr_cm;
241+
end
240242
drawnow;
241243
end
242244

@@ -247,17 +249,18 @@ function waitbar_out()
247249
timerDat.n_tick = 1;
248250
timerObject = timer('TimerFcn',@tick,...
249251
'ExecutionMode','fixedRate',...
250-
'Period',0.01,...
252+
'Period',0.05,...
251253
'UserData', timerDat);
252254
start(timerObject);
253255

254256
function tick(timerObj,event)
255-
256257
timerData = get(timerObj, 'UserData');
257258
im = timerData.im_dat;
258-
im = circshift(im,timerData.n_tick,2);
259+
im = circshift(im,timerData.n_tick*5,2);
259260
timerData.axes.Children.CData = im;
260-
timerData.axes.Colormap = gr_cm;
261+
if ver >= 2019
262+
timerData.axes.Colormap = gr_cm;
263+
end
261264
timerData.n_tick = timerData.n_tick + 1;
262265
set(timerObj, 'UserData', timerData);
263266
drawnow

functions/PeakFinding/tfm_find_peaks_2d.m

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
% License: Open Source under GPLv3
2222
% Contact: sandra.vanaert@uantwerpen.be
2323
%--------------------------------------------------------------------------
24-
24+
2525
% Avoide negative values
2626
img = img+min(img(:));
27+
img = img/max(img(:));
2728

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

3940
% Apply Wiener filter
4041
d = wiener2(d,[nkr_w nkr_w]);
42+
d = real(d);
4143

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

4850
d = max(min(img(:)), d);
49-
51+
5052
% Signal to noise ratio
5153
psnr = (var(d(:))/var(img(:)-d(:)));
5254
alpha = 1/psnr;
5355

5456
% Perform deconvolution with gaussian, according to:
55-
% Van Dyck, D. (2012). Basics of Image Processing and Analysis. p. 74. https://doi.org/10.5281/zenodo.51511
57+
% Van Dyck, D. (2012). Basics of Image Processing and Analysis. p. 74. https://doi.org/10.5281/zenodo.51511
5658
[ny, nx] = size(d);
5759
[gx,gy]=fcn_real_space_2_F_space(nx,ny);
5860
H = fcn_gauss_2d_fourier_space(gx,gy,sigma);
5961
Y = conj(H)./(abs(H).^2+(alpha));
60-
61-
d_deconv = fftshift(ifft2(fft2(ifftshift(d)).*ifftshift(Y)));
62+
63+
d_deconv = fcn_conv2D(d,Y);
6264
d_deconv = real(d_deconv);
65+
6366

64-
se = strel('disk',round(5*sigma));
67+
se = strel('disk',round(4*sigma));
6568
img_o = imtophat(d_deconv,se);
6669

6770
sc = max(img_o(:));
6871
img_o = img_o/sc;
69-
72+
7073
if nargout > 1
7174
xy = fcn_local_max(img_o,pk_min,d_min);
7275
else
@@ -180,5 +183,34 @@
180183
end
181184
img_o = max(0, a*x.^2+e+ix.*(b+ix.*(c+ix*d)));
182185

186+
187+
end
188+
189+
function im_o = fcn_conv2D(im1, im2)
190+
n_pix_border = 30;
191+
192+
im1_pad = fcn_smooth_borders(im1,n_pix_border);
193+
im2_pad = fcn_smooth_borders(im2,n_pix_border);
194+
195+
im_o = fftshift(ifft2(fft2(ifftshift(im1_pad)).*ifftshift(im2_pad)));
196+
im_o = im_o(n_pix_border+1:end-n_pix_border,n_pix_border+1:end-n_pix_border);
197+
183198
end
184-
199+
200+
function im_o = fcn_smooth_borders(img,n_pix_border)
201+
202+
img_pad = padarray(img,[n_pix_border n_pix_border],'symmetric');
203+
204+
[ny, nx] = size(img_pad);
205+
[X, Y] = meshgrid(0:nx-1, 0:ny-1);
206+
207+
dxf = nx-(abs(X-nx/2)+(nx/2));
208+
dyf = ny-(abs(Y-ny/2)+(ny/2));
209+
210+
df = min(cat(3,dxf,dyf),[],3);
211+
df = df./n_pix_border;
212+
df(df>=1) = 1;
213+
214+
im_o = img_pad.*df;
215+
216+
end

0 commit comments

Comments
 (0)