function [] = iterativeRegistering(vidname, NCparamStruct, regname) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Consecutive registrations with increasing grid number (2x2 -> 3x3 -> 6x6) % Inputs: % - vidname: The name of the video. Assumes that the videos are h5 % files % - NCparamStruct: A struct of the desired parameters for NormCorre % - regname: The desired name for the registered video % Output: (none) % - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% load in the data video = h5read(vidname, '/video'); [d1 d2 d3] = size(video); templateim = mean(video(:, :, 1:50), 3); M2 = video; %% Calculate initial metrics [cM2f(:, 1),mM2f,vM2f(1)] = motion_metrics(M2, 0); % Check to see if the h5 file exists. If it doesn't create it. clear video iteration = 0; vidcheck = dir(regname) if isempty(vidcheck) h5create(regname, '/iteration', size(iteration)); h5write(regname, '/iteration', iteration); h5create(regname, '/originalname', size(string(vidname)), 'Datatype', 'string'); h5write(regname, '/originalname', string(vidname)); end %% Go through the different stages of registration maxmove = [0 0 0 0]; for fn = 1:size(NCparamStruct.gridsize, 1) if fn == 1 else templateim = mean(M2(:, :, 1:50), 3); end completed = h5read(regname, '/iteration'); if completed < fn % Check to see if the registration completed successfully because % sometimes it can fail in the upsampling factor is too high count = 0; display(num2str(count)) err_count = 0; M2 = M2(maxmove(fn, 3)+1:end-maxmove(fn, 4), maxmove(fn, 1)+1:end-maxmove(fn, 2), :); templateim = templateim(maxmove(fn, 3)+1:end-maxmove(fn, 4), maxmove(fn, 1)+1:end-maxmove(fn, 2), :); [d1(fn+1) d2(fn+1) d3(fn+1)] = size(M2); options_nr = NoRMCorreSetParms('d1',d1(fn+1),'d2',d2(fn+1),'bin_width',50, ... 'grid_size',NCparamStruct.gridsize(fn, :),'mot_uf',NCparamStruct.motuf(fn),'correct_bidir',false, ... 'overlap_pre',NCparamStruct.overlap(fn),'overlap_post', NCparamStruct.overlap(fn),'max_shift',.... NCparamStruct.maxshift(fn), 'upd_template', false, 'shifts_method', NCparamStruct.interp,... 'max_dev', NCparamStruct.max_dev(fn)); tic; [M2,shifts2{fn+1},template2] = normcorre_batch(M2,options_nr,templateim); toc itname = strcat('/video', num2str(fn)); cshift = convertShifts(shifts2{fn+1}); maxmove(fn+1, :) = ceil([abs(max([0 max(cshift.x(:))])) abs(min([0 min(cshift.x(:))])) abs(max([0 max(cshift.y(:))])) abs(min([0 min(cshift.y(:))]))]); infcheck = h5info(regname); if ~contains(strcat([infcheck.Datasets.Name]), itname(2:end)) h5create(regname, itname, size(M2), 'Datatype', 'uint8'); end h5write(regname, itname, M2); iteration = fn; h5write(regname, '/iteration', iteration); end end %% Save the associated data into a .mat file for easier use assocdat = strcat(regname(1:end-3), '_associated.mat'); h5create(regname, '/datname', size(string(assocdat)), 'Datatype', 'string'); h5write(regname, '/datname', string(assocdat)); save(assocdat, 'maxmove', 'd1', 'd2', 'shifts2', 'NCparamStruct'); end