|
1
|
|
|
from niprov.dependencies import Dependencies |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class Camera(object): |
|
5
|
|
|
|
|
6
|
|
|
def __init__(self, dependencies): |
|
7
|
|
|
self.film = dependencies.getPictureCache() |
|
8
|
|
|
self.libs = dependencies.getLibraries() |
|
9
|
|
|
|
|
10
|
|
|
def saveSnapshot(self, data, for_): |
|
11
|
|
|
"""Plot an overview of the image and store it. |
|
12
|
|
|
|
|
13
|
|
|
Uses :class:`.PictureCache` as service that provides a file-like |
|
14
|
|
|
handle to save the plotted picture to. |
|
15
|
|
|
Calls takeSnapshot() to do the actual plotting. |
|
16
|
|
|
|
|
17
|
|
|
Args: |
|
18
|
|
|
data (numpy.ndarray): Array of 2, 3 or 4 dimensions with image data. |
|
19
|
|
|
""" |
|
20
|
|
|
newPicture = self.film.new() |
|
21
|
|
|
self.takeSnapshot(data, on=newPicture) |
|
22
|
|
|
self.film.keep(newPicture, for_) |
|
23
|
|
|
|
|
24
|
|
|
def takeSnapshot(self, data, on): |
|
25
|
|
|
"""Plot an overview of the image using matplotlib.pyplot. |
|
26
|
|
|
|
|
27
|
|
|
Args: |
|
28
|
|
|
data (numpy.ndarray): Array of 2, 3 or 4 dimensions with image data. |
|
29
|
|
|
on (str or file-like object): Where to save figure to. |
|
30
|
|
|
""" |
|
31
|
|
|
plt = self.libs.pyplot |
|
32
|
|
|
|
|
33
|
|
|
## 3D |
|
34
|
|
|
ndims = len(data.shape) |
|
35
|
|
|
sliceOrder = [1, 0, 2] |
|
36
|
|
|
|
|
37
|
|
|
fig, axs = plt.subplots(nrows=1, ncols=ndims, figsize=(8, 3), dpi=100) |
|
38
|
|
|
|
|
39
|
|
|
for d in range(ndims): |
|
40
|
|
|
slicing = [slice(None)]*ndims |
|
41
|
|
|
slicing[sliceOrder[d]] = int(data.shape[d]/2) |
|
42
|
|
|
axs[d].matshow(data[slicing].T, origin='lower', |
|
43
|
|
|
cmap = plt.get_cmap('gray'), vmin = 0, vmax = data.max()) |
|
44
|
|
|
axs[d].locator_params(nbins=3) |
|
45
|
|
|
axs[d].tick_params(axis='both', which='major', labelsize=8) |
|
46
|
|
|
|
|
47
|
|
|
plt.tight_layout() |
|
48
|
|
|
|
|
49
|
|
|
plt.savefig(on) |
|
50
|
|
|
|
|
51
|
|
|
|