Mentions légales du service
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
vt-python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
morpheme
vt-python
Commits
5506d55b
Commit
5506d55b
authored
5 years ago
by
MALANDAIN Gregoire
Browse files
Options
Downloads
Patches
Plain Diff
examples/image_visu.py: exemplify differences between 'XYZ' and 'ZYX' conventions
parent
fbb59fad
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/image_visu.py
+103
-54
103 additions, 54 deletions
examples/image_visu.py
with
103 additions
and
54 deletions
examples/image_visu.py
+
103
−
54
View file @
5506d55b
...
...
@@ -6,73 +6,106 @@ import os
from
vt
import
vtImage
def
plot_
3
D_array
_z_slice
(
arr
,
z_slice
=
None
,
cmap
=
'
gray
'
,
title
=
""
):
"""
Plot a
z-slice of a given 3
D array.
def
plot_
2
D_array
(
arr
,
cmap
=
'
gray
'
,
order
=
'
ZYX
'
,
title
=
""
):
"""
Plot a
2
D 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_
2
D_array
(
arr
,
cmap
=
'
gray
'
,
title
=
""
):
"""
Plot a
2
D array.
def
plot_
3
D_array
_z_slice
(
arr
,
z_slice
=
None
,
cmap
=
'
gray
'
,
order
=
'
ZYX
'
,
title
=
""
):
"""
Plot a
z-slice of a given 3
D 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
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment