Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 5506d55b authored by MALANDAIN Gregoire's avatar MALANDAIN Gregoire
Browse files

examples/image_visu.py: exemplify differences between 'XYZ' and 'ZYX' conventions

parent fbb59fad
Branches
Tags
No related merge requests found
......@@ -6,73 +6,106 @@ import os
from vt import vtImage
def plot_3D_array_z_slice(arr, z_slice=None, cmap='gray', title=""):
"""Plot a z-slice of a given 3D array.
def plot_2D_array(arr, cmap='gray', order='ZYX', title=""):
"""Plot a 2D array.
Parameters
----------
arr : np.ndarray
The array to represent.
z_slice : int, optional
The index of the z-sclice to represent.
cmap : str in {'gray', 'viridis'}, optional
Colormap name, all those known by matplolib works. By default use 'gray'.
title :
"""
import matplotlib.pyplot as plt
sh = arr.shape
if z_slice is None:
z_slice = sh[2]//2
plt.figure()
plt.imshow(arr[:, :, z_slice], cmap=cmap)
plt.suptitle(title)
plt.title("Z-slice: {}/{}".format(z_slice, sh[2]))
plt.xlabel('Y-axis')
plt.ylabel('X-axis')
if arr.flags.c_contiguous is True or order is 'ZYX':
import matplotlib.pyplot as plt
plt.figure()
plt.imshow(arr, cmap=cmap)
ltitle = title + " - ZYX-ordered"
elif arr.flags.f_contiguous is True or order is 'XYZ':
import matplotlib.pyplot as plt
plt.figure()
plt.imshow(np.transpose(arr), cmap=cmap)
ltitle = title + " - XYZ-ordered"
else:
print("do not know how to plot image")
return
plt.suptitle(ltitle)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.colorbar()
plt.show()
def plot_2D_array(arr, cmap='gray', title=""):
"""Plot a 2D array.
def plot_3D_array_z_slice(arr, z_slice=None, cmap='gray', order='ZYX', title=""):
"""Plot a z-slice of a given 3D array.
Parameters
----------
arr : np.ndarray
The array to represent.
z_slice : int, optional
The index of the z-sclice to represent.
cmap : str in {'gray', 'viridis'}, optional
Colormap name, all those known by matplolib works. By default use 'gray'.
title :
"""
import matplotlib.pyplot as plt
plt.figure()
plt.imshow(arr, cmap=cmap)
plt.suptitle(title)
plt.xlabel('Y-axis')
plt.ylabel('X-axis')
plt.colorbar()
plt.show()
if arr.ndim <= 1:
print("not an image")
elif arr.ndim == 2:
plot_2D_array(arr, cmap=cmap, title=title)
elif arr.ndim == 3:
if order is 'ZYX':
if z_slice is None:
z_slice = arr.shape[0] // 2
extarr = arr[z_slice, :, :]
elif order is 'XYZ':
if z_slice is None:
z_slice = arr.shape[2] // 2
extarr = arr[:, :, z_slice]
else:
print("do not know how to plot image")
return
plot_2D_array(extarr, cmap=cmap, order=order, title=title+" - "+str(z_slice)+"-slice")
else:
print("not an image")
# ------------------------------------------------------------------------------
# Test plotting function with a handmade 3D example:
# ------------------------------------------------------------------------------
def _plot_handmade_3D():
def _plot_handmade_3D(order='XYZ'):
xy_shape = 10
mid_xy = xy_shape//2
z_shape = 5
# - Create a 2D array with a diagonal at max value (255):
arr_2d = np.diag(np.repeat(255, xy_shape))
# - For all rows at middle column, replace by 200:
arr_2d[:, mid_xy] = 200
# - For all columns at middle row, replace by 100:
arr_2d[mid_xy, :] = 100
# - Repeat this 2D array `z_shape` times along new dimension to get a 3D array:
arr_3d = np.repeat(arr_2d[:, :, np.newaxis], z_shape, axis=2)
if order is 'XYZ':
# - Create a 2D array with a diagonal at max value (255):
arr_2d = np.diag(np.repeat(255, xy_shape))
# - For all rows at middle column, replace by 200:
arr_2d[:, mid_xy] = 200
# - For all columns at middle row, replace by 100:
arr_2d[mid_xy, :] = 100
# - Repeat this 2D array `z_shape` times along new dimension to get a 3D array:
arr_3d = np.repeat(arr_2d[:, :, np.newaxis], z_shape, axis=2)
lorder=order
else:
arr_2d = np.diag(np.repeat(255, xy_shape))
arr_2d[mid_xy, :] = 200
arr_2d[:, mid_xy] = 100
arr_3d = np.repeat(arr_2d[np.newaxis, :, :], z_shape, axis=0)
lorder = 'ZYX'
print("2D array is C-contiguous:", arr_2d.flags.c_contiguous)
print("3D array is C-contiguous:", arr_3d.flags.c_contiguous)
......@@ -81,14 +114,14 @@ def _plot_handmade_3D():
print("- type q to quit")
plot_3D_array_z_slice(arr_3d, cmap='viridis', title='Test array')
plot_3D_array_z_slice(arr_3d, cmap='viridis', order=order, title='Test array')
# ------------------------------------------------------------------------------
# Use plotting function with real data:
# ------------------------------------------------------------------------------
def _plot_example_data(img_name):
def _plot_example_data(img_name, order='ZYX'):
# Defines the image to read:
img_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "data", img_name))
......@@ -99,48 +132,64 @@ def _plot_example_data(img_name):
print("Image shape is: {}".format(sh))
# Extract the array of data:
arr = vt_im.copy_to_array().copy()
# arr = np.reshape(arr.ravel(), sh, order='C')
print("Array shape is: {}".format(arr.shape))
print("- type q to quit")
plot_3D_array_z_slice(arr, cmap='gray', title=img_name)
plot_3D_array_z_slice(arr, cmap='gray', order=order, title=img_name)
# ------------------------------------------------------------------------------
#
# ------------------------------------------------------------------------------
def _build_array(x, y, z=1, dtype=np.ubyte, order='C'):
def _build_array(x, y, z=1, dtype=np.ubyte, order='ZYX'):
a = None
if z == 1:
a = np.ndarray([y,x], dtype=dtype, order=order)
for y in range(a.shape[0]):
for x in range(a.shape[1]):
a[y][x] = y * a.shape[1] + x
elif z > 1:
a = np.ndarray([z, y, x], dtype=dtype, order=order)
for y in range(a.shape[0]):
if order is 'ZYX':
if z == 1:
a = np.ndarray([y, x], dtype=dtype)
for y in range(a.shape[0]):
for x in range(a.shape[1]):
a[y][x] = y * a.shape[1] + x
elif z > 1:
a = np.ndarray([z, y, x], dtype=dtype)
for z in range(a.shape[0]):
for y in range(a.shape[1]):
for x in range(a.shape[2]):
a[z][y][x] = z * a.shape[1] * a.shape[2] + y * a.shape[2] + x
else:
if z == 1:
a = np.ndarray([x, y], dtype=dtype)
for y in range(a.shape[1]):
for x in range(a.shape[2]):
a[z][y][x] = z * a.shape[1] * a.shape[2] + y * a.shape[2] + x
for x in range(a.shape[0]):
a[x][y] = y * a.shape[0] + x
elif z > 1:
a = np.ndarray([x, y, z], dtype=dtype)
for z in range(a.shape[2]):
for y in range(a.shape[1]):
for x in range(a.shape[0]):
a[x][y][z] = z * a.shape[1] * a.shape[0] + y * a.shape[0] + x
return a
def _plot_build_array(x, y, z=1, dtype=np.ubyte, order='C'):
def _plot_build_array(x, y, z=1, dtype=np.ubyte, order='ZYX'):
arr = _build_array(x=x, y=y, z=z, dtype=dtype, order=order)
print("Array shape is: {}".format(arr.shape))
print("Array strides is: {}".format(arr.strides))
print("Array flags are: {}".format(arr.flags))
if z > 1:
plot_3D_array_z_slice(arr, cmap='gray')
plot_3D_array_z_slice(arr, cmap='gray', order=order)
else:
plot_2D_array(arr, cmap='gray')
plot_2D_array(arr, cmap='gray', order=order)
# ------------------------------------------------------------------------------
# main
# ------------------------------------------------------------------------------
_plot_handmade_3D()
_plot_handmade_3D(order='ZYX')
_plot_example_data('p58-t0_INT_down_interp_2x.inr')
# _plot_build_array(19, 9)
# _plot_build_array(19, 17)
# _plot_build_array(19, 17, order='F')
_plot_build_array(19, 17, 13)
_plot_build_array(19, 17, 13, order='XYZ')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment