Révision 3
Fibril_Tool_Batch.ijm (revision 3) | ||
---|---|---|
1 |
////////////////////////////////////////////////////////////////////////////////// |
|
2 |
///////////////////////Automation of FibrilTool macro///////////////////////////// |
|
3 |
////////////////////////Marion Louveaux, PhD student////////////////////////////// |
|
4 |
////////////////////////////////////////////////////////////////////////////////// |
|
5 |
|
|
6 |
//This macro is an automated version of FibrilTool macro, developped by Arezki Boudaoud (see 2014, Boudaoud, Nature Protocols) |
|
7 |
//Put .png and RoiSet.zip in a unique directory (as much as you want) = "dir" |
|
8 |
// .png and RoiSet.zip must share the same name: ImgName.png and ImgName_RoiSet.zip |
|
9 |
//One RoiSet.zip = one .png |
|
10 |
//FibrilTool is executed on each .roi of the RoiSet |
|
11 |
//Results, raw data (new RoiSet) and Flattened images are saved in dir |
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
macro "Fibril Tool - C059L11eeL14beL41ebL178eL71e8L1a5eLa1e5" { |
|
16 |
|
|
17 |
dir = getDirectory("Choisir un repertoire de Projections") |
|
18 |
|
|
19 |
///Ask the user about fibril tool parameters |
|
20 |
Dialog.create("Fibril Tool"); |
|
21 |
Dialog.addMessage("Choose the channel\n(Red/Green/Blue)"); |
|
22 |
Dialog.addChoice("Channel for fibrils?\t", newArray("G", "R", "B")); |
|
23 |
Dialog.addChoice("Channel for drawing\t", newArray( "R", "G", "B")); |
|
24 |
Dialog.addNumber("Multiply linelength by\t", 1); |
|
25 |
Dialog.show(); |
|
26 |
fib = Dialog.getChoice(); |
|
27 |
drw = Dialog.getChoice(); |
|
28 |
norm_constant = Dialog.getNumber(); // scaling factor for drawing of segments |
|
29 |
|
|
30 |
|
|
31 |
///compute MTs orientation for each projection of the directory |
|
32 |
list = getFileList(dir); |
|
33 |
|
|
34 |
|
|
35 |
for (j=0; j<list.length; j++){ |
|
36 |
//print("entree dans la boucle 2"); |
|
37 |
if(endsWith (list[j], "png")){ |
|
38 |
//print("path_fichier",dir+list[j]); |
|
39 |
open( dir+File.separator+list[j] ); |
|
40 |
nom2=substring(list[j],0,indexOf(list[j],".png")); |
|
41 |
open( dir+File.separator+nom2+"_RoiSet.zip" ); |
|
42 |
|
|
43 |
//print("nom_fichier_tif",nom2); |
|
44 |
|
|
45 |
|
|
46 |
// the log output gives the average properties of the region |
|
47 |
// 0) image title |
|
48 |
// cell number |
|
49 |
// 1) x-coordinate of region centroid (scaled) |
|
50 |
// y-coordinate of region centroid (scaled) |
|
51 |
// area (scaled) |
|
52 |
// 2) nematic tensor |
|
53 |
// average orientation (angle in -90:90 in degrees) |
|
54 |
// quality of the orientation (score between 0 and 1) |
|
55 |
// The results are drawn on an overlay |
|
56 |
// 3) coordinates of polygon vertices for record |
|
57 |
|
|
58 |
|
|
59 |
// numbering of cells |
|
60 |
var num; |
|
61 |
|
|
62 |
var pi = 3.14159265; |
|
63 |
|
|
64 |
|
|
65 |
// |
|
66 |
|
|
67 |
id = getImageID(); |
|
68 |
title = getTitle(); |
|
69 |
|
|
70 |
getPixelSize(unit,pixelWidth,pixelHeight); |
|
71 |
if (pixelWidth != pixelHeight) exit("Rectangular pixels!"); |
|
72 |
scale = pixelWidth; |
|
73 |
|
|
74 |
//setBatchMode(true); |
|
75 |
|
|
76 |
//properties of selection |
|
77 |
num++ ; |
|
78 |
selectImage(id); |
|
79 |
|
|
80 |
///ROI selection |
|
81 |
n = roiManager("count"); |
|
82 |
for (cell=0; cell<n; cell++) { |
|
83 |
roiManager("select", cell); |
|
84 |
|
|
85 |
getSelectionCoordinates(vertx, verty); |
|
86 |
c = polygonCentre(vertx,verty); |
|
87 |
c0s = c[0]*scale ; |
|
88 |
c1s = c[1]*scale ; |
|
89 |
getRawStatistics(area); |
|
90 |
areas = area*scale*scale; |
|
91 |
pr = 2; |
|
92 |
sortie = title+"\t"+cell; |
|
93 |
sortie = sortie+"\t"+d2s(c0s,pr)+"\t"+d2s(c1s,pr)+"\t"+d2s(areas,pr); |
|
94 |
|
|
95 |
//extract fibril signal |
|
96 |
selectImage(id); |
|
97 |
run("Duplicate...", "title=Temp"); |
|
98 |
run("Crop"); |
|
99 |
getSelectionCoordinates(vertxloc, vertyloc); |
|
100 |
if (fib == "R") setRGBWeights(1,0,0); |
|
101 |
else if (fib == "G") setRGBWeights(0,1,0); |
|
102 |
else if (fib =="B") setRGBWeights(0,0,1); |
|
103 |
else exit("Fibril color undefined"); |
|
104 |
run("8-bit"); |
|
105 |
|
|
106 |
|
|
107 |
//compute x-gradient in "x" |
|
108 |
selectWindow("Temp"); |
|
109 |
run("Duplicate...","title=x"); |
|
110 |
run("32-bit"); |
|
111 |
run("Translate...", "x=-0.5 y=0 interpolation=Bicubic"); |
|
112 |
run ("Duplicate...","title=x1"); |
|
113 |
run("Translate...", "x=1 y=0 interpolation=None"); |
|
114 |
imageCalculator("substract","x","x1"); |
|
115 |
selectWindow("x1"); |
|
116 |
close(); |
|
117 |
|
|
118 |
//compute y-gradient in "y" |
|
119 |
selectWindow("Temp"); |
|
120 |
run ("Duplicate...","title=y"); |
|
121 |
run("32-bit"); |
|
122 |
run("Translate...", "x=0 y=-0.5 interpolation=Bicubic"); |
|
123 |
run ("Duplicate...","title=y1"); |
|
124 |
run("Translate...", "x=0 y=1 interpolation=None"); |
|
125 |
imageCalculator("substract","y","y1"); |
|
126 |
selectWindow("y1"); |
|
127 |
close(); |
|
128 |
|
|
129 |
|
|
130 |
//compute norm of gradient in "g" |
|
131 |
selectWindow("x"); |
|
132 |
run("Duplicate...","title=g"); |
|
133 |
imageCalculator("multiply","g","x"); |
|
134 |
selectWindow("y"); |
|
135 |
run("Duplicate...","title=gp"); |
|
136 |
imageCalculator("multiply","gp","y"); |
|
137 |
imageCalculator("add","g","gp"); |
|
138 |
selectWindow("gp"); |
|
139 |
close(); |
|
140 |
selectWindow("g"); |
|
141 |
w = getWidth(); h = getHeight(); |
|
142 |
for (y=0; y<h; y++) { |
|
143 |
for (x=0; x<w; x++){ |
|
144 |
setPixel(x, y, sqrt( getPixel(x, y))); |
|
145 |
} |
|
146 |
} |
|
147 |
//set the effect of the gradient to 1/255 when too low ; threshold = 2 |
|
148 |
selectWindow("g"); |
|
149 |
for (y=0; y<h; y++) { |
|
150 |
for (x=0; x<w; x++){ |
|
151 |
if (getPixel(x,y) < 2) |
|
152 |
setPixel(x, y, 255); |
|
153 |
} |
|
154 |
} |
|
155 |
|
|
156 |
//normalize "x" and "y" to components of normal |
|
157 |
imageCalculator("divide","x","g"); |
|
158 |
imageCalculator("divide","y","g"); |
|
159 |
|
|
160 |
|
|
161 |
//compute nxx |
|
162 |
selectWindow("x"); |
|
163 |
run("Duplicate...","title=nxx"); |
|
164 |
imageCalculator("multiply","nxx","x"); |
|
165 |
//compute nxy |
|
166 |
selectWindow("x"); |
|
167 |
run("Duplicate...","title=nxy"); |
|
168 |
imageCalculator("multiply","nxy","y"); |
|
169 |
//compute nyy |
|
170 |
selectWindow("y"); |
|
171 |
run("Duplicate...","title=nyy"); |
|
172 |
imageCalculator("multiply","nyy","y"); |
|
173 |
|
|
174 |
//closing |
|
175 |
selectWindow("Temp"); |
|
176 |
close(); |
|
177 |
selectWindow("x"); |
|
178 |
close(); |
|
179 |
selectWindow("y"); |
|
180 |
close(); |
|
181 |
selectWindow("g"); |
|
182 |
close(); |
|
183 |
|
|
184 |
//averaging nematic tensor |
|
185 |
selectWindow("nxx"); |
|
186 |
makeSelection("polygon",vertxloc,vertyloc); |
|
187 |
getRawStatistics(area,xx); |
|
188 |
selectWindow("nxx"); |
|
189 |
close(); |
|
190 |
|
|
191 |
selectWindow("nxy"); |
|
192 |
makeSelection("polygon",vertxloc,vertyloc); |
|
193 |
getRawStatistics(area,xy); |
|
194 |
selectWindow("nxy"); |
|
195 |
close(); |
|
196 |
|
|
197 |
selectWindow("nyy"); |
|
198 |
makeSelection("polygon",vertxloc,vertyloc); |
|
199 |
getRawStatistics(area,yy); |
|
200 |
selectWindow("nyy"); |
|
201 |
close(); |
|
202 |
|
|
203 |
//eigenvalues and eigenvector of texture tensor |
|
204 |
m = (xx + yy) / 2; |
|
205 |
d = (xx - yy) / 2; |
|
206 |
v1 = m + sqrt(xy*xy + d*d); |
|
207 |
v2 = m - sqrt(xy*xy + d*d); |
|
208 |
//direction |
|
209 |
tn = - atan((v2 - xx) / xy); |
|
210 |
//score |
|
211 |
scoren = abs((v1-v2) / 2 / m); |
|
212 |
|
|
213 |
//output |
|
214 |
tsn=tn*180/pi; |
|
215 |
// nematic tensor tensor |
|
216 |
sortie = sortie+"\t"+d2s(tsn,pr)+"\t"+d2s(scoren,2*pr); |
|
217 |
|
|
218 |
//polygon coordinates |
|
219 |
np = vertx.length; |
|
220 |
for (i=0; i<np; i++){ |
|
221 |
xp = vertx[i]; yp = verty[i]; |
|
222 |
sortie = sortie+"\t"+d2s(xp,pr)+"\t"+d2s(yp,pr); |
|
223 |
} |
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
// |
|
228 |
//print output |
|
229 |
print(sortie); |
|
230 |
|
|
231 |
|
|
232 |
// |
|
233 |
//drawing of directions and cell contour |
|
234 |
setBatchMode(false); |
|
235 |
selectImage(id); |
|
236 |
run("Add Selection...", "stroke=yellow width=1"); |
|
237 |
|
|
238 |
|
|
239 |
// drawing nematic tensor |
|
240 |
if ( drw != "No" ) { |
|
241 |
u1 = norm_constant*sqrt(area)*cos(tn)*scoren + c[0]; |
|
242 |
v1 = - norm_constant*sqrt(area)*sin(tn)*scoren + c[1]; |
|
243 |
u2 = - norm_constant*sqrt(area)*cos(tn)*scoren + c[0]; |
|
244 |
v2 = norm_constant*sqrt(area)*sin(tn)*scoren + c[1]; |
|
245 |
if (drw == "R") stroke = "red"; |
|
246 |
else if (drw == "G") stroke = "green"; |
|
247 |
else if (drw =="B") stroke = "blue"; |
|
248 |
else exit("Drawing color undefined"); |
|
249 |
makeLine(u1,v1,u2,v2); |
|
250 |
run("Add Selection...", "stroke="+stroke+" width=5"); |
|
251 |
} |
|
252 |
|
|
253 |
|
|
254 |
//print number at center |
|
255 |
selectImage(id); |
|
256 |
//makeText(num,c[0],c[1]); |
|
257 |
run("Add Selection...", "stroke="+stroke+" font=15 fill=none"); |
|
258 |
|
|
259 |
|
|
260 |
//restore original selection |
|
261 |
makeSelection("polygon",vertx,verty); |
|
262 |
|
|
263 |
setTool("polygon"); |
|
264 |
|
|
265 |
|
|
266 |
} //end of ROI selection |
|
267 |
|
|
268 |
selectWindow("ROI Manager"); |
|
269 |
run("Close"); //fermer le roi manager |
|
270 |
run("To ROI Manager"); |
|
271 |
roiManager("Save", dir+File.separator+nom2+"_RoiSet_MTs_contour.zip" ); |
|
272 |
roiManager("Show None"); |
|
273 |
roiManager("Show all without labels"); //OU roiManager("Show All"); |
|
274 |
|
|
275 |
//Identification of ROI of cell contour |
|
276 |
N = roiManager("count"); |
|
277 |
a1 = newArray(N/3); |
|
278 |
for (i=0; i<a1.length; i++){ |
|
279 |
a1[i] = i*3; |
|
280 |
} |
|
281 |
|
|
282 |
//Check indices of selected ROI |
|
283 |
//printArray(a1); |
|
284 |
|
|
285 |
|
|
286 |
//Delete ROI |
|
287 |
roiManager("select", a1); |
|
288 |
roiManager("Delete"); |
|
289 |
|
|
290 |
//Sauver les orientations des MTs |
|
291 |
roiManager("Save", dir+File.separator+nom2+"_RoiSet_MTs.zip" ); |
|
292 |
roiManager("Show all without labels"); |
|
293 |
run("Flatten"); //projeter les MTs sur le .png |
|
294 |
saveAs("Tiff", dir+File.separator+nom2+"_MTs.tif" ); |
|
295 |
run("Close"); //fermer le .tif |
|
296 |
|
|
297 |
newImage("Untitled", "RGB black", 1024, 1024, 1); //projeter les MTs sur fond noir |
|
298 |
roiManager("Show None"); |
|
299 |
roiManager("Show all without labels"); //OU roiManager("Show All"); |
|
300 |
run("Flatten"); |
|
301 |
saveAs("Tiff", dir+File.separator+nom2+"_MTs_blackBG.tif" ); |
|
302 |
close(); |
|
303 |
close(); |
|
304 |
close(); |
|
305 |
selectWindow("ROI Manager"); |
|
306 |
run("Close"); |
|
307 |
|
|
308 |
selectWindow("Log"); |
|
309 |
saveAs("text", dir+File.separator+nom2+"_Log.txt" ); |
|
310 |
run("Close"); |
|
311 |
|
|
312 |
} //end of if endswith .png |
|
313 |
|
|
314 |
|
|
315 |
|
|
316 |
//print("done"); |
|
317 |
|
|
318 |
} // end of for Projections |
|
319 |
|
|
320 |
|
|
321 |
|
|
322 |
|
|
323 |
// centroid of a polygon |
|
324 |
function polygonCentre(x,y){ |
|
325 |
n =x.length; |
|
326 |
area1 = 0; |
|
327 |
xc = 0; yc = 0; |
|
328 |
for (i=1; i<n; i++){ |
|
329 |
inc = x[i-1]*y[i] - x[i]*y[i-1]; |
|
330 |
area1 += inc; |
|
331 |
xc += (x[i-1]+x[i])*inc; |
|
332 |
yc += (y[i-1]+y[i])*inc; |
|
333 |
} |
|
334 |
inc = x[n-1]*y[0] - x[0]*y[n-1]; |
|
335 |
area1 += inc; |
|
336 |
xc += (x[n-1]+x[0])*inc; |
|
337 |
yc += (y[n-1]+y[0])*inc; |
|
338 |
area1 *= 3; |
|
339 |
xc /= area1; |
|
340 |
yc /= area1; |
|
341 |
return newArray(xc,yc); |
|
342 |
} |
|
343 |
|
|
344 |
|
|
345 |
|
|
346 |
//distance between two points (x1,y1) et (x2,y2) |
|
347 |
function distance(x1,y1,x2,y2) { |
|
348 |
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); |
|
349 |
} |
|
350 |
|
|
351 |
|
|
352 |
function printArray(a) { |
|
353 |
print(""); |
|
354 |
for (i=0; i<a.length; i++) |
|
355 |
print(i+": "+a[i]); |
|
356 |
} |
|
357 |
|
|
358 |
|
|
359 |
|
Formats disponibles : Unified diff