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

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Anirudh Acharya committed Dec 10, 2018
1 parent 95b5a2c commit 20b8349
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
33 changes: 25 additions & 8 deletions python/mxnet/image/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,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 @@ -84,11 +84,11 @@ def imread(filename, *args, **kwargs):
return _internal._cvimread(filename, *args, **kwargs)


def imresize(src, w, h, interp):
def imresize(src, w, h, *args, **kwargs):
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.
.. note:: `imresize` uses OpenCV (not the CV2 Python library). MXNet must have been built
with USE_OPENCV=1 for `imresize` to work.
Parameters
----------
Expand All @@ -98,8 +98,25 @@ def imresize(src, w, h, interp):
Width of resized image.
h : int, required
Height of resized image.
interp : int, optional, default='1'
interp : int, optional, default=1
Interpolation method (default=cv2.INTER_LINEAR).
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
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
The output NDArray to hold the result.
Expand All @@ -121,13 +138,13 @@ def imresize(src, w, h, interp):
>>> new_image
<NDArray 2321x3482x3 @cpu(0)>
"""
return _internal._cvimresize(src, w, h, interp)
return _internal._cvimresize(src, w, h, *args, **kwargs)


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 @@ -275,7 +292,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
17 changes: 11 additions & 6 deletions tests/python/unittest/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,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 Down Expand Up @@ -175,11 +175,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 @@ -196,21 +197,25 @@ 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:
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)])
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)
for interp_val in range(0, 2):
cv_resized = cv2.resize(cv_img, (new_w, new_h), interpolation=interp_val)
mx_resized = mx.image.imresize(mx_img, new_w, new_h, interp=interp_val)
assert_almost_equal(mx_resized.asnumpy()[:, :, (2, 1, 0)], cv_resized, atol=3)
out_img = mx.nd.zeros((new_h, new_w, 3), dtype=mx_img.dtype)
mx.image.imresize(mx_img, new_w, new_h, interp=interp_val, out=out_img)
assert_almost_equal(out_img.asnumpy()[:, :, (2, 1, 0)], cv_resized, atol=3)

def test_color_normalize(self):
for _ in range(10):
Expand Down

0 comments on commit 20b8349

Please sign in to comment.