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

Commit

Permalink
copyMakeBorder
Browse files Browse the repository at this point in the history
  • Loading branch information
Anirudh Acharya committed Nov 27, 2018
1 parent 1be0a3a commit c260411
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 12 deletions.
2 changes: 2 additions & 0 deletions docs/api/python/image/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ images provided in
image.imdecode
image.imresize
image.scale_down
image.copyMakeBorder
image.resize_short
image.fixed_crop
image.random_crop
Expand Down Expand Up @@ -169,6 +170,7 @@ and a list of augmenters specific for `Object detection` is provided
.. automethod:: mxnet.image.imdecode
.. automethod:: mxnet.image.imresize
.. automethod:: mxnet.image.scale_down
.. automethod:: mxnet.image.copyMakeBorder
.. automethod:: mxnet.image.resize_short
.. automethod:: mxnet.image.fixed_crop
.. automethod:: mxnet.image.random_crop
Expand Down
56 changes: 54 additions & 2 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 _cvcopyMakeBorder as copyMakeBorder
from .. import io
from .. import recordio

Expand Down Expand Up @@ -136,7 +135,7 @@ def imresize(src, w, h, *args, **kwargs):
<NDArray 2321x3482x3 @cpu(0)>
>>> new_image = mx.img.resize(image, 240, 360)
>>> new_image
<NDArray 2321x3482x3 @cpu(0)>
<NDArray 240x360x3 @cpu(0)>
"""
return _internal._cvimresize(src, w, h, *args, **kwargs)

Expand Down Expand Up @@ -234,6 +233,59 @@ def scale_down(src_size, size):
return int(w), int(h)


def copyMakeBorder(src, top, bot, left, right, *args, **kwargs):
"""Pad image border with OpenCV.
Parameters
----------
src : NDArray
source image
top : int, required
Top margin.
bot : int, required
Bottom margin.
left : int, required
Left margin.
right : int, required
Right margin.
type : int, optional, default='0'
Filling type (default=cv2.BORDER_CONSTANT).
0 - cv2.BORDER_CONSTANT - Adds a constant colored border.
1 - cv2.BORDER_REFLECT - Border will be mirror reflection of the
border elements, like this : fedcba|abcdefgh|hgfedcb
2 - cv2.BORDER_REFLECT_101 or cv.BORDER_DEFAULT - Same as above,
but with a slight change, like this : gfedcb|abcdefgh|gfedcba
3 - cv2.BORDER_REPLICATE - Last element is replicated throughout,
like this: aaaaaa|abcdefgh|hhhhhhh
4 - cv2.BORDER_WRAP - it will look like this : cdefgh|abcdefgh|abcdefg
value : double, optional, default=0
(Deprecated! Use ``values`` instead.) Fill with single value.
values : tuple of <double>, optional, default=[]
Fill with value(RGB[A] or gray), up to 4 channels.
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_border = mx.image.copyMakeBorder(mx_img, 1, 2, 3, 4, type=0)
>>> new_image
<NDArray 2324x3489x3 @cpu(0)>
"""
return _internal._cvcopyMakeBorder(src, top, bot, left, right, *args, **kwargs)


def _get_interp_method(interp, sizes=()):
"""Get the interpolation method for resize functions.
The major purpose of this function is to wrap a random interp method selection
Expand Down
44 changes: 34 additions & 10 deletions tests/python/unittest/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,15 @@ def test_imresize(self):
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_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)
new_h = np.random.randint(1, 1000)
new_w = np.random.randint(1, 1000)
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 Expand Up @@ -253,6 +252,31 @@ def check_imageiter(dtype='float32'):
# test with default dtype
check_imageiter()

@with_seed()
def test_copyMakeBorder(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)
top = np.random.randint(1, 10)
bot = np.random.randint(1, 10)
left = np.random.randint(1, 10)
right = np.random.randint(1, 10)
new_h, new_w, _ = mx_img.shape
new_h += top + bot
new_w += left + right
val = [np.random.randint(1, 255)] * 3
for type_val in range(0, 5):
cv_border = cv2.copyMakeBorder(cv_img, top, bot, left, right, borderType=type_val, value=val)
mx_border = mx.image.copyMakeBorder(mx_img, top, bot, left, right, type=type_val, values=val)
assert_almost_equal(mx_border.asnumpy(), cv_border)
out_img = mx.nd.zeros((new_h , new_w, 3), dtype=mx_img.dtype)
mx.image.copyMakeBorder(mx_img, top, bot, left, right, type=type_val, values=val, out=out_img)
assert_almost_equal(out_img.asnumpy(), cv_border)

@with_seed()
def test_augmenters(self):
# ColorNormalizeAug
Expand Down

0 comments on commit c260411

Please sign in to comment.