Debian Codegraph @

docs/source/examples/singleROI.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 """Copyright © 2014 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 We use the "Lenna" image in this tutorial.
15
16 "Lenna" by Original full portrait: "Playmate of the Month". Playboy
17 Magazine. November 1972, photographed by Dwight Hooker.This 512x512
18 electronic/mechanical scan of a section of the full portrait:
19 Alexander Sawchuk and two others[1] - The USC-SIPI image
20 database. Via Wikipedia -
21 http://en.wikipedia.org/wiki/File:Lenna.png#mediaviewer/File:Lenna.png
22
23 """
24
25 import nixio as nix
26 import numpy as np
27 from PIL import Image as img
28
29 import docutils
30
31
32 def load_image():
33 image = img.open('lenna.png')
34 pix = np.array(image)
35 channels = list(image.mode)
36 return pix, channels
37
38
39 def draw_rect(img_data, position, extent):
40 img_data[position[0]:position[0] + extent[0], position[1], :] = 255
41 img_data[position[0]:position[0] + extent[0], position[1] + extent[1], :] = 255
42 img_data[position[0], position[1]:position[1] + extent[1], :] = 255
43 img_data[position[0] + extent[0], position[1]:position[1] + extent[1], :] = 255
44
45
46 def plot_data(tag):
47 data_array = tag.references[0]
48 img_data = np.zeros(data_array.shape)
49 data_array.read_direct(img_data)
50 img_data = np.array(img_data, dtype='uint8')
51 # positions and extents are double by default, need to convert to int
52 pos = tuple(map(int, tag.position))
53 ext = tuple(map(int, tag.extent))
54 draw_rect(img_data, pos, ext)
55 new_img = img.fromarray(img_data)
56 if not docutils.is_running_under_pytest():
57 # new_img.save("../images/single_roi.png")
58 new_img.show()
59
60
61 def main():
62 img_data, channels = load_image()
63 # create a new file overwriting any existing content
64 file_name = 'single_roi.nix'
65 f = nix.File.open(file_name, nix.FileMode.Overwrite)
66
67 # create a 'Block' that represents a grouping object. Here, the recording session.
68 # it gets a name and a type
69 block = f.create_block("block name", "nix.session")
70
71 # create a 'DataArray' to take the sinewave, add some information about the signal
72 data = block.create_data_array("lenna", "nix.image.rgb", data=img_data)
73 # add descriptors for width, height and channels
74 data.append_sampled_dimension(1, label="height")
75 data.append_sampled_dimension(1, label="width")
76 data.append_set_dimension(labels=channels)
77
78 # create a Tag, position and extent must be 3-D since the data is 3-D
79 position = [250, 250, 0]
80 extent = [30, 100, 3]
81 tag = block.create_tag('Region of interest', 'nix.roi', position)
82 tag.extent = extent
83 tag.references.append(data)
84
85 # let's plot the data from the stored information
86 plot_data(tag)
87 f.close()
88
89
90 if __name__ == '__main__':
91 main()