functional_cv2.py 22.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
import math
16 17 18 19 20 21 22
import numbers

import numpy as np

import paddle
from paddle.utils import try_import

23
from collections.abc import Sequence, Iterable
24

25 26
__all__ = []

27 28 29 30 31 32 33 34

def to_tensor(pic, data_format='CHW'):
    """Converts a ``numpy.ndarray`` to paddle.Tensor.

    See ``ToTensor`` for more details.

    Args:
        pic (np.ndarray): Image to be converted to tensor.
35
        data_format (str, optional): Data format of output tensor, should be 'HWC' or
36 37 38 39 40 41 42
            'CHW'. Default: 'CHW'.

    Returns:
        Tensor: Converted image.

    """

43
    if data_format not in ['CHW', 'HWC']:
44
        raise ValueError(
45 46
            'data_format should be CHW or HWC. Got {}'.format(data_format)
        )
47 48 49 50 51 52 53 54 55 56

    if pic.ndim == 2:
        pic = pic[:, :, None]

    if data_format == 'CHW':
        img = paddle.to_tensor(pic.transpose((2, 0, 1)))
    else:
        img = paddle.to_tensor(pic)

    if paddle.fluid.data_feeder.convert_dtype(img.dtype) == 'uint8':
57
        return paddle.cast(img, np.float32) / 255.0
58 59 60 61 62 63 64 65 66 67 68
    else:
        return img


def resize(img, size, interpolation='bilinear'):
    """
    Resizes the image to given size

    Args:
        input (np.ndarray): Image to be resized.
        size (int|list|tuple): Target size of input data, with (height, width) shape.
69 70 71 72 73 74
        interpolation (int|str, optional): Interpolation method. when use cv2 backend,
            support method are as following:
            - "nearest": cv2.INTER_NEAREST,
            - "bilinear": cv2.INTER_LINEAR,
            - "area": cv2.INTER_AREA,
            - "bicubic": cv2.INTER_CUBIC,
75 76 77 78 79 80 81 82 83 84 85 86
            - "lanczos": cv2.INTER_LANCZOS4

    Returns:
        np.array: Resized image.

    """
    cv2 = try_import('cv2')
    _cv2_interp_from_str = {
        'nearest': cv2.INTER_NEAREST,
        'bilinear': cv2.INTER_LINEAR,
        'area': cv2.INTER_AREA,
        'bicubic': cv2.INTER_CUBIC,
87
        'lanczos': cv2.INTER_LANCZOS4,
88 89
    }

90 91 92
    if not (
        isinstance(size, int) or (isinstance(size, Iterable) and len(size) == 2)
    ):
93 94 95 96 97 98 99 100 101 102 103 104 105
        raise TypeError('Got inappropriate size arg: {}'.format(size))

    h, w = img.shape[:2]

    if isinstance(size, int):
        if (w <= h and w == size) or (h <= w and h == size):
            return img
        if w < h:
            ow = size
            oh = int(size * h / w)
            output = cv2.resize(
                img,
                dsize=(ow, oh),
106 107
                interpolation=_cv2_interp_from_str[interpolation],
            )
108 109 110 111 112 113
        else:
            oh = size
            ow = int(size * w / h)
            output = cv2.resize(
                img,
                dsize=(ow, oh),
114 115
                interpolation=_cv2_interp_from_str[interpolation],
            )
116
    else:
117 118 119 120 121
        output = cv2.resize(
            img,
            dsize=(size[1], size[0]),
            interpolation=_cv2_interp_from_str[interpolation],
        )
122 123 124 125 126 127 128 129 130 131 132 133 134
    if len(img.shape) == 3 and img.shape[2] == 1:
        return output[:, :, np.newaxis]
    else:
        return output


def pad(img, padding, fill=0, padding_mode='constant'):
    """
    Pads the given numpy.array on all sides with specified padding mode and fill value.

    Args:
        img (np.array): Image to be padded.
        padding (int|list|tuple): Padding on each border. If a single int is provided this
135 136
            is used to pad all borders. If list/tuple of length 2 is provided this is the padding
            on left/right and top/bottom respectively. If a list/tuple of length 4 is provided
137 138 139 140
            this is the padding for the left, top, right and bottom borders
            respectively.
        fill (float, optional): Pixel fill value for constant fill. If a tuple of
            length 3, it is used to fill R, G, B channels respectively.
141
            This value is only used when the padding_mode is constant. Default: 0.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        padding_mode: Type of padding. Should be: constant, edge, reflect or symmetric. Default: 'constant'.

            - constant: pads with a constant value, this value is specified with fill

            - edge: pads with the last value on the edge of the image

            - reflect: pads with reflection of image (without repeating the last value on the edge)

                       padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode
                       will result in [3, 2, 1, 2, 3, 4, 3, 2]

            - symmetric: pads with reflection of image (repeating the last value on the edge)

                         padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode
                         will result in [2, 1, 1, 2, 3, 4, 4, 3]

    Returns:
        np.array: Padded image.

    """
    cv2 = try_import('cv2')
    _cv2_pad_from_str = {
        'constant': cv2.BORDER_CONSTANT,
        'edge': cv2.BORDER_REPLICATE,
        'reflect': cv2.BORDER_REFLECT_101,
167
        'symmetric': cv2.BORDER_REFLECT,
168 169 170 171 172 173 174 175 176 177 178
    }

    if not isinstance(padding, (numbers.Number, list, tuple)):
        raise TypeError('Got inappropriate padding arg')
    if not isinstance(fill, (numbers.Number, str, list, tuple)):
        raise TypeError('Got inappropriate fill arg')
    if not isinstance(padding_mode, str):
        raise TypeError('Got inappropriate padding_mode arg')

    if isinstance(padding, Sequence) and len(padding) not in [2, 4]:
        raise ValueError(
179 180 181
            "Padding must be an int or a 2, or 4 element tuple, not a "
            + "{} element tuple".format(len(padding))
        )
182

183 184 185 186 187 188
    assert padding_mode in [
        'constant',
        'edge',
        'reflect',
        'symmetric',
    ], 'Padding mode should be either constant, edge, reflect or symmetric'
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

    if isinstance(padding, list):
        padding = tuple(padding)
    if isinstance(padding, int):
        pad_left = pad_right = pad_top = pad_bottom = padding
    if isinstance(padding, Sequence) and len(padding) == 2:
        pad_left = pad_right = padding[0]
        pad_top = pad_bottom = padding[1]
    if isinstance(padding, Sequence) and len(padding) == 4:
        pad_left = padding[0]
        pad_top = padding[1]
        pad_right = padding[2]
        pad_bottom = padding[3]

    if len(img.shape) == 3 and img.shape[2] == 1:
204 205 206 207 208 209 210 211 212
        return cv2.copyMakeBorder(
            img,
            top=pad_top,
            bottom=pad_bottom,
            left=pad_left,
            right=pad_right,
            borderType=_cv2_pad_from_str[padding_mode],
            value=fill,
        )[:, :, np.newaxis]
213
    else:
214 215 216 217 218 219 220 221 222
        return cv2.copyMakeBorder(
            img,
            top=pad_top,
            bottom=pad_bottom,
            left=pad_left,
            right=pad_right,
            borderType=_cv2_pad_from_str[padding_mode],
            value=fill,
        )
223 224 225 226 227 228


def crop(img, top, left, height, width):
    """Crops the given image.

    Args:
229
        img (np.array): Image to be cropped. (0,0) denotes the top left
230 231 232 233 234 235 236 237 238 239 240
            corner of the image.
        top (int): Vertical component of the top left corner of the crop box.
        left (int): Horizontal component of the top left corner of the crop box.
        height (int): Height of the crop box.
        width (int): Width of the crop box.

    Returns:
        np.array: Cropped image.

    """

241
    return img[top : top + height, left : left + width, :]
242 243 244 245 246


def center_crop(img, output_size):
    """Crops the given image and resize it to desired size.

247 248 249 250 251
    Args:
        img (np.array): Image to be cropped. (0,0) denotes the top left corner of the image.
        output_size (sequence or int): (height, width) of the crop box. If int,
            it is used for both directions
        backend (str, optional): The image proccess backend type. Options are `pil`, `cv2`. Default: 'pil'.
252

253 254
    Returns:
        np.array: Cropped image.
255

256
    """
257 258 259 260 261 262

    if isinstance(output_size, numbers.Number):
        output_size = (int(output_size), int(output_size))

    h, w = img.shape[0:2]
    th, tw = output_size
263 264
    i = int(round((h - th) / 2.0))
    j = int(round((w - tw) / 2.0))
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    return crop(img, i, j, th, tw)


def hflip(img):
    """Horizontally flips the given image.

    Args:
        img (np.array): Image to be flipped.

    Returns:
        np.array:  Horizontall flipped image.

    """
    cv2 = try_import('cv2')

    return cv2.flip(img, 1)


def vflip(img):
    """Vertically flips the given np.array.

    Args:
        img (np.array): Image to be flipped.

    Returns:
        np.array:  Vertically flipped image.

    """
    cv2 = try_import('cv2')

    if len(img.shape) == 3 and img.shape[2] == 1:
        return cv2.flip(img, 0)[:, :, np.newaxis]
    else:
        return cv2.flip(img, 0)


def adjust_brightness(img, brightness_factor):
    """Adjusts brightness of an image.

    Args:
        img (np.array): Image to be adjusted.
        brightness_factor (float):  How much to adjust the brightness. Can be
            any non negative number. 0 gives a black image, 1 gives the
            original image while 2 increases the brightness by a factor of 2.

    Returns:
        np.array: Brightness adjusted image.

    """
    cv2 = try_import('cv2')

316 317 318 319 320
    table = (
        np.array([i * brightness_factor for i in range(0, 256)])
        .clip(0, 255)
        .astype('uint8')
    )
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

    if len(img.shape) == 3 and img.shape[2] == 1:
        return cv2.LUT(img, table)[:, :, np.newaxis]
    else:
        return cv2.LUT(img, table)


def adjust_contrast(img, contrast_factor):
    """Adjusts contrast of an image.

    Args:
        img (np.array): Image to be adjusted.
        contrast_factor (float): How much to adjust the contrast. Can be any
            non negative number. 0 gives a solid gray image, 1 gives the
            original image while 2 increases the contrast by a factor of 2.

    Returns:
        np.array: Contrast adjusted image.

    """
    cv2 = try_import('cv2')

343 344 345 346 347
    table = (
        np.array([(i - 74) * contrast_factor + 74 for i in range(0, 256)])
        .clip(0, 255)
        .astype('uint8')
    )
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    if len(img.shape) == 3 and img.shape[2] == 1:
        return cv2.LUT(img, table)[:, :, np.newaxis]
    else:
        return cv2.LUT(img, table)


def adjust_saturation(img, saturation_factor):
    """Adjusts color saturation of an image.

    Args:
        img (np.array): Image to be adjusted.
        saturation_factor (float):  How much to adjust the saturation. 0 will
            give a black and white image, 1 will give the original image while
            2 will enhance the saturation by a factor of 2.

    Returns:
        np.array: Saturation adjusted image.

    """
    cv2 = try_import('cv2')

    dtype = img.dtype
    img = img.astype(np.float32)
371 372 373
    alpha = np.random.uniform(
        max(0, 1 - saturation_factor), 1 + saturation_factor
    )
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray_img = gray_img[..., np.newaxis]
    img = img * alpha + gray_img * (1 - alpha)
    return img.clip(0, 255).astype(dtype)


def adjust_hue(img, hue_factor):
    """Adjusts hue of an image.

    The image hue is adjusted by converting the image to HSV and
    cyclically shifting the intensities in the hue channel (H).
    The image is then converted back to original image mode.

    `hue_factor` is the amount of shift in H channel and must be in the
    interval `[-0.5, 0.5]`.

    Args:
        img (np.array): Image to be adjusted.
        hue_factor (float):  How much to shift the hue channel. Should be in
            [-0.5, 0.5]. 0.5 and -0.5 give complete reversal of hue channel in
            HSV space in positive and negative direction respectively.
            0 means no shift. Therefore, both -0.5 and 0.5 will give an image
            with complementary colors while 0 gives the original image.

    Returns:
        np.array: Hue adjusted image.

    """
    cv2 = try_import('cv2')

    if not (-0.5 <= hue_factor <= 0.5):
405
        raise ValueError(
406 407
            'hue_factor:{} is not in [-0.5, 0.5].'.format(hue_factor)
        )
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

    dtype = img.dtype
    img = img.astype(np.uint8)
    hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV_FULL)
    h, s, v = cv2.split(hsv_img)

    alpha = np.random.uniform(hue_factor, hue_factor)
    h = h.astype(np.uint8)
    # uint8 addition take cares of rotation across boundaries
    with np.errstate(over="ignore"):
        h += np.uint8(alpha * 255)
    hsv_img = cv2.merge([h, s, v])
    return cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR_FULL).astype(dtype)


423 424 425 426 427 428 429 430 431 432
def affine(
    img,
    angle,
    translate,
    scale,
    shear,
    interpolation='nearest',
    fill=0,
    center=None,
):
433 434 435 436 437 438 439 440 441
    """Affine the image by matrix.

    Args:
        img (PIL.Image): Image to be affined.
        translate (sequence or int): horizontal and vertical translations
        scale (float): overall scale ratio
        shear (sequence or float): shear angle value in degrees between -180 to 180, clockwise direction.
            If a sequence is specified, the first value corresponds to a shear parallel to the x axis, while
            the second value corresponds to a shear parallel to the y axis.
442
        interpolation (int|str, optional): Interpolation method. If omitted, or if the
443
            image has only one channel, it is set to cv2.INTER_NEAREST.
444 445 446
            when use cv2 backend, support method are as following:
            - "nearest": cv2.INTER_NEAREST,
            - "bilinear": cv2.INTER_LINEAR,
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
            - "bicubic": cv2.INTER_CUBIC
        fill (3-tuple or int): RGB pixel fill value for area outside the affined image.
            If int, it is used for all channels respectively.
        center (sequence, optional): Optional center of rotation. Origin is the upper left corner.
            Default is the center of the image.

    Returns:
        np.array: Affined image.

    """
    cv2 = try_import('cv2')
    _cv2_interp_from_str = {
        'nearest': cv2.INTER_NEAREST,
        'bilinear': cv2.INTER_LINEAR,
        'area': cv2.INTER_AREA,
        'bicubic': cv2.INTER_CUBIC,
463
        'lanczos': cv2.INTER_LANCZOS4,
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
    }

    h, w = img.shape[0:2]

    if isinstance(fill, int):
        fill = tuple([fill] * 3)

    if center is None:
        center = (w / 2.0, h / 2.0)

    M = np.ones([2, 3])
    # Rotate and Scale
    R = cv2.getRotationMatrix2D(angle=angle, center=center, scale=scale)

    # Shear
    sx = math.tan(shear[0] * math.pi / 180)
    sy = math.tan(shear[1] * math.pi / 180)
    M[0] = R[0] + sy * R[1]
    M[1] = R[1] + sx * R[0]

    # Translation
    tx, ty = translate
    M[0, 2] = tx
    M[1, 2] = ty

    if len(img.shape) == 3 and img.shape[2] == 1:
490 491 492 493 494 495 496
        return cv2.warpAffine(
            img,
            M,
            dsize=(w, h),
            flags=_cv2_interp_from_str[interpolation],
            borderValue=fill,
        )[:, :, np.newaxis]
497
    else:
498 499 500 501 502 503 504 505 506 507 508 509
        return cv2.warpAffine(
            img,
            M,
            dsize=(w, h),
            flags=_cv2_interp_from_str[interpolation],
            borderValue=fill,
        )


def rotate(
    img, angle, interpolation='nearest', expand=False, center=None, fill=0
):
510 511 512 513 514
    """Rotates the image by angle.

    Args:
        img (np.array): Image to be rotated.
        angle (float or int): In degrees degrees counter clockwise order.
515
        interpolation (int|str, optional): Interpolation method. If omitted, or if the
516
            image has only one channel, it is set to cv2.INTER_NEAREST.
517 518 519
            when use cv2 backend, support method are as following:
            - "nearest": cv2.INTER_NEAREST,
            - "bilinear": cv2.INTER_LINEAR,
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
            - "bicubic": cv2.INTER_CUBIC
        expand (bool, optional): Optional expansion flag.
            If true, expands the output image to make it large enough to hold the entire rotated image.
            If false or omitted, make the output image the same size as the input image.
            Note that the expand flag assumes rotation around the center and no translation.
        center (2-tuple, optional): Optional center of rotation.
            Origin is the upper left corner.
            Default is the center of the image.
        fill (3-tuple or int): RGB pixel fill value for area outside the rotated image.
            If int, it is used for all channels respectively.

    Returns:
        np.array: Rotated image.

    """
    cv2 = try_import('cv2')
536 537 538 539 540
    _cv2_interp_from_str = {
        'nearest': cv2.INTER_NEAREST,
        'bilinear': cv2.INTER_LINEAR,
        'area': cv2.INTER_AREA,
        'bicubic': cv2.INTER_CUBIC,
541
        'lanczos': cv2.INTER_LANCZOS4,
542
    }
543

544
    h, w = img.shape[0:2]
545
    if center is None:
546
        center = (w / 2.0, h / 2.0)
547
    M = cv2.getRotationMatrix2D(center, angle, 1)
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

    if expand:

        def transform(x, y, matrix):
            (a, b, c, d, e, f) = matrix
            return a * x + b * y + c, d * x + e * y + f

        # calculate output size
        xx = []
        yy = []

        angle = -math.radians(angle)
        expand_matrix = [
            round(math.cos(angle), 15),
            round(math.sin(angle), 15),
            0.0,
            round(-math.sin(angle), 15),
            round(math.cos(angle), 15),
            0.0,
        ]

        post_trans = (0, 0)
        expand_matrix[2], expand_matrix[5] = transform(
571 572 573 574
            -center[0] - post_trans[0],
            -center[1] - post_trans[1],
            expand_matrix,
        )
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
        expand_matrix[2] += center[0]
        expand_matrix[5] += center[1]

        for x, y in ((0, 0), (w, 0), (w, h), (0, h)):
            x, y = transform(x, y, expand_matrix)
            xx.append(x)
            yy.append(y)
        nw = math.ceil(max(xx)) - math.floor(min(xx))
        nh = math.ceil(max(yy)) - math.floor(min(yy))

        M[0, 2] += (nw - w) * 0.5
        M[1, 2] += (nh - h) * 0.5

        w, h = int(nw), int(nh)

590
    if len(img.shape) == 3 and img.shape[2] == 1:
591 592 593 594 595 596 597
        return cv2.warpAffine(
            img,
            M,
            (w, h),
            flags=_cv2_interp_from_str[interpolation],
            borderValue=fill,
        )[:, :, np.newaxis]
598
    else:
599 600 601 602 603 604 605
        return cv2.warpAffine(
            img,
            M,
            (w, h),
            flags=_cv2_interp_from_str[interpolation],
            borderValue=fill,
        )
606 607


608 609 610 611 612 613 614
def perspective(img, startpoints, endpoints, interpolation='nearest', fill=0):
    """Perspective the image.

    Args:
        img (np.array): Image to be perspectived.
        startpoints (list[list[int]]): [top-left, top-right, bottom-right, bottom-left] of the original image,
        endpoints (list[list[int]]): [top-left, top-right, bottom-right, bottom-left] of the transformed image.
615
        interpolation (int|str, optional): Interpolation method. If omitted, or if the
616
            image has only one channel, it is set to cv2.INTER_NEAREST.
617 618 619
            when use cv2 backend, support method are as following:
            - "nearest": cv2.INTER_NEAREST,
            - "bilinear": cv2.INTER_LINEAR,
620 621 622 623 624 625 626 627 628 629 630 631 632 633
            - "bicubic": cv2.INTER_CUBIC
        fill (3-tuple or int): RGB pixel fill value for area outside the rotated image.
            If int, it is used for all channels respectively.

    Returns:
        np.array: Perspectived image.

    """
    cv2 = try_import('cv2')
    _cv2_interp_from_str = {
        'nearest': cv2.INTER_NEAREST,
        'bilinear': cv2.INTER_LINEAR,
        'area': cv2.INTER_AREA,
        'bicubic': cv2.INTER_CUBIC,
634
        'lanczos': cv2.INTER_LANCZOS4,
635 636 637 638 639 640 641 642
    }
    h, w = img.shape[0:2]

    startpoints = np.array(startpoints, dtype="float32")
    endpoints = np.array(endpoints, dtype="float32")
    matrix = cv2.getPerspectiveTransform(startpoints, endpoints)

    if len(img.shape) == 3 and img.shape[2] == 1:
643 644 645 646 647 648 649
        return cv2.warpPerspective(
            img,
            matrix,
            dsize=(w, h),
            flags=_cv2_interp_from_str[interpolation],
            borderValue=fill,
        )[:, :, np.newaxis]
650
    else:
651 652 653 654 655 656 657
        return cv2.warpPerspective(
            img,
            matrix,
            dsize=(w, h),
            flags=_cv2_interp_from_str[interpolation],
            borderValue=fill,
        )
658 659


660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
def to_grayscale(img, num_output_channels=1):
    """Converts image to grayscale version of image.

    Args:
        img (np.array): Image to be converted to grayscale.

    Returns:
        np.array: Grayscale version of the image.
            if num_output_channels = 1 : returned image is single channel

            if num_output_channels = 3 : returned image is 3 channel with r = g = b

    """
    cv2 = try_import('cv2')

    if num_output_channels == 1:
        img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[:, :, np.newaxis]
    elif num_output_channels == 3:
        # much faster than doing cvtColor to go back to gray
        img = np.broadcast_to(
680 681
            cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[:, :, np.newaxis], img.shape
        )
682 683 684 685 686 687 688 689 690 691 692 693 694
    else:
        raise ValueError('num_output_channels should be either 1 or 3')

    return img


def normalize(img, mean, std, data_format='CHW', to_rgb=False):
    """Normalizes a ndarray imge or image with mean and standard deviation.

    Args:
        img (np.array): input data to be normalized.
        mean (list|tuple): Sequence of means for each channel.
        std (list|tuple): Sequence of standard deviations for each channel.
695
        data_format (str, optional): Data format of img, should be 'HWC' or
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
            'CHW'. Default: 'CHW'.
        to_rgb (bool, optional): Whether to convert to rgb. Default: False.

    Returns:
        np.array: Normalized mage.

    """

    if data_format == 'CHW':
        mean = np.float32(np.array(mean).reshape(-1, 1, 1))
        std = np.float32(np.array(std).reshape(-1, 1, 1))
    else:
        mean = np.float32(np.array(mean).reshape(1, 1, -1))
        std = np.float32(np.array(std).reshape(1, 1, -1))
    if to_rgb:
        # inplace
712
        img = img[..., ::-1]
713 714 715

    img = (img - mean) / std
    return img
716 717 718 719 720


def erase(img, i, j, h, w, v, inplace=False):
    """Erase the pixels of selected area in input image array with given value.

721 722 723 724 725 726 727 728
    Args:
         img (np.array): input image array, which shape is (H, W, C).
         i (int): y coordinate of the top-left point of erased region.
         j (int): x coordinate of the top-left point of erased region.
         h (int): Height of the erased region.
         w (int): Width of the erased region.
         v (np.array): value used to replace the pixels in erased region.
         inplace (bool, optional): Whether this transform is inplace. Default: False.
729

730 731
     Returns:
         np.array: Erased image.
732

733 734 735 736
    """
    if not inplace:
        img = img.copy()

737
    img[i : i + h, j : j + w, ...] = v
738
    return img