Debian Codegraph @

docs/source/examples/multipleROIs.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 """Copyright © 2014 - 2021 German Neuroinformatics Node (G-Node)
5
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted under the terms of the BSD License. See
10 LICENSE file in the root of the Project.
11
12 Author: Jan Grewe <[email protected]>
13
14 See https://github.com/G-node/nix/wiki for more information.
15
16 We use the "Lenna" image in this tutorial.
17
18 "Lenna" by Original full portrait: "Playmate of the Month". Playboy
19 Magazine. November 1972, photographed by Dwight Hooker.This 512x512
20 electronic/mechanical scan of a section of the full portrait:
21 Alexander Sawchuk and two others[1] - The USC-SIPI image
22 database. Via Wikipedia -
23 http://en.wikipedia.org/wiki/File:Lenna.png#mediaviewer/File:Lenna.png
24
25 """
26
27 import nixio
28 import numpy as np
29 from PIL import Image as img
30 import matplotlib.pyplot as plt
31
32 import docutils
33
34
35 def load_image():
36 image = img.open('lenna.png')
37 pix = np.array(image)
38 channels = list(image.mode)
39 return pix, channels
40
41
42 def draw_rect(img_data, position, extent):
43 img_data[position[0]:position[0] + extent[0], position[1], :] = 255
44 img_data[position[0]:position[0] + extent[0], position[1] + extent[1], :] = 255
45 img_data[position[0], position[1]:position[1] + extent[1], :] = 255
46 img_data[position[0] + extent[0], position[1]:position[1] + extent[1], :] = 255
47 return img_data
48
49
50 def plot_data(tag):
51 data_array = tag.references[0]
52 positions = tag.positions
53 img_data = data_array[:]
54 img_data = np.array(img_data, dtype='uint8')
55 positions_data = tag.positions[:]
56 extents_data = tag.extents[:]
57 for i in range(positions.data_extent[0]):
58 img_data = draw_rect(img_data, positions_data[i, :], extents_data[i, :])
59 # new_img = img.fromarray(img_data)
60 plt.imshow(img_data)
61 plt.gcf().set_size_inches((5.5, 5.5))
62 if docutils.is_running_under_pytest():
63 plt.close()
64 else:
65 # plt.savefig("../images/multiple_rois.png")
66 plt.show()
67
68
69 def plot_roi_data(tag):
70 position_count = tag.positions.shape[0]
71
72 fig = plt.figure(figsize=(5.5, 5.5))
73 for p in range(position_count):
74 roi_data = tag.tagged_data(p, "lenna")[:]
75 roi_data = np.array(roi_data, dtype='uint8')
76 ax = fig.add_subplot(position_count, 1, p + 1)
77 image = img.fromarray(roi_data)
78 ax.imshow(image)
79
80 if docutils.is_running_under_pytest():
81 plt.close()
82 else:
83 # fig.savefig('../images/retrieved_rois.png')
84 plt.show()
85
86
87 def main():
88 img_data, channels = load_image()
89 # create a new file overwriting any existing content
90 file_name = 'multiple_roi.nix'
91 file = nixio.File.open(file_name, nixio.FileMode.Overwrite)
92
93 # create a 'Block' that represents a grouping object. Here, the recording session.
94 # it gets a name and a type
95 block = file.create_block("block name", "nix.session")
96
97 # create a 'DataArray' to take the sinewave, add some information about
98 # the signal
99 data = block.create_data_array("lenna", "nix.image.rgb", data=img_data)
100 # add descriptors for width, height and channels
101 data.append_sampled_dimension(1, label="height")
102 data.append_sampled_dimension(1, label="width")
103 data.append_set_dimension(labels=channels)
104
105 num_regions = 3
106 num_dimensions = len(data.dimensions)
107 roi_starts = np.zeros((num_regions, num_dimensions), dtype=int)
108 roi_starts[0, :] = [250, 245, 0]
109 roi_starts[1, :] = [250, 315, 0]
110 roi_starts[2, :] = [340, 260, 0]
111
112 roi_extents = np.zeros((num_regions, num_dimensions), dtype=int)
113 roi_extents[0, :] = [30, 45, 3]
114 roi_extents[1, :] = [30, 40, 3]
115 roi_extents[2, :] = [25, 65, 3]
116
117 # create the positions DataArray
118 positions = block.create_data_array("ROI positions", "nix.positions", data=roi_starts)
119 positions.append_set_dimension() # these can be empty
120 positions.append_set_dimension()
121
122 # create the extents DataArray
123 extents = block.create_data_array("ROI extents", "nix.extents", data=roi_extents)
124 extents.append_set_dimension()
125 extents.append_set_dimension()
126
127 # create a MultiTag
128 multi_tag = block.create_multi_tag("Regions of interest", "nix.roi", positions)
129 multi_tag.extents = extents
130 multi_tag.references.append(data)
131
132 # let's plot the data from the stored information
133 plot_data(multi_tag)
134 plot_roi_data(multi_tag)
135 file.close()
136
137
138 if __name__ == '__main__':
139 main()