Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
added a test folder for the medAlign function
Browse files Browse the repository at this point in the history
MPIBR-vollrathf committed Jul 29, 2016
1 parent bb5cc17 commit 96941f5
Showing 4 changed files with 431 additions and 0 deletions.
98 changes: 98 additions & 0 deletions medAlign_new/batchMedAlignWithTemporalBinning.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

nSubIm = 10;

% set a query directory
dir_query = '/storage/letz/Data/Data/Leona_Enke/20160726_L59/imaging';
dir_output = '/storage/letz/Data/Data/Leona_Enke/20160726_L59/imaging';

% level one
[dir_list, dir_count] = getDirsFromDir(dir_query);

d = 1;

for d = 1 : dir_count

% sub dir name
sub_dir = [dir_query, filesep, dir_list{d}];
subdir_list = getDirsFromDir(sub_dir);
disp(d);

% remove finished
idx_rmv = strncmp('aligned',subdir_list,7);
subdir_list(idx_rmv) = [];
subdir_count = size(subdir_list,1);

% loop through sub dirs
for k = 10 : subdir_count



% current id
folder_id = sscanf(subdir_list{k},'Untitled%03d');




% Compose image stack from filenames

filePrefixGreen = 'ChanA_0001_0001_0001_'; %Gruen Kanal
filePrefixRed = 'ChanB_0001_0001_0001_'; %Rote Kanal

m = dir([sub_dir,filesep,subdir_list{k},filesep,'ChanB_0001_0001_0001_*.tif']);
nImages = size(m,1);

file_image = [sub_dir,filesep,subdir_list{k},filesep,'ChanB_0001_0001_0001_0001.tif'];
im = imread(file_image);


%Assembling Red Image stack
allIm = zeros(size(im,1),size(im,2),nImages);
for i = 1:nImages
filename= sprintf([sub_dir,filesep,subdir_list{k},filesep,filePrefixRed,'%04u.tif'],i);
allIm(:,:,i) = imread(filename);
end

%temporal binning on red image stack
class_red = class(allIm);
allIm = cast(binning(allIm, [1 1 2], 'mean'), class_red);

%Realignment
[rs, cs] = medAlign(allIm, nSubIm, 50, -inf);

%Rotation and translation on red channel
filename= sprintf('%s%s%s%sred%03d.tif',dir_output,filesep,dir_list{d},filesep,folder_id);
for i = 1:nImages/2
registered(:,:,i) = circshift(allIm(:,:,i),[rs(i),cs(i)]);
imwrite(uint16(registered(:,:,i)),filename, 'tif','WriteMode','append');
end


%Assembling Green Image stack
allIm = zeros(size(im,1),size(im,2),nImages);
for i = 1:nImages
filename= sprintf([sub_dir,filesep,subdir_list{k},filesep,filePrefixGreen,'%04u.tif'],i);
allIm(:,:,i) = imread(filename);
end

%temporal binning on green image stack
class_red = class(allIm);
allIm = cast(binning(allIm, [1 1 2], 'mean'), class_red);

%Rotation and translation on green channel
filename= sprintf('%s%s%s%sgreen%03d.tif',dir_output,filesep,dir_list{d},filesep,folder_id);
%filename= sprintf([dir_output,filesep,filePrefixGreen,'_cc.tif'],i);
for i = 1:nImages/2
registered(:,:,i) = circshift(allIm(:,:,i),[rs(i),cs(i)]);
imwrite(uint16(registered(:,:,i)),filename, 'tif','WriteMode','append');
end



end





end

149 changes: 149 additions & 0 deletions medAlign_new/binning.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
function A = binning(A,bin,fun_str)
%
% A=binning(A,bin,fun)
%
% This function rearranges the array A according to the specifications
% of the vector bin. In the first dimension of the generated temporary
% array the bin elements from all dimension are arranged. By applying
% functions like 'sum', 'mean' or 'std' to the first dimension, a real
% binning ('sum'), a averaging ('mean') and error calculations can be
% done.
%
% A=binning(A,bin) The input array 'A' is binned by the vector 'bin'.
% If 'bin' is a scalar, 'bin' elements in each dimenion of 'A' are
% summarized.
%
% Note: The input array 'A' is cut to the maxial integer factor of
% 'bin'.
%
% A=binning(A,bin,fun) The function 'fun' is applied to the first
% dimension of the rearranged array. Beside the real binning with the
% function 'sum' a number of additional functions can be applied. Up
% to now the following functions are implemented:
% 'sum', 'mean', 'prod'
%
% Special comment:
% The functionality can be easily extended by implementing additional
% functions, or due to the time consuming permutations by the
% combination of appropriate functions like 'mean' and 'std'.
%
% Author: A. Zeug (last modified 19.07.2007)
% Version: 2.0.0

% comments to: azeug@gwdg.de

tic; t1=toc;
%% Input validation
if nargin < 3
fun_str='sum';
fun_type = 1;
elseif sum(strcmp(fun_str,{'sum';'mean';'prod'}))>0
fun_type = 1;
elseif sum(strcmp(fun_str,{'std';'var'}))>0
fun_type = 2;
else
error('Function not implemented yet')
end
fun=str2func(fun_str); % defines function type

if nargin < 1
help binning
return
elseif nargin < 2
error('Please specify binning vector.')
end

%% bin vector dimension
trans = false;
dims = ndims(A);
if isvector(A)
dims = 2;
if size(A,2)==1; trans = true; end
end
if length(bin)==1;
bin = bin * ones(1, dims);
elseif length(bin)~=dims;
error('Wrong length of binning vector.')
end

%% resizing of A to fit bin
sz=size(A);
if dims==1; sz=length(A); end
bin=min([sz;bin]);
for i=1:dims
index{i}=1:(bin(i)*floor(sz(i)/bin(i)));
end
A = A(index{:});

%% reshaping A
sz = size(A); if dims==1; sz = length(A); end
v = []; sz_new = sz./bin;
for i=1:dims
v=[v, bin(i), sz_new(i)];
end
A=reshape(A,v);

%% Calculate binning
if fun_type==1
% standard type 'sum', 'mean','prod'
for i=1:dims
if isequal(fun, @sum)
A=fun(A,(2*i-1),'native');
else
A=fun(A,(2*i-1));
end
end
else
% type 'std', 'var'
% need to reshape again to calc function at ones
v = [];
for i=1:dims
v=[v, i, dims+i];
end
A = permute(A, v);
A = reshape(A, [prod(bin) sz_new]);
try
A = fun( double(A) );
catch
t=toc; fprintf('(%07.3fs) binning: direct calculation failed, start subsequential method (%0.5fs)\n',t,t-t1); t1=t;
try
A1 = zeros(sz_new,'double');
fun1 = @double;
catch
t=toc; fprintf('(%07.3fs) binning: calculating %s with double precession failed, try single (%0.5fs)\n',t,func2str(fun),t-t1); t1=t;
A1 = zeros(sz_new, 'single');
fun1 = @single;
end
h = waitbar(0,'Please wait...','Name','binning: Plese Wait!');
% subsequential calculation
sub_dim = find(sz_new == max(sz_new));
for i = 1:dims; cc{i}=':'; end
step = floor(1E6 / (prod(sz_new(sz_new < max(sz_new)))*prod(bin)));
for ii = 0:step:sz_new(sub_dim) % subsequentially...
waitbar(ii/sz_new(sub_dim),h,['Calculated binning: ' num2str(100*ii/sz_new(sub_dim),'%0.2f') '%']);
ii_end = min(sz_new(sub_dim), ii+step) - ii;
cc{sub_dim}=(1:ii_end)+ii;
A1(cc{:}) = fun(fun1(A(:,cc{:})));
end
A = A1;
delete(h); drawnow;
t=toc; fprintf('(%07.3fs) binning: calculation with %s precession susseed (%0.5fs)\n',t,func2str(fun1),t-t1); t1=t;
end
end

%
%A = reshape(A, sz_new);

if dims==1
A=squeeze(A);
if trans==true
A=A';
end
else
A = reshape(A, sz_new);

end




Loading

0 comments on commit 96941f5

Please sign in to comment.