From 9f5231e2bec15a06613bc4f6fe567aaa7de1805f Mon Sep 17 00:00:00 2001 From: Florian Vollrath Date: Tue, 20 Sep 2016 11:26:10 +0200 Subject: [PATCH] deleted iterative shift correction and implemented cross correlation --- CellSortXCorr.m | 73 +++++++++++++++++++++++++++++++++++++ main.m | 33 +++++------------ readTiffNoStack.m | 4 ++ testOfDifferentTifImports.m | 32 ++++++++++++++++ 4 files changed, 119 insertions(+), 23 deletions(-) create mode 100644 CellSortXCorr.m create mode 100644 readTiffNoStack.m create mode 100644 testOfDifferentTifImports.m diff --git a/CellSortXCorr.m b/CellSortXCorr.m new file mode 100644 index 0000000..b065225 --- /dev/null +++ b/CellSortXCorr.m @@ -0,0 +1,73 @@ +function CellSortXCorr(nSubIm, nImages, m, n) +% nSubIm = 10; +% nImages = 1344; +% m=14; +% n=24; + +for k = m:n; + % Compose image stack from filenames + folder = sprintf('/storage/letz/Data/Data/Elisabeth_Abs/75EA/habituation/%u', k) + if exist(folder) == 0 + continue + end + folder_corr = '/storage/letz/Data/Data/Elisabeth_Abs/75EA/habituation/corrected'; + + file_image_red = [folder, 'ChanB_0001_0001_0001_0001.tif']; + filePrefix_red = 'ChanB_0001_0001_0001_'; + + file_image_green = [folder, 'ChanA_0001_0001_0001_0001.tif']; + filePrefix_green = 'ChanA_0001_0001_0001_'; + + filename_red = sprintf([folder,filesep,filePrefix_red,'%04u.tif'],1); + im_red = imread(filename_red); + + filename_green = sprintf([folder,filesep,filePrefix_green,'%04u.tif'],1); + im_green = imread(filename_green); + + %preallocate data variable + allIm_red = zeros(size(im_red,1),size(im_red,2),nImages); + %read tiffs + for i = 1:nImages + filename_red = sprintf([folder,filesep,filePrefix_red,'%04u.tif'],i); + allIm_red(:,:,i) = readTiffNoStack(filename_red); + end + + %preallocate data variable + allIm_green = zeros(size(im_green,1),size(im_green,2),nImages); + %read tiffs + for i = 1:nImages + filename_green = sprintf([folder,filesep,filePrefix_green,'%04u.tif'],i); + allIm_green(:,:,i) = readTiffNoStack(filename_green); + end + + + [rs, cs] = medAlign(allIm_red, nSubIm, 50, -inf); + + registered = zeros(size(allIm_red,1),size(allIm_red,2),size(allIm_red,3), class(allIm_red)); + parfor i = 1:nImages + registered(:,:,i) = circshift(allIm_red(:,:,i),[rs(i),cs(i)]); + end + + data = binning(registered,[1,1,2],'mean'); + + filename = sprintf([folder_corr,filesep,'red_%u.tiff'],k); + for j=1:(nImages/2) + imwrite(uint16(data(:,:,j)),filename, 'tiff','WriteMode','append'); + end + + + + %align green channel with parameters from red alignment + parfor i = 1:nImages + registered(:,:,i) = circshift(allIm_green(:,:,i),[rs(i),cs(i)]); + end + + data = binning(registered,[1,1,2],'mean'); + + filename = sprintf([folder_corr,filesep,'green_%u.tiff'],k); + for j=1:(nImages/2) + imwrite(uint16(data(:,:,j)),filename, 'tiff','WriteMode','append'); + end + + +end \ No newline at end of file diff --git a/main.m b/main.m index 68810ae..9a4498b 100644 --- a/main.m +++ b/main.m @@ -1,29 +1,16 @@ -%% load and prepare all single tiffs -%importing -[data_g, data_r] = CellSortLoadSingleFiles('ChanA_*_*.tif', 'ChanB_*_*.tif'); -%% matVis -matVis(data_r); -%% get ref frame for shift correction -interval = 1:20; -refFrame = cast(mean(data_r(:,:,interval), 3), class(data_r)); -matVis(refFrame); -%% shiftCorrection -%generates shift corrected files for the green and red channels, needs -%DIPlib (available for free) -[data_g, data_r] = CellSortShiftCorrection(data_g, data_r, refFrame); -%temporal binning -binningFactor = [1 1 2]; %x, y, t -[data_g, data_r] = CellSortTemporalBinning(data_g, data_r, binningFactor); - -fn = 'processedData.mat'; -save(fn, 'data_g', 'data_r', '-v7.3'); +%% load files and do cross correlation alignment +nSubIm = 10; +nImages = 1344; +m=14; +n=24; +CellSortXCorr(nSubIm, nImages, m, n); %% load files -%if you haven't used the shiftCorrection you have to load the already processed files -fn= 'processedData.mat'; -load(fn); +fn_green = 'data_g.tif'; +fn_red = 'data_r.tif'; +data_g = readTiff(fn_green); +data_r = readTiff(fn_red); %% 1. PCA %calculate PCAs -fn_green = 'processedData.mat'; nPCs = 100; flims = []; dSamp = [1 1]; %temporal, spatial downsampling diff --git a/readTiffNoStack.m b/readTiffNoStack.m new file mode 100644 index 0000000..be515d5 --- /dev/null +++ b/readTiffNoStack.m @@ -0,0 +1,4 @@ +function data = readTiffNoStack(filename) +t_g = Tiff(filename, 'r'); +data = t_g.read(); +end \ No newline at end of file diff --git a/testOfDifferentTifImports.m b/testOfDifferentTifImports.m new file mode 100644 index 0000000..4e2fe42 --- /dev/null +++ b/testOfDifferentTifImports.m @@ -0,0 +1,32 @@ +fn = 'test.tif'; + +tic; + +for i=1:5000 +data = readTiff(fn); +end + +timeReadTiff = toc; + +tic; +for i=1:5000 +data = imread(fn); +end + +timeImread = toc; + + +tic; +for i=1:5000 +data = readTiffNoStack(fn); +end + +timeNoStack = toc; + +tic; +for i=1:5000 +t_g = Tiff(fn, 'r'); +data = t_g.read(); +end + +timeNoStackNoFunction = toc; \ No newline at end of file