diff --git a/fiji_script_vertical_combination.py b/fiji_script_vertical_combination.py index 261c9ab..d6f9064 100644 --- a/fiji_script_vertical_combination.py +++ b/fiji_script_vertical_combination.py @@ -1,66 +1,76 @@ #@String name from ij import IJ, ImagePlus from ij.io import Opener, FileSaver from ij.plugin import RGBStackMerge, StackCombiner, Resizer from ij.process import ImageConverter from ij.plugin import Resizer import json #import file paths json_file = open('files.json','r') text = json_file.read() files = json.loads(text) json_file.close() #Instanciate necessary classes opener = Opener() RGB_stack_merge = RGBStackMerge() stack_combiner = StackCombiner() resizer = Resizer() #Open images unregistered_GC6s_info = opener.getTiffFileInfo(files['GC6s_input']) unregistered_GC6s_imp = opener.openTiffStack(unregistered_GC6s_info) unregistered_tdTom_info = opener.getTiffFileInfo(files['tdTom_input']) unregistered_tdTom_imp = opener.openTiffStack(unregistered_tdTom_info) registered_GC6s_info = opener.getTiffFileInfo(files['GC6s_registered']) registered_GC6s_imp = opener.openTiffStack(registered_GC6s_info) registered_tdTom_info = opener.getTiffFileInfo(files['tdTom_registered']) registered_tdTom_imp = opener.openTiffStack(registered_tdTom_info) vector_info = opener.getTiffFileInfo(files['vectors_out']) vector_imp = opener.openTiffStack(vector_info) +#Convert fluorescence images to 32-bit and adjust brightness +IJ.run(unregistered_GC6s_imp,"32-bit","slices") +IJ.run(registered_GC6s_imp,"32-bit","slices") +IJ.run(unregistered_tdTom_imp,"32-bit","slices") +IJ.run(registered_tdTom_imp,"32-bit","slices") +IJ.run(unregistered_GC6s_imp, "Enhance Contrast", "saturated=0.35"); +IJ.run(registered_GC6s_imp, "Enhance Contrast", "saturated=0.35"); +IJ.run(unregistered_tdTom_imp, "Enhance Contrast", "saturated=0.35"); +IJ.run(registered_tdTom_imp, "Enhance Contrast", "saturated=0.35"); + #Remove first frame from registered and unregistered stacks IJ.run(unregistered_GC6s_imp, "Delete Slice", "") IJ.run(registered_GC6s_imp, "Delete Slice", "") IJ.run(unregistered_tdTom_imp, "Delete Slice", "") IJ.run(registered_tdTom_imp, "Delete Slice", "") #Upsample to resolution of vector image IJ.run(unregistered_GC6s_imp, "Size...", "width=%d height=%d depth=%d interpolation=None" % (vector_imp.getWidth(),vector_imp.getHeight(),vector_imp.getStackSize())) IJ.run(registered_GC6s_imp, "Size...", "width=%d height=%d depth=%d interpolation=None" % (vector_imp.getWidth(),vector_imp.getHeight(),vector_imp.getStackSize())) IJ.run(unregistered_tdTom_imp, "Size...", "width=%d height=%d depth=%d interpolation=None" % (vector_imp.getWidth(),vector_imp.getHeight(),vector_imp.getStackSize())) IJ.run(registered_tdTom_imp, "Size...", "width=%d height=%d depth=%d interpolation=None" % (vector_imp.getWidth(),vector_imp.getHeight(),vector_imp.getStackSize())) #Convert all images to RGB unregistered_merged_channels_imp = RGBStackMerge.mergeChannels([unregistered_tdTom_imp, unregistered_GC6s_imp, None],False) registered_merged_channels_imp = RGBStackMerge.mergeChannels([registered_tdTom_imp, registered_GC6s_imp, None],False) IJ.run(unregistered_merged_channels_imp, "RGB Color", "slices") IJ.run(registered_merged_channels_imp, "RGB Color", "slices") IJ.run(vector_imp, "RGB Color", "slices") #Combine image into one (stacked on top of each other) two_combined_stack = stack_combiner.combineVertically(unregistered_merged_channels_imp.getStack(), vector_imp.getStack()) three_combined_stack = stack_combiner.combineVertically(two_combined_stack, registered_merged_channels_imp.getStack()) #Save result file_saver_combined_image = FileSaver(ImagePlus('combined',three_combined_stack)) file_saver_combined_image.saveAsTiffStack(files['column_out']) diff --git a/files.json b/files.json index 337f6b2..62302df 100644 --- a/files.json +++ b/files.json @@ -1,9 +1,9 @@ { - "tdTom_input": "/home/aymanns/data/1_Horizontal_VNC/tdTom.tif", "GC6s_input": "/home/aymanns/data/1_Horizontal_VNC/GC6s.tif", - "tdTom_registered": "/home/aymanns/motion-correction-paper/results/test/warped1.tif", - "GC6s_registered": "/home/aymanns/motion-correction-paper/results/test/warped2.tif", + "tdTom_input": "/home/aymanns/data/1_Horizontal_VNC/tdTom.tif", + "GC6s_registered": "/home/aymanns/motion-correction-paper/results/test/warped1.tif", + "tdTom_registered": "/home/aymanns/motion-correction-paper/results/test/warped2.tif", "color_flow_out" : "/home/aymanns/motion-correction-paper/results/test/color_flow.tif", "vectors_out": "/home/aymanns/motion-correction-paper/results/test/vectors.tif", "column_out": "/home/aymanns/motion-correction-paper/results/test/column.tif" } diff --git a/motion_compensate.m b/motion_compensate.m index fbe102d..1a284b4 100755 --- a/motion_compensate.m +++ b/motion_compensate.m @@ -1,77 +1,77 @@ function motion_compensate(fnInput1,fnInput2,fnMatch,fnDeepMatching,fnOut1,fnOut2,fnColor,fnVector,N,param) %% Description % Motion is estimated with a brigthness constancy data term defined on fnInput1 % and a feature matching similarity term defined on fnMatch. The sequences fnIput1 and % fnInput2 are warped according to the estimated motion field. % For more details see the paper: "Imaging neural activity in the ventral % nerve cord of behaving adult Drosophila", bioRxiv % %% Input % fnInput1: filename of the sequence used for the brightness constancy term, in TIF format % fnInput2: filename of the sequence warped with the motion field estimated from fnInput1 and fnMatch, in TIF format % fnMatch: filename of the sequence used for feature matching, in TIF format % fnDeepMatching: filename of the deepmatching code % fnOut1: filename used to save the warped version of fnInput1, in TIF format % fnOut2: filename used to save the warped version of fnInput2, in TIF format % fnOut2: filename used to save the color visualization of the estimated motion, in TIF format % N: number of frames to process % param: parameters of the algorithm (see 'default_parameters.m') % % Copyright (C) 2017 D. Fortun, denis.fortun@epfl.ch % % This program is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by % the Free Software Foundation, either version 3 of the License, or % (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. %% Input data seq=mijread_stack(fnInput1); seqW=mijread_stack(fnInput2); seqMatch=mijread_stack(fnMatch); if N==-1 N=size(seq,3); end seq=double(seq(:,:,1:N)); seqRescale=(seq-min(seq(:)))/(max(seq(:))-min(seq(:)))*255; seqW=double(seqW(:,:,1:N)); seqMatch=double(seqMatch(:,:,1:N)); seqMatch=(seqMatch-min(seqMatch(:)))/(max(seqMatch(:))-min(seqMatch(:)))*255; -%% Motion estimation +% Motion estimation w=zeros(size(seqRescale,1),size(seqRescale,2),2,size(seqRescale,3)-1); colorFlow=zeros(size(seqRescale,1),size(seqRescale,2),3,size(seqRescale,3)-1); vectorFlow=zeros(512,512,size(seqRescale,3)-1); i1=seqRescale(:,:,1); i1Match=seqMatch(:,:,1); for t=1:N-1 % Replace parfor by for if you don't want to parallelize fprintf('Frame %i\n',t); i2=seqRescale(:,:,t+1); i2Match=seqMatch(:,:,t+1); [i10,i2]=midway(i1,i2); w(:,:,:,t) = compute_motion(i10,i2,i1Match,i2Match,fnDeepMatching,param,t); colorFlow(:,:,:,t)=flowToColor(w(:,:,:,t)); vectorFlow(:,:,t)=flowToVectorImage(w(:,:,:,t),15,[512,512]); end %% Registration seqWarped=seq; seqwWarped=seqW; parfor t=1:N-1 seqWarped(:,:,t+1)=warpImg(seq(:,:,t+1),w(:,:,1,t),w(:,:,2,t)); seqwWarped(:,:,t+1)=warpImg(seqW(:,:,t+1),w(:,:,1,t),w(:,:,2,t)); end %% Save mijwrite_stack(single(seqWarped),fnOut1); mijwrite_stack(single(seqwWarped),fnOut2); mijwrite_stack(single(colorFlow),fnColor,1); mijwrite_stack(single(vectorFlow),fnVector);