Mentions légales du service

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

examples/image_visu.py: add conversion from 'XYZ' to 'ZYX' and conversely

parent 5506d55b
No related branches found
No related tags found
No related merge requests found
......@@ -19,12 +19,12 @@ def plot_2D_array(arr, cmap='gray', order='ZYX', title=""):
"""
if arr.flags.c_contiguous is True or order is 'ZYX':
if 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':
elif order is 'XYZ':
import matplotlib.pyplot as plt
plt.figure()
plt.imshow(np.transpose(arr), cmap=cmap)
......@@ -60,7 +60,7 @@ def plot_3D_array_z_slice(arr, z_slice=None, cmap='gray', order='ZYX', title="")
if arr.ndim <= 1:
print("not an image")
elif arr.ndim == 2:
plot_2D_array(arr, cmap=cmap, title=title)
plot_2D_array(arr, cmap=cmap, order=order, title=title)
elif arr.ndim == 3:
if order is 'ZYX':
if z_slice is None:
......@@ -107,10 +107,10 @@ def _plot_handmade_3D(order='XYZ'):
lorder = 'ZYX'
print("2D array is C-contiguous:", arr_2d.flags.c_contiguous)
print("3D array is C-contiguous:", arr_3d.flags.c_contiguous)
print("Z-slice of 3D array is C-contiguous:", arr_3d[:, :, 1].flags.c_contiguous)
print("Z-slice copy of 3D array is C-contiguous:", arr_3d[:, :, 1].copy().flags.c_contiguous)
# print("2D array is C-contiguous:", arr_2d.flags.c_contiguous)
# print("3D array is C-contiguous:", arr_3d.flags.c_contiguous)
# print("Z-slice of 3D array is C-contiguous:", arr_3d[:, :, 1].flags.c_contiguous)
# print("Z-slice copy of 3D array is C-contiguous:", arr_3d[:, :, 1].copy().flags.c_contiguous)
print("- type q to quit")
......@@ -141,6 +141,12 @@ def _plot_example_data(img_name, order='ZYX'):
#
# ------------------------------------------------------------------------------
def _print_array_information(arr):
print(" - Array shape is: {}".format(arr.shape))
print(" - Array strides is: {}".format(arr.strides))
print(" - Array flags are: {}".format(arr.flags))
print(" - Array data is: {}".format(arr.data))
print("")
def _build_array(x, y, z=1, dtype=np.ubyte, order='ZYX'):
a = None
......@@ -174,22 +180,49 @@ def _build_array(x, y, z=1, dtype=np.ubyte, order='ZYX'):
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', order=order)
else:
plot_2D_array(arr, cmap='gray', order=order)
def _XYZ_ZYX_convert(arr):
if arr.ndim <= 1:
print("not an image")
elif arr.ndim == 2:
return arr.transpose([1,0])
elif arr.ndim == 3:
return arr.transpose([2, 1, 0])
else:
print("not an image")
def _test_plot_build_array(x, y, z=1, dtype=np.ubyte):
print("1. build a 'ZYX'-array")
arr = _build_array(x=x, y=y, z=z, dtype=dtype, order='ZYX')
_print_array_information(arr)
plot_3D_array_z_slice(arr, cmap='gray', order='ZYX')
print("2. convert it into a 'XYZ'-array")
brr = _XYZ_ZYX_convert(arr)
_print_array_information(brr)
plot_3D_array_z_slice(brr, cmap='gray', order='XYZ')
print("3. back to a 'ZYX'-array")
crr = _XYZ_ZYX_convert(brr)
_print_array_information(crr)
plot_3D_array_z_slice(crr, cmap='gray', order='ZYX')
# ------------------------------------------------------------------------------
# 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, 13)
_plot_build_array(19, 17, 13, order='XYZ')
# _plot_build_array(19, 17, 13)
# _plot_build_array(19, 17, 13, order='XYZ')
_test_plot_build_array(9, 7, 5)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment