.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_video.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_video.py: Transform video =============== In this example, we use ``torchio.Resample((2, 2, 1))`` to divide the spatial size of the clip (height and width) by two and ``RandomAffine(degrees=(0, 0, 20))`` to rotate a maximum of 20 degrees around the time axis. .. GENERATED FROM PYTHON SOURCE LINES 10-70 .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_video_001.png :alt: plot video :srcset: /auto_examples/images/sphx_glr_plot_video_001.png :class: sphx-glr-multi-img * .. container:: sphx-glr-animation .. raw:: html
* .. container:: sphx-glr-animation .. raw:: html
.. code-block:: Python import matplotlib.animation as animation import matplotlib.pyplot as plt import numpy as np import torch from PIL import Image import torchio as tio def read_clip(path, undersample=4): """Read a GIF a return an array of shape (C, W, H, T).""" gif = Image.open(path) frames = [] for i in range(gif.n_frames): gif.seek(i) frames.append(np.array(gif.convert('RGB'))) frames = frames[::undersample] array = np.stack(frames).transpose(3, 1, 2, 0) delay = gif.info['duration'] return array, delay def plot_gif(image): def _update_frame(num): frame = get_frame(image, num) im.set_data(frame) return def get_frame(image, i): return image.data[..., i].permute(1, 2, 0).byte() plt.rcParams['animation.embed_limit'] = 25 fig, ax = plt.subplots() im = ax.imshow(get_frame(image, 0)) return animation.FuncAnimation( fig, _update_frame, repeat_delay=image['delay'], frames=image.shape[-1], ) # Source: https://thehigherlearning.wordpress.com/2014/06/25/watching-a-cell-divide-under-an-electron-microscope-is-mesmerizing-gif/ array, delay = read_clip('nBTu3oi.gif') plt.imshow(array[..., 0].transpose(1, 2, 0)) plt.plot() image = tio.ScalarImage(tensor=array, delay=delay) original_animation = plot_gif(image) transform = tio.Compose( ( tio.Resample((2, 2, 1)), tio.RandomAffine(degrees=(0, 0, 20)), ) ) torch.manual_seed(0) transformed = transform(image) transformed_animation = plot_gif(transformed) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.285 seconds) .. _sphx_glr_download_auto_examples_plot_video.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_video.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_video.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_video.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_