add iminvert (#58)

* add iminvert

* fix for flake8

* minor improvement

* add test for iminvert

* fix for flake8

* fix for flake8

* revise __init__

* revise __init__
This commit is contained in:
Yue Zhao 2019-04-20 15:27:59 +08:00 committed by Kai Chen
parent 38fa314226
commit 944da49fcb
4 changed files with 26 additions and 6 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, iminvert, imflip, imrotate, imcrop, impad,
impad_to_multiple, imnormalize, imdenormalize, impad_to_multiple, imnormalize, imdenormalize,
imresize, imresize_like, imrescale) 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', 'iminvert', 'imflip', 'imrotate',
'impad_to_multiple', 'imnormalize', 'imdenormalize', 'imresize', 'imcrop', 'impad', 'impad_to_multiple', 'imnormalize', 'imdenormalize',
'imresize_like', 'imrescale' 'imresize', 'imresize_like', 'imrescale'
] ]

View File

@ -1,10 +1,11 @@
from .colorspace import bgr2gray, gray2bgr, bgr2rgb, rgb2bgr, bgr2hsv, hsv2bgr from .colorspace import (bgr2gray, gray2bgr, bgr2rgb, rgb2bgr, bgr2hsv,
hsv2bgr, iminvert)
from .geometry import imflip, imrotate, imcrop, impad, impad_to_multiple from .geometry import imflip, imrotate, imcrop, impad, impad_to_multiple
from .normalize import imnormalize, imdenormalize from .normalize import imnormalize, imdenormalize
from .resize import imresize, imresize_like, imrescale 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', 'iminvert', 'imflip', 'imrotate', 'imcrop', 'impad', 'impad_to_multiple',
'imnormalize', 'imdenormalize', 'imresize', 'imresize_like', 'imrescale' 'imnormalize', 'imdenormalize', 'imresize', 'imresize_like', 'imrescale'
] ]

View File

@ -1,4 +1,16 @@
import cv2 import cv2
import numpy as np
def iminvert(img):
"""Invert (negate) an image
Args:
img (ndarray): Image to be inverted.
Returns:
ndarray: The inverted image.
"""
return np.full_like(img, 255) - img
def bgr2gray(img, keepdim=False): def bgr2gray(img, keepdim=False):

View File

@ -285,3 +285,10 @@ class TestImage(object):
with pytest.raises(ValueError): with pytest.raises(ValueError):
mmcv.imrotate(img, 90, center=(0, 0), auto_bound=True) mmcv.imrotate(img, 90, center=(0, 0), auto_bound=True)
def test_iminvert(self):
img = np.array([[0, 128, 255], [1, 127, 254], [2, 129, 253]],
dtype=np.uint8)
img_r = np.array([[255, 127, 0], [254, 128, 1], [253, 126, 2]],
dtype=np.uint8)
assert_array_equal(mmcv.iminvert(img), img_r)