Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Add imresize to mxnet.image documentation #12831

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/api/python/image/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ images provided in
:nosignatures:

image.imdecode
image.imresize
image.scale_down
image.resize_short
image.fixed_crop
Expand Down Expand Up @@ -164,6 +165,7 @@ and a list of augmenters specific for `Object detection` is provided
:members:

.. automethod:: mxnet.image.imdecode
.. automethod:: mxnet.image.imresize
.. automethod:: mxnet.image.scale_down
.. automethod:: mxnet.image.resize_short
.. automethod:: mxnet.image.fixed_crop
Expand Down
64 changes: 60 additions & 4 deletions python/mxnet/image/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from ..base import numeric_types
from .. import ndarray as nd
from ..ndarray import _internal
from ..ndarray._internal import _cvimresize as imresize
from ..ndarray._internal import _cvcopyMakeBorder as copyMakeBorder
from .. import io
from .. import recordio
Expand All @@ -47,7 +46,7 @@
def imread(filename, *args, **kwargs):
"""Read and decode an image to an NDArray.

Note: `imread` uses OpenCV (not the CV2 Python library).
.. note:: `imread` uses OpenCV (not the CV2 Python library).
MXNet must have been built with USE_OPENCV=1 for `imdecode` to work.

Parameters
Expand Down Expand Up @@ -85,10 +84,67 @@ def imread(filename, *args, **kwargs):
return _internal._cvimread(filename, *args, **kwargs)


def imresize(src, w, h, interp):
r"""Resize image with OpenCV.

.. note:: `imresize` uses OpenCV (not the CV2 Python library). MXNet must have been built
anirudhacharya marked this conversation as resolved.
Show resolved Hide resolved
with USE_OPENCV=1 for `imresize` to work.

Parameters
----------
src : NDArray
source image
w : int, required
Width of resized image.
h : int, required
Height of resized image.
interp : int, optional, default=1
Interpolation method (default=cv2.INTER_LINEAR).
anirudhacharya marked this conversation as resolved.
Show resolved Hide resolved
Possible values:
0: Nearest Neighbors Interpolation.
1: Bilinear interpolation.
2: Area-based (resampling using pixel area relation). It may be a
preferred method for image decimation, as it gives moire-free
results. But when the image is zoomed, it is similar to the Nearest
Neighbors method. (used by default).
3: Bicubic interpolation over 4x4 pixel neighborhood.
4: Lanczos interpolation over 8x8 pixel neighborhood.
9: Cubic for enlarge, area for shrink, bilinear for others
10: Random select from interpolation method metioned above.
Note:
When shrinking an image, it will generally look best with AREA-based
anirudhacharya marked this conversation as resolved.
Show resolved Hide resolved
interpolation, whereas, when enlarging an image, it will generally look best
with Bicubic (slow) or Bilinear (faster but still looks OK).
More details can be found in the documentation of OpenCV, please refer to
http://docs.opencv.org/master/da/d54/group__imgproc__transform.html.

out : NDArray, optional
anirudhacharya marked this conversation as resolved.
Show resolved Hide resolved
The output NDArray to hold the result.

Returns
-------
out : NDArray or list of NDArrays
The output of this function.

Example
-------
>>> with open("flower.jpeg", 'rb') as fp:
... str_image = fp.read()
...
>>> image = mx.img.imdecode(str_image)
>>> image
<NDArray 2321x3482x3 @cpu(0)>
>>> new_image = mx.img.resize(image, 240, 360)
>>> new_image
<NDArray 2321x3482x3 @cpu(0)>
"""
return _internal._cvimresize(src, w, h, interp)


def imdecode(buf, *args, **kwargs):
"""Decode an image to an NDArray.

Note: `imdecode` uses OpenCV (not the CV2 Python library).
.. note:: `imdecode` uses OpenCV (not the CV2 Python library).
MXNet must have been built with USE_OPENCV=1 for `imdecode` to work.

Parameters
Expand Down Expand Up @@ -235,7 +291,7 @@ def _get_interp_method(interp, sizes=()):
def resize_short(src, size, interp=2):
"""Resizes shorter edge to size.

Note: `resize_short` uses OpenCV (not the CV2 Python library).
.. note:: `resize_short` uses OpenCV (not the CV2 Python library).
MXNet must have been built with OpenCV for `resize_short` to work.

Resizes the original image by setting the shorter edge to size
Expand Down
22 changes: 20 additions & 2 deletions tests/python/unittest/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_imdecode(self):
try:
import cv2
except ImportError:
return
raise unittest.SkipTest("Unable to import cv2.")
for img in TestImage.IMAGES:
with open(img, 'rb') as fp:
str_image = fp.read()
Expand All @@ -97,11 +97,12 @@ def test_scale_down(self):
assert mx.image.scale_down((360, 1000), (480, 500)) == (360, 375)
assert mx.image.scale_down((300, 400), (0, 0)) == (0, 0)

@with_seed()
def test_resize_short(self):
try:
import cv2
except ImportError:
return
raise unittest.SkipTest("Unable to import cv2")
for img in TestImage.IMAGES:
cv_img = cv2.imread(img)
mx_img = mx.nd.array(cv_img[:, :, (2, 1, 0)])
Expand All @@ -118,6 +119,23 @@ def test_resize_short(self):
mx_resized = mx.image.resize_short(mx_img, new_size, interp)
assert_almost_equal(mx_resized.asnumpy()[:, :, (2, 1, 0)], cv_resized, atol=3)

@with_seed()
def test_imresize(self):
try:
import cv2
except ImportError:
raise unittest.SkipTest("Unable to import cv2")
for img in TestImage.IMAGES:
cv_img = cv2.imread(img)
mx_img = mx.nd.array(cv_img[:, :, (2, 1, 0)])
for _ in range(3):
new_h = np.random.randint(1, 1000)
anirudhacharya marked this conversation as resolved.
Show resolved Hide resolved
new_w = np.random.randint(1, 1000)
for interp in range(0, 2):
cv_resized = cv2.resize(cv_img, (new_w, new_h), interpolation=interp)
mx_resized = mx.image.imresize(mx_img, new_w, new_h, interp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have a test with passing out parameter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can add that test.

assert_almost_equal(mx_resized.asnumpy()[:, :, (2, 1, 0)], cv_resized, atol=3)
anirudhacharya marked this conversation as resolved.
Show resolved Hide resolved

def test_color_normalize(self):
for _ in range(10):
mean = np.random.rand(3) * 255
Expand Down