refactoring and remove some public apis

This commit is contained in:
Kai Chen 2018-10-04 17:15:14 +08:00
parent 34eca27216
commit 51b73ac173
5 changed files with 12 additions and 65 deletions

View File

@ -1,12 +1,12 @@
from .io import imread, imwrite, imfrombytes from .io import imread, imwrite, imfrombytes
from .transforms import (bgr2gray, gray2bgr, bgr2rgb, rgb2bgr, bgr2hsv, from .transforms import (bgr2gray, gray2bgr, bgr2rgb, rgb2bgr, bgr2hsv,
hsv2bgr, imflip, imrotate, imcrop, impad, hsv2bgr, imflip, imrotate, imcrop, impad,
impad_to_multiple, imnorm, imdenorm, scale_size, impad_to_multiple, imnormalize, imdenormalize,
imresize, imresize_like, imrescale, limit_size) imresize, imresize_like, imrescale)
__all__ = [ __all__ = [
'imread', 'imwrite', 'imfrombytes', 'bgr2gray', 'gray2bgr', 'bgr2rgb', 'imread', 'imwrite', 'imfrombytes', 'bgr2gray', 'gray2bgr', 'bgr2rgb',
'rgb2bgr', 'bgr2hsv', 'hsv2bgr', 'imflip', 'imrotate', 'imcrop', 'impad', 'rgb2bgr', 'bgr2hsv', 'hsv2bgr', 'imflip', 'imrotate', 'imcrop', 'impad',
'impad_to_multiple', 'imnorm', 'imdenorm', 'scale_size', 'imresize', 'impad_to_multiple', 'imnormalize', 'imdenormalize', 'imresize',
'imresize_like', 'imrescale', 'limit_size' 'imresize_like', 'imrescale'
] ]

View File

@ -1,11 +1,10 @@
from .colorspace import bgr2gray, gray2bgr, bgr2rgb, rgb2bgr, bgr2hsv, hsv2bgr from .colorspace import bgr2gray, gray2bgr, bgr2rgb, rgb2bgr, bgr2hsv, hsv2bgr
from .geometry import imflip, imrotate, imcrop, impad, impad_to_multiple from .geometry import imflip, imrotate, imcrop, impad, impad_to_multiple
from .normalize import imnorm, imdenorm from .normalize import imnormalize, imdenormalize
from .resize import scale_size, imresize, imresize_like, imrescale, limit_size from .resize import imresize, imresize_like, imrescale
__all__ = [ __all__ = [
'bgr2gray', 'gray2bgr', 'bgr2rgb', 'rgb2bgr', 'bgr2hsv', 'hsv2bgr', 'bgr2gray', 'gray2bgr', 'bgr2rgb', 'rgb2bgr', 'bgr2hsv', 'hsv2bgr',
'imflip', 'imrotate', 'imcrop', 'impad', 'impad_to_multiple', 'imnorm', 'imflip', 'imrotate', 'imcrop', 'impad', 'impad_to_multiple',
'imdenorm', 'scale_size', 'imresize', 'imresize_like', 'imrescale', 'imnormalize', 'imdenormalize', 'imresize', 'imresize_like', 'imrescale'
'limit_size'
] ]

View File

@ -3,14 +3,14 @@ import numpy as np
from .colorspace import bgr2rgb, rgb2bgr from .colorspace import bgr2rgb, rgb2bgr
def imnorm(img, mean, std, to_rgb=True): def imnormalize(img, mean, std, to_rgb=True):
img = img.astype(np.float32) img = img.astype(np.float32)
if to_rgb: if to_rgb:
img = bgr2rgb(img) img = bgr2rgb(img)
return (img - mean) / std return (img - mean) / std
def imdenorm(img, mean, std, to_bgr=True): def imdenormalize(img, mean, std, to_bgr=True):
img = (img * std) + mean img = (img * std) + mean
if to_bgr: if to_bgr:
img = rgb2bgr(img) img = rgb2bgr(img)

View File

@ -3,7 +3,7 @@ from __future__ import division
import cv2 import cv2
def scale_size(size, scale): def _scale_size(size, scale):
"""Rescale a size by a ratio. """Rescale a size by a ratio.
Args: Args:
@ -99,37 +99,9 @@ def imrescale(img, scale, return_scale=False, interpolation='bilinear'):
raise TypeError( raise TypeError(
'Scale must be a number or tuple of int, but got {}'.format( 'Scale must be a number or tuple of int, but got {}'.format(
type(scale))) type(scale)))
new_size = scale_size((w, h), scale_factor) new_size = _scale_size((w, h), scale_factor)
rescaled_img = imresize(img, new_size, interpolation=interpolation) rescaled_img = imresize(img, new_size, interpolation=interpolation)
if return_scale: if return_scale:
return rescaled_img, scale_factor return rescaled_img, scale_factor
else: else:
return rescaled_img return rescaled_img
def limit_size(img, max_edge, return_scale=False, interpolation='bilinear'):
"""Limit the size of an image.
If the long edge of the image is greater than max_edge, resize the image.
Args:
img (ndarray): The input image.
max_edge (int): Maximum value of the long edge.
return_scale (bool): Whether to return scale besides the resized image.
interpolation (str): Same as :func:`resize`.
Returns:
tuple or ndarray: (resized_img, scale_factor) or resized_img.
"""
h, w = img.shape[:2]
if max(h, w) > max_edge:
scale = float(max_edge) / max(h, w)
new_size = scale_size((w, h), scale)
resized_img = imresize(img, new_size, interpolation=interpolation)
else:
scale = 1.0
resized_img = img
if return_scale:
return resized_img, scale
else:
return resized_img

View File

@ -110,10 +110,6 @@ class TestImage(object):
computed_hsv[i, j, :] = [h, s, v] computed_hsv[i, j, :] = [h, s, v]
assert_array_almost_equal(out_img, computed_hsv, decimal=2) assert_array_almost_equal(out_img, computed_hsv, decimal=2)
def test_scale_size(self):
assert mmcv.scale_size((300, 200), 0.5) == (150, 100)
assert mmcv.scale_size((11, 22), 0.7) == (8, 15)
def test_imresize(self): def test_imresize(self):
resized_img = mmcv.imresize(self.img, (1000, 600)) resized_img = mmcv.imresize(self.img, (1000, 600))
assert resized_img.shape == (600, 1000, 3) assert resized_img.shape == (600, 1000, 3)
@ -158,26 +154,6 @@ class TestImage(object):
with pytest.raises(TypeError): with pytest.raises(TypeError):
mmcv.imrescale(self.img, [100, 100]) mmcv.imrescale(self.img, [100, 100])
def test_limit_size(self):
# limit to 800
resized_img = mmcv.limit_size(self.img, 800)
assert resized_img.shape == (300, 400, 3)
resized_img, scale = mmcv.limit_size(self.img, 800, True)
assert resized_img.shape == (300, 400, 3) and scale == 1
# limit to 200
resized_img = mmcv.limit_size(self.img, 200)
assert resized_img.shape == (150, 200, 3)
resized_img, scale = mmcv.limit_size(self.img, 200, True)
assert resized_img.shape == (150, 200, 3) and scale == 0.5
# test with img rather than img path
img = mmcv.imread(self.img)
resized_img = mmcv.limit_size(img, 200)
assert resized_img.shape == (150, 200, 3)
resized_img, scale = mmcv.limit_size(img, 200, True)
assert resized_img.shape == (150, 200, 3) and scale == 0.5
def test_imflip(self): def test_imflip(self):
# test horizontal flip (color image) # test horizontal flip (color image)
img = np.random.rand(80, 60, 3) img = np.random.rand(80, 60, 3)