Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bob.io.image
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
bob
bob.io.image
Commits
961af72b
Commit
961af72b
authored
4 years ago
by
Amir MOHAMMADI
Browse files
Options
Downloads
Patches
Plain Diff
Allow conversion of batched images and videos to matplotlib and back
parent
220d8645
No related branches found
No related tags found
1 merge request
!44
Allow conversion of batched images and videos to matplotlib and back
Pipeline
#44257
passed
4 years ago
Stage: build
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
bob/io/image/utils.py
+77
-22
77 additions, 22 deletions
bob/io/image/utils.py
doc/guide.rst
+4
-0
4 additions, 0 deletions
doc/guide.rst
version.txt
+1
-1
1 addition, 1 deletion
version.txt
with
82 additions
and
23 deletions
bob/io/image/utils.py
+
77
−
22
View file @
961af72b
...
...
@@ -2,55 +2,110 @@ import numpy as np
def
to_matplotlib
(
img
):
'''
Returns a view of the image compatible with matplotlib.
"""
Returns a view of the image from Bob format to matplotlib format.
This function works with images, batches of images, videos, and higher
dimensional arrays that contain images.
Parameters
----------
img : numpy.ndarray
A
2 or 3
dimensional array containing an image in
b
ob
style: For a 2D
array (grayscal
e image
)
should
be ``(y, x)``; For a 3D array (color
image) should be ``(n
,
y
,
x
)``.
A
N
dimensional array containing an image in
B
ob
format (channels
first): For an ND array (N >= 3), th
e image should
have the following
format: ``(..., c
,
h
,
w
)``.
Returns
-------
numpy.ndarray
A view of the ``img`` compatible with
:py:func:`matplotlib.pyplot.imshow`.
'''
if
img
.
ndim
==
3
:
img
=
np
.
moveaxis
(
img
,
0
,
-
1
)
return
img
"""
if
img
.
ndim
<
3
:
return
img
return
np
.
moveaxis
(
img
,
-
3
,
-
1
)
def
to_bob
(
img
):
'''
Returns a view of the image compatible with Bob.
"""
Returns a view of the image from matplotlib format to Bob format.
This function works with images, batches of images, videos, and higher
dimensional arrays that contain images.
Parameters
----------
img : numpy.ndarray
A 2 or 3 dimensional array containing an image in matplotlib style: For
a 2D array (grayscale image) should be ``(y, x)``; For a 3D array
(color image) should be ``(y, x, n)``.
An image in matplotlib format (channels last): For an ND array (N >= 3),
the image should have the following format: ``(..., h, w, c)``.
Returns
-------
numpy.ndarray
A view of the ``img`` compatible with Bob ``(n, y, x)`` for 3D images.
'''
if
img
.
ndim
==
3
:
img
=
np
.
moveaxis
(
img
,
-
1
,
0
)
return
img
A view of the ``img`` compatible with Bob.
"""
if
img
.
ndim
<
3
:
return
img
return
np
.
moveaxis
(
img
,
-
1
,
-
3
)
def
opencvbgr_to_bob
(
img
):
"""
Returns a view of the image from OpenCV BGR format to Bob RGB format.
This function works with images, batches of images, videos, and higher
dimensional arrays that contain images.
Parameters
----------
img : numpy.ndarray
An image loaded by OpenCV. It needs to have at least 3 dimensions.
Returns
-------
numpy.ndarray
A view of the ``img`` compatible with Bob.
Raises
------
ValueError
If the image dimension is less than 3.
"""
if
img
.
ndim
<
3
:
raise
ValueError
(
"
You need to provide at least a 3 dimensional image
"
)
img
=
img
[...,
::
-
1
]
return
to_bob
(
img
)
def
bob_to_opencvbgr
(
img
):
"""
Returns a view of the image from Bob format to OpenCV BGR format.
This function works with images, batches of images, videos, and higher
dimensional arrays that contain images.
Parameters
----------
img : numpy.ndarray
An image loaded by Bob. It needs to have at least 3 dimensions.
Returns
-------
numpy.ndarray
A view of the ``img`` compatible with OpenCV.
Raises
------
ValueError
If the image dimension is less than 3.
"""
if
img
.
ndim
<
3
:
raise
ValueError
(
"
You need to provide at least a 3 dimensional image
"
)
img
=
img
[...,
::
-
1
,
:,
:]
return
to_matplotlib
(
img
)
def
imshow
(
img
,
cmap
=
None
,
**
kwargs
):
'''
Plots the images that are returned by :py:func:`bob.io.base.load`
"""
Plots the images that are returned by :py:func:`bob.io.base.load`
Parameters
----------
img : numpy.ndarray
A 2 or 3 dimensional array containing an image in
bob style: For a 2D array (grayscale image) should be ``(
y
,
x
)``;
A 3D array (color image) should be in the ``(
n
,
y
,
x
)`` format.
bob style: For a 2D array (grayscale image) should be ``(
h
,
w
)``;
A 3D array (color image) should be in the ``(
c
,
h
,
w
)`` format.
cmap : matplotlib.colors.Colormap
Colormap, optional, default: ``None``.
If ``cmap`` is ``None`` and ``img.ndim`` is 2, defaults to
'
gray
'
.
...
...
@@ -62,10 +117,10 @@ def imshow(img, cmap=None, **kwargs):
-------
object
Returns whatever ``plt.imshow`` returns.
'''
"""
import
matplotlib.pyplot
as
plt
if
cmap
is
None
and
img
.
ndim
==
2
:
cmap
=
'
gray
'
cmap
=
"
gray
"
return
plt
.
imshow
(
to_matplotlib
(
img
),
cmap
=
cmap
,
**
kwargs
)
This diff is collapsed.
Click to expand it.
doc/guide.rst
+
4
−
0
View file @
961af72b
...
...
@@ -83,6 +83,10 @@ Or you can just get a view (not copy) of your image that is
>>> assert img_view_for_matplotlib.base is img
You can also get the original image back using :py:func:`bob.io.image.to_bob`.
This function works with images, batches of images, videos, and higher
dimensional arrays that contain images.
Moreover, see :any:`bob.io.image.opencvbgr_to_bob` and
:any:`bob.io.image.bob_to_opencvbgr`.
.. testcleanup:: *
...
...
This diff is collapsed.
Click to expand it.
version.txt
+
1
−
1
View file @
961af72b
2.4.7b0
\ No newline at end of file
2.5.0b0
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