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

Commit

Permalink
Add imresize API to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Anirudh Acharya committed Oct 18, 2018
1 parent 1ebbf94 commit 58f2636
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
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
41 changes: 40 additions & 1 deletion 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 Down Expand Up @@ -85,6 +84,46 @@ def imread(filename, *args, **kwargs):
return _internal._cvimread(filename, *args, **kwargs)


def imresize(src, w, h, interp):
r"""Resize image with OpenCV.
Note: `imread` uses OpenCV (not the CV2 Python library).
MXNet must have been built with USE_OPENCV=1 for `imdecode` 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).
out : NDArray, optional
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.
Expand Down
16 changes: 16 additions & 0 deletions tests/python/unittest/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ 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)

def test_imresize(self):
try:
import cv2
except ImportError:
return
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)
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)
assert_almost_equal(mx_resized.asnumpy()[:, :, (2, 1, 0)], cv_resized, atol=3)

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

0 comments on commit 58f2636

Please sign in to comment.