Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
c49d196e3a
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
137 lines (126 sloc) 4.25 KB
clear all
close all
clc
%% Set Paramters
inFolder = 'D:\data\Leona';
outFolder = 'D:\data\out';
binningFactor = [1, 1, 2];
nSubIm = 1000;%number of subImages for med align
referenceChannel = 0; %0: green, 1: red; reference channel for shift corrections
numShiftCorrections = 1; %number of shift corrections
%% Load Files
currentFolder = pwd;
cd(inFolder);
folders = dir;
for k = length(folders):-1:1
% remove non-folders
if ~folders(k).isdir
folders(k) = [ ];
continue
end
% remove folders starting with .
fname = folders(k).name;
if fname(1) == '.'
folders(k) = [ ];
end
end
numFolders = length(folders);
%iterate through all folders
for f = 1:numFolders
%go into folder
cd(inFolder);
cd(folders(f).name);
greenFiles = dir('ChanA_*_*.tif');
redFiles = dir('ChanB_*_*.tif');
%check if fileNumber is the same
if size(greenFiles, 1) ~= size(redFiles, 1)
error(['Unequal number of files in trial folder "', folders(f).name, '"!!']);
end
numFiles = size(greenFiles, 1);
%load first red frame to get resolution and being able to preallocate
temp = readTiffNoStack(redFiles(1).name);
res = size(temp);
%preallocate data_r
data_r = zeros(res(1), res(2), numFiles);
data_g = zeros(res(1), res(2), numFiles);
%load data
disp(['Loading Files In Folder ', folders(f).name]);
parfor i=1:numFiles
data_r(:,:,i) = readTiffNoStack(redFiles(i).name);
data_g(:,:,i) = readTiffNoStack(greenFiles(i).name);
end
disp(['Shift Correction Of Files In Folder ', folders(f).name]);
%save stacks in memory
cd(outFolder);
disp(['Write Files Of Folder ', folders(f).name ' in ' outFolder ' to RAM']);
data_r_complete{f} = data_r;
data_g_complete{f} = data_g;
end
%go back to old current folder
cd(currentFolder);
clear data_r data_g
data_r = data_r_complete;
data_g = data_g_complete;
%create big array
numFrames = 0;
numFrameList = zeros(1,numFolders);
for i = 1:numFolders
numFrames = numFrames + size(data_g{i},3);
numFrameList(i+1) = numFrames;
end
%preallocate data
data_r_array = zeros(size(data_g{i},1),size(data_g{i},2),numFrames, class(data_r{1}));
data_g_array = zeros(size(data_g{i},1),size(data_g{i},2),numFrames, class(data_g{1}));
for i = 1:numFolders
data_r_array(:,:,(numFrameList(i)+1):numFrameList(i+1)) = data_r{i};
data_r{i} = [];
data_g_array(:,:,(numFrameList(i)+1):numFrameList(i+1)) = data_g{i};
data_g{i} = [];
end
clear data_r_complete data_g_complete data_r data_g
data_r = data_r_array;
data_g = data_g_array;
clear data_g_array data_r_array
%% Do Shift Correction
wb = waitbar(0, 'Performing Shift Correction');
for j = 1:numShiftCorrections
if referenceChannel
referenceChannelData = data_r;
else
referenceChannelData = data_g;
end
%Med Align
parfor f=1:size(referenceChannelData,3)
referenceChannelData(:,:,f) = imgaussfilt(referenceChannelData(:,:,f),2);
end
[rs, cs] = medAlign(referenceChannelData, nSubIm, 50, -inf);
%shift red and green channel
waitbar(j/numShiftCorrections, wb, 'Performing Med Align..');
parfor i = 1:size(data_g,3)
data_r(:,:,i) = circshift(data_r(:,:,i),[rs(i),cs(i)]);
data_g(:,:,i) = circshift(data_g(:,:,i),[rs(i),cs(i)]);
end
end
%matVis(data_r,data_g);
%% Export Files
%save trial files
for i=1:numFolders
waitbar(i/numFolders, wb, 'Saving Trial Tiff Files..');
%binning
data_r_save = uint16(binning(data_r(:,:,numFrameList(i)+1:numFrameList(i+1)), binningFactor, 'mean'));
data_g_save = uint16(binning(data_g(:,:,numFrameList(i)+1:numFrameList(i+1)), binningFactor, 'mean'));
saveastiff(uint16(data_r_save), [outFolder filesep folders(i).name '_r.tif']);
saveastiff(uint16(data_g_save), [outFolder filesep folders(i).name '_g.tif']);
end
%save big tiff file
data_r = uint16(binning(data_r, binningFactor, 'mean'));
data_g = uint16(binning(data_g, binningFactor, 'mean'));
disp('Saving Big Data Set..');
waitbar(0, wb, 'Saving Big Tiff Files..');
options.big = true;
saveastiff(data_r, [outFolder filesep 'complete_r.tif'], options);
waitbar(0.5, wb, 'Saving Big Tiff Files..');
saveastiff(data_g, [outFolder filesep 'complete_g.tif'], options);
%%
close(wb);
disp('Finished Script!');