diff --git a/docs/api/python/image/image.md b/docs/api/python/image/image.md index 11fff4f43406..0f2bce16d9ac 100644 --- a/docs/api/python/image/image.md +++ b/docs/api/python/image/image.md @@ -17,6 +17,7 @@ images provided in :nosignatures: image.imdecode + image.imresize image.scale_down image.resize_short image.fixed_crop diff --git a/python/mxnet/image/image.py b/python/mxnet/image/image.py index eee2ccf14a8e..dece30ecb7f2 100644 --- a/python/mxnet/image/image.py +++ b/python/mxnet/image/image.py @@ -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 @@ -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 + + >>> new_image = mx.img.resize(image, 240, 360) + >>> new_image + + """ + return _internal._cvimresize(src, w, h, interp) + + def imdecode(buf, *args, **kwargs): """Decode an image to an NDArray. diff --git a/tests/python/unittest/test_image.py b/tests/python/unittest/test_image.py index 0df08af317aa..1e780149b427 100644 --- a/tests/python/unittest/test_image.py +++ b/tests/python/unittest/test_image.py @@ -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