-
Notifications
You must be signed in to change notification settings - Fork 32
/
images.py
141 lines (108 loc) · 4.03 KB
/
images.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
139
140
141
from numpy import ndarray, asarray
from lightning.types.base import Base
from lightning.types.decorators import viztype
from lightning.types.utils import add_property, array_to_im, polygon_to_points, polygon_to_mask
@viztype
class Image(Base):
_name = 'image'
@staticmethod
def clean(imagedata):
"""
Display an array as an image.
.. image:: image.png
Parameters
----------
imagedata : array-like
Image as a two dimensional (grayscale) or three dimensional (RGB) array.
"""
if asarray(imagedata).ndim not in set((2, 3)):
raise Exception("Input must be two or three dimensional")
outdict = [array_to_im(imagedata)]
return {'images': outdict}
@viztype
class ImagePoly(Base):
_name = 'image-poly'
_func = 'imagepoly'
@staticmethod
def clean(imagedata, coordinates=None, xy=None):
"""
Display an array as an image with polygonal regions and region drawing.
.. image:: image.png
Parameters
----------
imagedata : array-like
Image as a two dimensional (grayscale) or three dimensional (RGB) array.
coordinates : array-like
List of coordinates or list of list of coordinates. Assumes array indexing
(i.e. row/column), in the form [[r,c],[r,c]] for one region or
[[[r0,c0],[r0,c0]], [[r1,c1],[r1,c1]]] for multiple regions
xy : boolean, optional, default = None
Only if True treat coordinates as x/y positions instead of row/column indices
"""
if asarray(imagedata).ndim not in set((2, 3)):
raise Exception("Input must be two or three dimensional")
imgs = [array_to_im(imagedata)]
outdict = {'images': imgs}
outdict = add_property(outdict, coordinates, 'coordinates', xy=xy)
return outdict
@property
def _coords(self):
"""
Coordinates of regions retrieved from visualization user data.
"""
user_data = self.get_user_data()['settings']
if 'coords' in user_data.keys():
return user_data['coords']
else:
return []
@property
def polygons(self):
"""
Coordinates of polygons as drawn on an image.
These coordinates can be drawn directly to an image using lighting.imagepoly
and should be in the exact same locatinos as they were drawn.
"""
coords = self._coords
# convert from x/y to row/column indexing
polygons = map(lambda b: asarray(b)[:, ::-1].tolist(), coords)
return polygons
def points(self, z=None):
"""
Points contained in regions drawn on an image
Parameters
----------
z : int, optiona, default=None
Append a z-index to coordinates (yielding three dimensional coordinates)
"""
coords = self._coords
return [polygon_to_points(x, z) for x in coords]
def masks(self, dims, z=None):
"""
Binary masks with regions filled in as 1s or 0s.
Parameters
----------
dims : array-like
Specify the dimensions of the image containing the polygons
z : int, optional, default=None
Use a z-index to insert regions into the appropriate slice if using
three-dimensional volumes.
"""
coords = self._coords
return [polygon_to_mask(x, dims, z) for x in coords]
@viztype
class Gallery(Base):
_name = 'gallery'
@staticmethod
def clean(imagedata):
"""
Display a collection of arrays as browsable images with thumbnails.
.. image:: gallery.png
Parameters
----------
imagedata : array-like, or list of array-like
Image or list of images as two dimensional (grayscale) or three dimensional (RGB) arrays.
"""
if isinstance(imagedata, ndarray):
imagedata = [imagedata]
outdict = [array_to_im(im) for im in imagedata]
return {'images': outdict}