-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrain_size_lineal_intersection.ijm
More file actions
221 lines (117 loc) · 4.66 KB
/
grain_size_lineal_intersection.ijm
File metadata and controls
221 lines (117 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//==============================================
//Preprocess grain image for colour contrast, add random intersects and
//output grey values along intersect for each colour channel
//Lin Yangchen
//Centre for Bioimaging Sciences
//National University of Singapore
//7 March 2023
//==============================================
//detailed use case:
//https://www.linyangchen.com/Singapore-organ-pipe-microscopy
//You may have to manually click and run the median filter at the desired radius before running this script.
//Otherwise the median filter will run with the default radius even if the radius is set to a different value in the script.
//===============================================================
//function to generate randomly positioned and oriented lines
//spanning height and width of rectangular image
//===============================================================
function randomline(height, width, nlines){
apportion = nlines/2;
dim1 = floor(apportion);
dim2 = Math.ceil(apportion);
for(i = 0; i < dim1; i++){
makeLine(
0,
round(random() * h),
w,
round(random() * h)
);
roiManager("Add");
}
for(i = 0; i < dim2; i++){
makeLine(
round(random() * w),
0,
round(random() * w),
h
);
roiManager("Add");
}
}
//===============================================================
//function for extracting and saving profile data
//===============================================================
function extractprofile(input, output, overlays, filename, radius, scale){
open(input+filename);
run("Set Scale...", "distance=" + scale + " known=1 unit=micron");
scalebarwidth = 100;
scalebarthick = 2;
scalebarfont = 24;
//extract pixel dimensions
h = getHeight;
w = getWidth;
//random intersections
randomline(h, w, nlines);
roiManager("Save", overlays + filename + "_intersects.zip");
//lengths of intersections
run("Clear Results");
roiManager("multi-measure measure_all");
saveAs("Results", output + filename + "_intersect_lengths.csv");
//show intersects on micrograph
roiManager("Show All with labels");
run("Flatten");
run("Scale Bar...", "width=" + scalebarwidth + " thickness=" + scalebarthick +
" font=" + scalebarfont + " background=Black horizontal");
saveAs("Jpeg", overlays + filename + ".jpg");
//preprocessing
selectImage(filename);
run("Median...", "radius = rad");
run("Unsharp Mask...", "radius=1000 mask=0.5");
//show intersects on processed micrograph
run("Flatten");
run("Scale Bar...", "width=" + scalebarwidth + " thickness=" + scalebarthick +
" font=" + scalebarfont + " background=Black horizontal");
saveAs("Jpeg", overlays + "processed_" + filename + ".jpg");
//extract and save profile data for each channel and each line
colours = newArray("red", "green", "blue");
selectWindow(filename);
run("Split Channels");
for(i = 0; i < colours.length; i++){
selectWindow(filename + " (" + colours[i] + ")");
run("Enhance Contrast", "saturated=0");
for(j = 0; j < nlines; j++){
run("Clear Results");
roiManager("Select", j);
profile = getProfile();
for (k = 0; k < profile.length; k++){
setResult("Value", k, profile[k]);
}
updateResults;
linenum = j + 1;
saveAs("Results", output + filename + "_line" + linenum + "_" + colours[i] + "_ch.csv");
}
}
roiManager("Deselect");
roiManager("Delete");
close("*");
}
//===============================================================
//run analysis
//===============================================================
//set seed for pseudorandom number generator
random("seed", 73659);
//input and output directories
input = "/Users/yangchen/microscopy/metallography/organpipe/micrographs/";
output = "/Users/yangchen/microscopy/metallography/organpipe/data/";
overlays = "/Users/yangchen/microscopy/metallography/organpipe/overlays/";
filelist = getFileList(input);
radius = 50; //pixel radius for median filter
nlines = 50; //number of random lines to draw in each micrograph
scale = 0.5; //pixels per micron
//disable all other measurements when measuring length of intersect
run("Set Measurements...", " redirect=None decimal=2");
for (i = 0; i < filelist.length; i++){
extractprofile(input, output, overlays, filelist[i], radius, scale);
run("Collect Garbage");
}
selectWindow("Results"); run("Close");
close("ROI Manager");