MRI_Image_Conversion_Tools.txt

tools to convert lif and lsm images to tif - Volker Baecker, 07/08/2011 06:21 PM

Download (7.19 KB)

 
1
/**
2
  * MRI Image Conversion Tools
3
  * Tools for batch-converting lsm or lif images to tif
4
  *
5
  * Work on a folder containing lif or lsm files
6
  * For each file and for each image it contains:
7
  * 	- save the channels each as a tif file
8
  *	- save the overlay as an RGB tif file
9
  *                      - optional: save stacks as projections
10
  *
11
  * written 2011 by Volker Baecker (INSERM) at Montpellier RIO Imaging (www.mri.cnrs.fr)
12
  *
13
  * The Lif2Tif macro is based on the Leica LIF Extractor macro by Christophe LETERRIER (http://christopheleterrier.com/LeterrierSOFT.html)
14
  * which is based on the ZStacks Projector macro version 1.1 14/05/2008
15
*/
16

    
17
var helpURL = "http://dev.mri.cnrs.fr/wiki/imagej-macros/Image_Conversion_Tools"
18
var DO_PROJECTION = false;
19
var USE_ALL_SLICES_FOR_PROJECTION = false;
20
var PROJECTION_START_SLICE = 1;
21
var PROJECTION_END_SLICE = 3;
22
var PROJECTION_METHOD = "Max Intensity";
23

    
24
var OUTPUT_SUBFOLDER = "tif"
25

    
26
macro "Unused Tool - C037" { }
27

    
28
macro "MRI Image Conversion Tools Help Action Tool - C000T4b12?" {
29
    run('URL...', 'url='+helpURL);
30
}
31
 
32
macro "Lif2Tif Action Tool - C037T0b11LT7b09iTcb09f" {
33

    
34
DIR_PATH=getDirectory("Select a directory");
35
OUTPUT_DIR = DIR_PATH + "/" + OUTPUT_SUBFOLDER;
36
File.makeDirectory(OUTPUT_DIR);
37

    
38
print("\\Clear");
39
print("converting lif file in folder: "+DIR_PATH);
40

    
41
// Get all file names
42
ALL_NAMES=getFileList(DIR_PATH);
43
ALL_EXT=newArray(ALL_NAMES.length);
44
// Create extensions array
45
for (i=0; i<ALL_NAMES.length; i++) {
46
	LENGTH=lengthOf(ALL_NAMES[i]);
47
	ALL_EXT[i]=substring(ALL_NAMES[i],LENGTH-4,LENGTH);
48
}
49

    
50
setBatchMode(true);
51

    
52
// Loop on all .lei and .lif extensions
53
fileIndex = 0;
54
numberOfLifFiles = 0;
55
for (n=0; n<ALL_EXT.length; n++) {
56
	if (ALL_EXT[n]==".lei" || ALL_EXT[n]==".lif") numberOfLifFiles++;
57
}
58

    
59
for (n=0; n<ALL_EXT.length; n++) {
60
if (ALL_EXT[n]==".lei" || ALL_EXT[n]==".lif") {
61
    fileIndex++;
62
    // Get the file path
63
    FILE_PATH=DIR_PATH+ALL_NAMES[n];
64
    FILE_NAME=File.getName(FILE_PATH);
65

    
66
    // Start BioFormats and get series number in file.
67
    run("Bio-Formats Macro Extensions");
68
    Ext.setId(FILE_PATH);
69
    Ext.getSeriesCount(SERIES_COUNT);
70
    SERIES_NAMES=newArray(SERIES_COUNT);
71

    
72
    seriesIndex = 0;
73
    for (i=0; i<SERIES_COUNT; i++) {
74
          seriesIndex++;
75
          print("\\Update2:converting image " + (fileIndex) + " of " + numberOfLifFiles + " / series " + seriesIndex + " of " + SERIES_COUNT);
76
          Ext.setSeries(i);
77
          Ext.getEffectiveSizeC(CHANNEL_COUNT);
78
          Ext.getSizeZ(SLICE_COUNT);
79
          SERIES_NAMES[i]="";
80
          Ext.getSeriesName(SERIES_NAMES[i]);
81
          TEMP_NAME=toLowerCase(SERIES_NAMES[i]);
82
          run("Bio-Formats Importer", "open=["+ FILE_PATH + "] " + " color_mode=Default view=Hyperstack" + " stack_order=Default " + "series_"+d2s(i+1,0));
83
          Stack.setDisplayMode("composite");
84
          title = getTitle();
85
          run("Stack to RGB", "slices keep");
86
          rename(title + "-1");
87
           if (SLICE_COUNT>1 && DO_PROJECTION)  doProjection();
88
          currentTitle = getTitle();
89
          index = lastIndexOf(currentTitle, ".lif - ");
90
          name = substring(currentTitle, index+7, lengthOf(currentTitle)-2);
91
          save(OUTPUT_DIR + "/" + name + " - RGB.tif");
92
          close();	
93
          run("Split Channels");
94
          // Loop on each channel (each opened window)
95
         for(j=1; j<CHANNEL_COUNT+1; j++) {
96
	selectImage("C"+j+"-"+title); 
97
	if (SLICE_COUNT>1 && DO_PROJECTION)  doProjection();
98
	currentTitle = getTitle();
99
	index = lastIndexOf(currentTitle, ".lif - ");
100
	name = substring(currentTitle, index+7, lengthOf(currentTitle));
101
	save(OUTPUT_DIR + "/" + name + " - C" + j + ".tif");
102
	close();
103
        }
104
   }
105
}
106
}
107
setBatchMode("exit and display");
108
showStatus("finished converting lif to tif");
109
print("finished converting lif to tif");
110
}
111

    
112

    
113

    
114
macro "Lsm2Tif Action Tool - C037T0b11LT7b09sTcb09m" {
115
    DIR_PATH=getDirectory("Select a directory");
116
    OUTPUT_DIR = DIR_PATH + "/" + OUTPUT_SUBFOLDER;
117
    File.makeDirectory(OUTPUT_DIR);
118

    
119
    print("\\Clear");
120
    print("converting lsm file in folder: "+DIR_PATH);
121

    
122
    // Get all file names
123
    ALL_NAMES=getFileList(DIR_PATH);
124
    ALL_EXT=newArray(ALL_NAMES.length);
125
    // Create extensions array
126
    for (i=0; i<ALL_NAMES.length; i++) {
127
	LENGTH=lengthOf(ALL_NAMES[i]);
128
	ALL_EXT[i]=substring(ALL_NAMES[i],LENGTH-4,LENGTH);
129
    } 
130

    
131
    setBatchMode(true);
132

    
133
    // Loop on all .lei and .lsm extensions
134
    fileIndex = 0;
135
    numberOfLifFiles = 0;
136
    for (n=0; n<ALL_EXT.length; n++) {
137
        if (ALL_EXT[n]==".lsm" )  numberOfLifFiles++;
138
    }
139

    
140
    for (n=0; n<ALL_EXT.length; n++) {
141
        if (ALL_EXT[n]==".lsm") {
142
            fileIndex++;
143
            // Get the file path
144
            FILE_PATH=DIR_PATH+ALL_NAMES[n];
145
            FILE_NAME=File.getName(FILE_PATH);
146
            print("\\Update2:converting image " + (fileIndex) + " of " + numberOfLifFiles);
147
            open(FILE_PATH);
148
            Stack.setDisplayMode("composite");
149
            title = getTitle();
150
            getDimensions(width, height, colors, slices, frames);
151
            run("Stack to RGB", "slices keep");
152
            rename(title + "-1");
153
             if (slices>1 && DO_PROJECTION)  doProjection();
154
            currentTitle = getTitle();
155
            name = substring(currentTitle, 0, lengthOf(currentTitle)-6);
156
            save(OUTPUT_DIR + "/" + name + " - RGB.tif");
157
            close();	
158
            run("Split Channels");
159
            // Loop on each channel (each opened window)
160
            for(j=1; j<colors+1; j++) {
161
	selectImage("C"+j+"-"+title); 
162
	if (slices>1 && DO_PROJECTION)  doProjection();
163
	currentTitle = getTitle();
164
	name = substring(currentTitle, 3, lengthOf(currentTitle)-4);
165
	save(OUTPUT_DIR + "/" + name + " - C" + j + ".tif");
166
	close();
167
            }
168
            close();
169
       }
170
    }
171
    setBatchMode("exit and display");
172
    showStatus("finished converting lsm to tif");
173
    print("finished converting lsm to tif");
174
}
175

    
176
function doProjection() {
177
    currentTitle = getTitle();
178
    startSlice = PROJECTION_START_SLICE;
179
    endSlice = PROJECTION_END_SLICE;
180
    if (USE_ALL_SLICES_FOR_PROJECTION) {
181
        startSllice = 1;
182
        endSlice = SLICE_COUNT;
183
    }
184
    run("Z Project...", "start=" + startSlice + " stop=" + endSlice +" projection=" + "[" + PROJECTION_METHOD + "]");
185
    selectImage(currentTitle);
186
    close();
187
    rename(currentTitle);
188
}
189

    
190
macro 'Lif2Tif Action Tool Options' {
191
     showOptionsDialog()
192
}
193

    
194
macro 'Lsm2Tif Action Tool Options' {
195
     showOptionsDialog()
196
}
197

    
198
function showOptionsDialog() {
199
       Dialog.create("Lif2Tif Options");
200
       Dialog.addCheckbox("do z-projection", DO_PROJECTION);
201
       Dialog.addCheckbox("use all slices for projection", USE_ALL_SLICES_FOR_PROJECTION);
202
       Dialog.addNumber("start slice for projection", PROJECTION_START_SLICE);
203
       Dialog.addNumber("end slice for projection", PROJECTION_END_SLICE);
204
       Dialog.addChoice("projection method", newArray("Average Intensity", "Max Intensity", "Min Intensity", "Sum Slices", "Standard Deviation", "Median"), PROJECTION_METHOD);
205
       Dialog.show();
206
       DO_PROJECTION = Dialog.getCheckbox();
207
       USE_ALL_SLICES_FOR_PROJECTION = Dialog.getCheckbox();
208
       PROJECTION_START_SLICE = Dialog.getNumber();
209
       PROJECTION_END_SLICE = Dialog.getNumber();
210
       PROJECTION_METHOD = Dialog.getChoice();
211
}