Commit 944da49f authored by Yue Zhao's avatar Yue Zhao Committed by Kai Chen
Browse files

add iminvert (#58)

* add iminvert

* fix for flake8

* minor improvement

* add test for iminvert

* fix for flake8

* fix for flake8

* revise __init__

* revise __init__
parent 38fa3142
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'
] ]
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'
] ]
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):
......
...@@ -285,3 +285,10 @@ class TestImage(object): ...@@ -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)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment