-
Notifications
You must be signed in to change notification settings - Fork 3
/
napari_measure_widget.py
138 lines (125 loc) · 5.26 KB
/
napari_measure_widget.py
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
from __future__ import annotations
import math
from typing import TYPE_CHECKING
import napari
import numpy as np
from magicgui import magicgui
if TYPE_CHECKING:
import napari.layers
import napari.types
def measure_shape(
viewer: napari.Viewer, keybind: str = "m", overwrite: bool = False
) -> None:
# get top-most, visible image layer
img_layer = None
for layer in viewer.layers:
if isinstance(layer, napari.layers.Image) and layer.visible != 0:
img_layer = layer
if img_layer is None:
raise Exception("No image layer found; is needed for scale information")
# get img dimensions
px_size = img_layer.scale[-2:]
if viewer.scale_bar.unit is None:
units = "px"
elif viewer.scale_bar.unit == "um":
units = "µm"
else:
units = viewer.scale_bar.unit
ndim = len(img_layer.data.shape)
measure_layer = viewer.add_shapes(
None,
ndim=ndim,
edge_color="red",
edge_width=2,
scale=px_size,
name="Measure",
)
measure_layer.mode = "add_line"
# prep results table for the table widget
result_table = {
"Shape": [],
f"Length ({units})": [],
"Angle (°)": [],
f"Area ({units}\u00b2)": [],
}
@viewer.bind_key(keybind, overwrite=overwrite)
@magicgui(call_button=f"Measure ({keybind})", result_widget=True)
def measure_prop(viewer: napari.Viewer) -> magicgui.widgets.Table:
layer = measure_layer
if layer.data:
# Check if a shape is selected, if so use that shape
if layer.selected_data:
m_shape_vertices = layer.data[[*layer.selected_data][0]]
m_shape_type = layer.shape_type[[*layer.selected_data][0]]
else: # otherwise use the last shape
m_shape_vertices = layer.data[-1]
m_shape_type = layer.shape_type[-1]
if m_shape_type == "line":
length = math.dist(*m_shape_vertices) * px_size[-1]
result_table["Shape"].append("Line")
result_table[f"Length ({units})"].append(round(length))
result_table["Angle (°)"].append("N/A")
result_table[f"Area ({units}\u00b2)"].append("N/A")
elif m_shape_type == "rectangle":
area = (
math.dist(m_shape_vertices[0], m_shape_vertices[1])
* math.dist(m_shape_vertices[0], m_shape_vertices[3])
* px_size[-1] ** 2
)
result_table["Shape"].append("Rectangle")
result_table[f"Length ({units})"].append("N/A")
result_table["Angle (°)"].append("N/A")
result_table[f"Area ({units}\u00b2)"].append(round(area))
elif m_shape_type == "ellipse":
area = (
math.pi
* 0.5
* math.dist(m_shape_vertices[0], m_shape_vertices[1])
* 0.5
* math.dist(m_shape_vertices[0], m_shape_vertices[3])
) * px_size[-1] ** 2
result_table["Shape"].append("Ellipse")
result_table[f"Area ({units}\u00b2)"].append(round(area))
result_table[f"Length ({units})"].append("N/A")
result_table["Angle (°)"].append("N/A")
elif m_shape_type == "polygon":
i = np.arange(len(m_shape_vertices))
area = (
0.5
* np.abs(
np.sum(np.cross(m_shape_vertices[i - 1], m_shape_vertices[i]))
)
* px_size[-1] ** 2
)
result_table["Shape"].append("Polygon")
result_table[f"Area ({units}\u00b2)"].append(round(area))
result_table[f"Length ({units})"].append("N/A")
result_table["Angle (°)"].append("N/A")
elif m_shape_type == "path":
length = 0
for i in range(len(m_shape_vertices) - 1):
length += (
math.dist(m_shape_vertices[i], m_shape_vertices[i + 1])
* px_size[-1]
)
if m_shape_vertices.shape[0] == 3:
angle_rad = np.arccos(
np.inner(
m_shape_vertices[0] - m_shape_vertices[1],
m_shape_vertices[2] - m_shape_vertices[1],
)
/ (
np.linalg.norm(m_shape_vertices[1] - m_shape_vertices[0])
* np.linalg.norm(m_shape_vertices[2] - m_shape_vertices[1])
)
)
angle_deg = np.rad2deg(angle_rad)
result_table["Angle (°)"].append(round(angle_deg))
else:
result_table["Angle (°)"].append("N/A")
result_table["Shape"].append("Path")
result_table[f"Length ({units})"].append(round(length))
result_table[f"Area ({units}\u00b2)"].append("N/A")
return result_table
widget = measure_prop.show()
viewer.window.add_dock_widget(widget, name="Measure Shapes", area="bottom")