| 1 |
|
| 2 |
|
| 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 |
|
| 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 |
|
| 58 |
new_img.show() |
| 59 |
|
| 60 |
|
| 61 |
def main(): |
| 62 |
img_data, channels = load_image() |
| 63 |
|
| 64 |
file_name = 'single_roi.nix' |
| 65 |
f = nix.File.open(file_name, nix.FileMode.Overwrite) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
block = f.create_block("block name", "nix.session") |
| 70 |
|
| 71 |
|
| 72 |
data = block.create_data_array("lenna", "nix.image.rgb", data=img_data) |
| 73 |
|
| 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 |
|
| 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 |
|
| 86 |
plot_data(tag) |
| 87 |
f.close() |
| 88 |
|
| 89 |
|
| 90 |
if __name__ == '__main__': |
| 91 |
main() |