-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathROI_Color_Coder.ijm
200 lines (180 loc) · 6.73 KB
/
ROI_Color_Coder.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
/* ROI_Color_Coder.ijm
* IJ BAR: https://github.com/tferr/Scripts#scripts
* http://imagejdocu.tudor.lu/doku.php?id=macro:roi_color_coder
*
* Colorizes ROIs by matching LUT indexes to measurements in the Results table. It is
* complementary to the ParticleAnalyzer (Analyze>Analyze Particles...), generating
* particle-size heat maps. Requires IJ 1.46h.
*
* Tiago Ferreira, v.5.2 2015.08.13
*/
// assess required conditions before proceeding
requires("1.46h");
if (roiManager("count")==0 || nResults==0) {
showMessageWithCancel("No images open or the ROI Manager is empty...\n"
+ "Run demo? (Results Table and ROI Manager will be cleared)");
run("Blobs (25K)");
setThreshold(126, 255);
run("Analyze Particles...", "display clear add");
resetThreshold();
}
setBatchMode(true);
nROIs= roiManager("count");
nRES= nResults;
// if no measurements exist, measure existing ROIs
if (nRES==0) {
roiManager('Deselect');
roiManager('Measure');
nRES= nResults;
}
// create the dialog prompt
Dialog.create("ROI Color Coder: "+ getTitle);
headings= split(String.getResultsHeadings);
Dialog.addChoice("Measurement:", headings, "Area");
luts= getLutList();
Dialog.addChoice("LUT:", luts, luts[1]);
Dialog.setInsets(12, 0, 0);
Dialog.addMessage("Contours:");
Dialog.addNumber("Width:", 0, 0, 4, "pixels (0 to fill ROIs)");
Dialog.addSlider("Opacity (%):", 0, 100, 80);
Dialog.setInsets(12, 0, 0);
Dialog.addMessage("Legend:");
getPixelSize(unit, null, null);
Dialog.addString("Label:", unit+"^2", 10);
Dialog.addString("Range:", "Min-Max", 10);
Dialog.setInsets(-35, 213, 0);
Dialog.addMessage("(e.g., 10-100)");
Dialog.addNumber("N. of labels:", 5, 0, 3, "(Defines major ticks interval)");
Dialog.addNumber("Decimal places:", 0, 0, 3, "(Scientific notation if negative)");
Dialog.addChoice("Ramp size:", newArray("128 pixels", "256 pixels", "512 pixels"), "256 pixels");
Dialog.addChoice("Font:", newArray("Monospaced", "SansSerif", "Serif"), "SansSerif");
Dialog.addNumber("Font_size:", 14, 0, 3, "pt");
Dialog.setInsets(-25, 200, 0);
Dialog.addCheckbox("Draw tick marks", true);
if (nROIs!=nRES)
Dialog.addMessage(nROIs +" ROI(s) in Manager, "+ nRES +" rows in Results table:\n"
+ abs(nROIs-nRES) +" item(s) will be ignored...");
Dialog.addHelp("http://imagejdocu.tudor.lu/doku.php?id=macro:roi_color_coder");
Dialog.show;
parameter= Dialog.getChoice;
lut= Dialog.getChoice;
stroke= Dialog.getNumber;
alpha= pad(toHex(255*Dialog.getNumber/100));
label= Dialog.getString;
range= split(Dialog.getString, "-");
if (range.length==1) {
min= NaN; max= parseFloat(range[0]);
} else {
min= parseFloat(range[0]); max= parseFloat(range[1]);
}
numLabels= Dialog.getNumber;
decPlaces= Dialog.getNumber;
rampH= parseFloat(Dialog.getChoice);
font= Dialog.getChoice;
fontS= Dialog.getNumber;
ticks= Dialog.getCheckbox;
// get id of image and number of ROIs to colorize
id= getImageID();
items= minOf(nROIs, nRES);
// get values for chosen parameter
values= newArray(items);
for (i=0; i<items; i++)
values[i]= getResult(parameter,i);
Array.getStatistics(values, minC, maxC);
if (isNaN(min)) min= minC;
if (isNaN(max)) max= maxC;
// make heat-map legend
rampW= rampH/8; canvasH= 3*fontS+rampH; canvasW= rampH/2; tickL= rampW/4;
getLocationAndSize(imgx, imgy, imgwidth, imgheight);
call("ij.gui.ImageWindow.setNextLocation", imgx+imgwidth, imgy);
newImage(parameter+" Ramp", "8-bit ramp", rampH, rampW, 1);
// load the LUT as a hexColor array
roiColors= loadLutColors(lut);
// continue the legend design
saveSettings;
setColor(0, 0, 0);
setBackgroundColor(255, 255, 255);
setLineWidth(1);
setFont(font, fontS, "antialiased");
run("RGB Color");
if (ticks) { // left & right borders
drawLine(0, 0, rampH, 0);
drawLine(0, rampW-1, rampH, rampW-1);
} else
drawRect(0, 0, rampH, rampW);
run("Rotate 90 Degrees Left");
run("Canvas Size...", "width="+ canvasW +" height="+ canvasH +" position=Center-Left");
// draw ticks and values
step= rampH;
if (numLabels>2)
step /= (numLabels-1);
for (i=0; i<numLabels; i++) {
yPos= round(fontS/2 + (rampH - i*step - 1))+fontS;
rampLabel= min + (max-min)/(numLabels-1) * i;
drawString( d2s(rampLabel,decPlaces), rampW+2, yPos+fontS/2);
if (ticks) {
drawLine(0, yPos, tickL, yPos); // left tick
drawLine(rampW-1-tickL, yPos, rampW-1, yPos); // right tick
}
}
// parse symbols in unit and draw final label below ramp
label= cleanLabel(label);
uW= minOf(getStringWidth(label), rampW);
drawString(label, (rampW-uW)/2, canvasH);
restoreSettings;
// iterate through the ROI Manager list and colorize ROIs
selectImage(id);
for (countNaN=0, i=0; i<items; i++) {
if (isNaN(values[i])) countNaN++;
if (values[i]<=min)
lutIndex= 0;
else if (values[i]>max)
lutIndex= 255;
else
lutIndex= round(values[i]*255/max);
roiManager("select", i);
if (stroke>0) {
roiManager("Set Line Width", stroke);
roiManager("Set Color", alpha+roiColors[lutIndex]);
} else
roiManager("Set Fill Color", alpha+roiColors[lutIndex]);
}
// display result
roiManager("Show all");
if (countNaN!=0)
print("\n>>>> ROI Color Coder:\n"
+ "Some values from the \""+ parameter +"\" column could not be retrieved.\n"
+ countNaN +" ROI(s) were labeled with a default color.");
setBatchMode("exit & display");
function getLutList() {
luts= getFileList(getDirectory("luts"));
for (i=0, count=0; i<luts.length; i++)
if (endsWith(luts[i], ".lut"))
count++;
list= newArray(count+3);
list[0]= "Fire"; list[1]= "Ice"; list[2]= "Spectrum";
for (i=0, j=3; i<luts.length; i++)
if (endsWith(luts[i], ".lut"))
list[j++]= substring(luts[i], 0, lengthOf(luts[i])-4);
return list;
}
function loadLutColors(lut) {
run(lut);
getLut(reds, greens, blues);
hexColors= newArray(256);
for (i=0; i<256; i++) {
r= toHex(reds[i]); g= toHex(greens[i]); b= toHex(blues[i]);
hexColors[i]= ""+ pad(r) +""+ pad(g) +""+ pad(b);
}
return hexColors;
}
function pad(n) {
n= toString(n); if (lengthOf(n)==1) n= "0"+n; return n;
}
function cleanLabel(string) {
string= replace(string, "\\^2", fromCharCode(178)); // superscript 2
string= replace(string, "\\^3", fromCharCode(179)); // superscript 3
string= replace(string, "(?<![A-Za-z0-9])u(?=m)", fromCharCode(181)); // micrometer units
string= replace(string, "\\b[aA]ngstrom\\b", fromCharCode(197)); // angstrom symbol
return string;
}