-
Notifications
You must be signed in to change notification settings - Fork 1
/
Scale_Bar_Tool.ijm
52 lines (44 loc) · 2.06 KB
/
Scale_Bar_Tool.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
// Kevin Terretaz
//adapted from Aleš Kladnik there https://forum.image.sc/t/automatic-scale-bar-in-fiji-imagej/60774
var REMOVE_SCALEBAR_TEXT = false;
macro "Live Scale Bar Tool - C000 T0508B T5508a Ta508r"{
getPixelSize(unit,w,h);
if (unit == "pixels") exit("Image not spatially calibrated");
bar_Length = 1; // initial scale bar length in measurement units
bar_Relative_Size = 0;
bar_Height = 0;
if (REMOVE_SCALEBAR_TEXT == true) text_Parameter = "hide";
else text_Parameter = "bold";
font_Size = minOf(Image.width, Image.height) / 15; // estimation of "good" font size
getCursorLoc(x2, y2, z2, flags2);
getCursorLoc(last_x, last_y, z, flags);
while (flags >= 16) { //left click
getCursorLoc(x, y, z, flags);
getDisplayedArea(area_x, area_y, width, height);
//if mouse moved
if (x != last_x || y != last_y) {
// approximate size of the scale bar relative to image width
bar_Relative_Size = round(((width-(x - area_x))/width) * 10);
// recursively calculate a 1-2-5-10-20... series
// 1-2-5 series is calculated by repeated multiplication with 2.3, rounded to one significant digit
for (i = 0; i < bar_Relative_Size; i++) {
magical_Formula = Math.pow( 10, (floor( Math.log10( abs(bar_Length * 2.3)))));
bar_Length = round( (bar_Length*2.3) / magical_Formula) * magical_Formula;
}
bar_Height = round(((height - (y - area_y)) / height) * Image.height/20);
//if size or height values changed from last loop, update scale bar
if (bar_Relative_Size != last_Size || bar_Height != last_Height)
run("Scale Bar...", "width=&bar_Length height=&bar_Height font=&font_Size color=White background=None location=[Lower Right] "+ text_Parameter +" overlay");
showStatus("height = "+bar_Height+ "px length = "+ bar_Length + unit);
bar_Length = 1;
}
//save changes
last_Size = bar_Relative_Size;
last_Height = bar_Height;
getCursorLoc(last_x, last_y, z, flags);
wait(10);
}
}
macro "Live Scale Bar Tool Options"{
REMOVE_SCALEBAR_TEXT = getBoolean("Remove text?", "Yes", "No");
}