def resize_long(src, size, interp=2): """Resizes longer edge to size. 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 and setting the longer edge accordingly. This will ensure the new image will fit into the `size` specified. Resizing function is called from OpenCV. Parameters ---------- src : NDArray The original image. size : int The length to be set for the shorter edge. interp : int, optional, default=2 Interpolation method used for resizing the image. 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. Returns ------- NDArray An 'NDArray' containing the resized image. Example ------- >>> with open("flower.jpeg", 'rb') as fp: ... str_image = fp.read() ... >>> image = mx.img.imdecode(str_image) >>> image <NDArray 2321x3482x3 @cpu(0)> >>> size = 640 >>> new_image = mx.img.resize_long(image, size) >>> new_image <NDArray 2321x3482x3 @cpu(0)> """ from mxnet.image.image import _get_interp_method as get_interp h, w, _ = src.shape if h > w: new_h, new_w = size, size * w // h else: new_h, new_w = size * h // w, size return imresize(src, new_w, new_h, interp=get_interp(interp, (h, w, new_h, new_w)))
def resize_long(src, size, interp=2): """Resizes longer edge to size. Note: `resize_long` uses OpenCV (not the CV2 Python library). MXNet must have been built with OpenCV for `resize_long` to work. Resizes the original image by setting the longer edge to size and setting the shorter edge accordingly. This will ensure the new image will fit into the `size` specified. Resizing function is called from OpenCV. Parameters ---------- src : NDArray The original image. size : int The length to be set for the shorter edge. interp : int, optional, default=2 Interpolation method used for resizing the image. 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 mentioned 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. Returns ------- NDArray An 'NDArray' containing the resized image. Example ------- >>> with open("flower.jpeg", 'rb') as fp: ... str_image = fp.read() ... >>> image = mx.img.imdecode(str_image) >>> image <NDArray 2321x3482x3 @cpu(0)> >>> size = 640 >>> new_image = mx.img.resize_long(image, size) >>> new_image <NDArray 386x640x3 @cpu(0)> """ from mxnet.image.image import _get_interp_method as get_interp h, w, _ = src.shape if h > w: new_h, new_w = size, size * w // h else: new_h, new_w = size * h // w, size return imresize(src, new_w, new_h, interp=get_interp(interp, (h, w, new_h, new_w)))
def resize_short_within(src, short, max_size, mult_base=1, interp=2): h, w, _ = src.shape # _ = len im_size_min, im_size_max = (h, w) if w > h else (w, h) scale = float(short) / float(im_size_min) if np.round(scale * im_size_max / mult_base) * mult_base > max_size: # fit in max_size scale = float( np.floor(max_size / mult_base) * mult_base) / float(im_size_max) new_w, new_h = (int(np.round(w * scale / mult_base) * mult_base), int(np.round(h * scale / mult_base) * mult_base)) return imresize(src, new_w, new_h, interp=get_interp(interp, (h, w, new_h, new_w)))
def imresize(src, w, h, interp=1): """Resize image with OpenCV. This is a duplicate of mxnet.image.imresize for name space consistancy. Parameters ---------- src : mxnet.nd.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. Examples -------- >>> import mxnet as mx >>> from gluoncv import data as gdata >>> img = mx.random.uniform(0, 255, (300, 300, 3)).astype('uint8') >>> print(img.shape) (300, 300, 3) >>> img = gdata.transforms.image.imresize(img, 200, 200) >>> print(img.shape) (200, 200, 3) """ from mxnet.image.image import _get_interp_method as get_interp oh, ow, _ = src.shape return mx.image.imresize(src, w, h, interp=get_interp(interp, (oh, ow, h, w)))
def imresize(src, w, h, interp=1): """Resize image with OpenCV. This is a duplicate of mxnet.image.imresize for name space consistency. Parameters ---------- src : mxnet.nd.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. Examples -------- >>> import mxnet as mx >>> from gluoncv import data as gdata >>> img = mx.random.uniform(0, 255, (300, 300, 3)).astype('uint8') >>> print(img.shape) (300, 300, 3) >>> img = gdata.transforms.image.imresize(img, 200, 200) >>> print(img.shape) (200, 200, 3) """ from mxnet.image.image import _get_interp_method as get_interp oh, ow, _ = src.shape return mx.image.imresize(src, w, h, interp=get_interp(interp, (oh, ow, h, w)))
def imresize(src, w, h, interp=1): oh, ow, _ = src.shape # len return mx.image.imresize(src, w, h, interp=get_interp(interp, (oh, ow, h, w)))
def resize_short_within(src, short, max_size, interp=2): """Resizes shorter edge to size but make sure it's capped at maximum size. Note: `resize_short_within` uses OpenCV (not the CV2 Python library). MXNet must have been built with OpenCV for `resize_short_within` to work. Resizes the original image by setting the shorter edge to size and setting the longer edge accordingly. Also this function will ensure the new image will not exceed ``max_size`` even at the longer side. Resizing function is called from OpenCV. Parameters ---------- src : NDArray The original image. short : int Resize shorter side to ``short``. max_size : int Make sure the longer side of new image is smaller than ``max_size``. interp : int, optional, default=2 Interpolation method used for resizing the image. 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. Returns ------- NDArray An 'NDArray' containing the resized image. 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_short_within(image, size=600, max_size=1000) >>> new_image <NDArray 604x1000x3 @cpu(0)> >>> new_image = mx.img.resize_short_within(image, size=600, max_size=1200) >>> new_image <NDArray 600x993x3 @cpu(0)> """ from mxnet.image.image import _get_interp_method as get_interp h, w, _ = src.shape im_size_min, im_size_max = (h, w) if w > h else (w, h) scale = float(short) / float(im_size_min) if np.round(scale * im_size_max) > max_size: # fit in max_size scale = float(max_size) / float(im_size_max) new_w, new_h = int(np.round(w * scale)), int(np.round(h * scale)) return imresize(src, new_w, new_h, interp=get_interp(interp, (h, w, new_h, new_w)))
def resize_short_within(src, short, max_size, mult_base=1, interp=2): """Resizes shorter edge to size but make sure it's capped at maximum size. Note: `resize_short_within` uses OpenCV (not the CV2 Python library). MXNet must have been built with OpenCV for `resize_short_within` to work. Resizes the original image by setting the shorter edge to size and setting the longer edge accordingly. Also this function will ensure the new image will not exceed ``max_size`` even at the longer side. Resizing function is called from OpenCV. Parameters ---------- src : NDArray The original image. short : int Resize shorter side to ``short``. max_size : int Make sure the longer side of new image is smaller than ``max_size``. mult_base : int, default is 1 Width and height are rounded to multiples of `mult_base`. interp : int, optional, default=2 Interpolation method used for resizing the image. 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 mentioned 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. Returns ------- NDArray An 'NDArray' containing the resized image. 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 = resize_short_within(image, short=800, max_size=1000) >>> new_image <NDArray 667x1000x3 @cpu(0)> >>> new_image = resize_short_within(image, short=800, max_size=1200) >>> new_image <NDArray 800x1200x3 @cpu(0)> >>> new_image = resize_short_within(image, short=800, max_size=1200, mult_base=32) >>> new_image <NDArray 800x1184x3 @cpu(0)> """ from mxnet.image.image import _get_interp_method as get_interp h, w, _ = src.shape im_size_min, im_size_max = (h, w) if w > h else (w, h) scale = float(short) / float(im_size_min) if np.round(scale * im_size_max / mult_base) * mult_base > max_size: # fit in max_size scale = float(np.floor(max_size / mult_base) * mult_base) / float(im_size_max) new_w, new_h = (int(np.round(w * scale / mult_base) * mult_base), int(np.round(h * scale / mult_base) * mult_base)) return imresize(src, new_w, new_h, interp=get_interp(interp, (h, w, new_h, new_w)))