# Install dependencies for this example
# Note: This does not include itkwidgets, itself
import sys
!{sys.executable} -m pip install itk-io requests tqdm
import requests
from tqdm import tqdm_notebook as tqdm
import os
import numpy as np
import itk
from itkwidgets import view
import itkwidgets
def download_data(url, filename):
if not os.path.exists(filename):
chunk_size = 32 * 1024
r = requests.get(url, stream=True)
total_size = int(r.headers.get('content-length', 0))
pbar = tqdm(unit="B", unit_scale=True, total=int(total_size))
with open(filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=chunk_size):
if chunk: # filter out keep-alive new chunks
pbar.update(len(chunk))
f.write(chunk)
# Download example data
# Source: https://cvlab.epfl.ch/data/em
#
# 1024x768x165 unsigned char, 124MB file
large_url = 'https://documents.epfl.ch/groups/c/cv/cvlab-unit/www/data/%20ElectronMicroscopy_Hippocampus/training.tif'
large_filename = 'large_hippocampus.tif'
# 2048x1536x1065 unsigned char, 3.2GB file
very_large_url = 'https://documents.epfl.ch/groups/c/cv/cvlab-unit/www/data/%20ElectronMicroscopy_Hippocampus/volumedata.tif'
very_large_filename = 'very_large_hippocampus.tif'
download_data(large_url, large_filename)
This function will be removed in tqdm==5.0.0 Please use `tqdm.notebook.tqdm` instead of `tqdm.tqdm_notebook`
HBox(children=(FloatProgress(value=0.0, max=129919357.0), HTML(value='')))
%time large_image = itk.imread(large_filename)
CPU times: user 450 ms, sys: 43.5 ms, total: 493 ms Wall time: 583 ms
%time view(large_image, select_roi=True, gradient_opacity=0.4, shadow=False, cmap=itkwidgets.cm.gray, annotations=False)
CPU times: user 1.58 s, sys: 256 ms, total: 1.84 s Wall time: 3.55 s
Viewer(annotations=False, cmap='gray_Matlab', geometries=[], gradient_opacity=0.4, point_sets=[], rendered_ima…
Next, pass select_roi=True
when creating the view, or click the crop button in the widget user interface. Adjust the location of the region of interest (roi) by clicking and dragging the corner of the cropping box.
Large images are decimated for visualization; higher resolutions are presented as the roi shrinks.
%time view(large_image, select_roi=False, mode='z', gradient_opacity=0.4, shadow=False, cmap=itkwidgets.cm.gray, annotations=False)
CPU times: user 343 ms, sys: 123 ms, total: 465 ms Wall time: 162 ms
Viewer(annotations=False, cmap='gray_Matlab', geometries=[], gradient_opacity=0.4, mode='z', point_sets=[], re…
Alternatively, set select_roi
to False
and the mode
to x
, y
, or z
. When zooming, the region of interest will shrink around the viewed area.
download_data(very_large_url, very_large_filename)
This function will be removed in tqdm==5.0.0 Please use `tqdm.notebook.tqdm` instead of `tqdm.tqdm_notebook`
HBox(children=(FloatProgress(value=0.0, max=3353674357.0), HTML(value='')))
%time very_large_image = itk.imread(very_large_filename)
%time view(very_large_image, gradient_opacity=0.4, shadow=False, cmap=itkwidgets.cm.gray, annotations=False)
To change the resolution of the rendered image, use the size_limit_3d
keyword.
size_limit_3d = [128, 128, 128]
%time view(very_large_image, size_limit_3d=size_limit_3d, gradient_opacity=0.3, shadow=False, cmap=itkwidgets.cm.gray, annotations=False)