-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscale_width.ijm
216 lines (131 loc) · 5.02 KB
/
scale_width.ijm
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
//==============================================
//==============================================
//batch automeasure greatest horizontal widths of fish scales
//Lin Yangchen
//Centre for Bioimaging Sciences
//National University of Singapore
//December 2024
//==============================================
//==============================================
//NOTES FOR USER
//image file(s) should have TIF or tif extension
//only one scale should be visible in the image
//scale should already be oriented vertically
//scale should be stained magenta against bright neutral-grey background
//unstained regions inside scale boundary will not affect measurements
//More than one row of pixels may give the same maximal width measurement.
//The program uses the last such row.
//fine-tune these parameters if necessary
//for a given pixel, green value should be smaller than this percentage
//of red value for the pixel to be considered as part of the scale
green = 0.8;
//for a given pixel, red value should be larger than this
//to exclude dirt spots in the image
red = 100;
//==============================================
//prompt user to select folder containing images
dir = getDirectory("choose folder");
//replace backslashes with forward slashes (if using Windows)
dir = replace(dir, "\\", "/");
//remove spaces and illegal characters in file names
run("Fix Funny Filenames", "which="+dir);
//get the list of files in the folder
filelist = getFileList(dir);
//create subfolder for results
output = dir + "results/";
File.makeDirectory(output);
//==============================================
setBatchMode(true); //do not display images during execution
//table for storing scale widths
Table.create("scalewidths");
//array to temporarily store R, G values of each pixel
chvals = newArray(2);
count = 0;
for (i = 0; i < filelist.length; i++)
{
if (endsWith(filelist[i], ".TIF") || endsWith(filelist[i], ".tif"))
{
count = count + 1;
print("processing " + filelist[i] + " (image " + count + ")");
open(dir + filelist[i]);
filename = File.nameWithoutExtension;
//extract pixel dimensions
h = getHeight;
w = getWidth;
//duplicate image for processing
run("Duplicate...", " ");
//separate into R, G, B channels
run("RGB Stack");
//for each row
//arrays to store data (0-indexing)
row_widths = newArray(); //width of scale in each row
row_nums = newArray(); //corresponding row number
firstpix = newArray(); //position of first scale pixel
lastpix = newArray(); //position of last scale pixel
for (r = 0; r < h; r++)
{
//array for pixel positions satisfying threshold
//percentage green channel to be identified as scale
pixpos = newArray(); //0-indexing
//for each pixel in the row
for (p = 0; p < w; p++)
{
//find pixel values of R and G channels
for (c = 1; c <= 2; c++)
{
Stack.setChannel(c); //select channel (1-indexing)
val = getPixel(p,r);
chvals[c-1] = val; //0-indexing
if(c == 1)
{
redval = val; //for checking red pixel value to exclude dirt
}
} //end of channel loop
//calculate whether pixel is part of scale
percent_green = chvals[1]/chvals[0];
//if criteria satisfied, append pixel position
if(percent_green < green && redval > red)
{
pixpos = Array.concat(pixpos, p);
}
} //end of pixel loop
//if length of pixpos is not 0 (row contains scale pixels)
if(pixpos.length > 0)
{
//for this row, find min/max pixel positions and
Array.getStatistics(pixpos, pixmin, pixmax, mean, std);
firstpix = Array.concat(firstpix, pixmin); //record position of first scale pixel
lastpix = Array.concat(lastpix, pixmax); //record position of last scale pixel
//subtract the two values to get scale width
row_widths = Array.concat(row_widths, pixmax - pixmin);
//record corresponding row number
row_nums = Array.concat(row_nums, r);
} //end of if statement excluding empty rows
} //end of row loop
//sort all arrays according to row_widths
Array.sort(row_widths, row_nums, firstpix, lastpix);
//max scale width
maxwidth = row_widths[row_widths.length - 1]; //last element of sorted array
//optional code to convert width in pixel units to actual width
//(not implemented)
//corresponding row number
maxrow = row_nums[row_nums.length - 1];
//append scale width to table
selectWindow("scalewidths");
Table.set("image", i, filelist[i]);
Table.set("scalewidth", i, maxwidth);
//overlay measurement line on image
selectImage(filelist[i]);
makeLine(firstpix[firstpix.length - 1], maxrow, lastpix[lastpix.length - 1], maxrow);
run("Flatten");
saveAs("Jpeg", output + filename + "_width_line.jpg");
run("Collect Garbage"); //clear memory
} //end of if statement for file format
} //end of filelist loop
//==============================================
//save csv of scale widths
selectWindow("scalewidths");
saveAs("Results", output + "scalewidths.csv");
//if you want to close table window automatically
//selectWindow("scalewidths.csv"); run("Close");
print("yahoo!!! job finished");