Esempio n. 1
0
def test_rgb2gray_gray2rgb():
    im_rgb = mv.gray2rgb(IM_GRAY)
    im_gray_2 = mv.rgb2gray(im_rgb)
    assert_image_equal(IM_GRAY, im_gray_2)

    im_gray_2 = cv2.cvtColor(im_rgb, cv2.COLOR_RGB2GRAY)
    assert_image_equal(IM_GRAY, im_gray_2)
Esempio n. 2
0
def normalize_rgb(img, mean, std):
    """ Normalize an image.

    Subtract mean per channel and divide by std per channel. (support
    grayscale image and RGB image).

    Args:
        img (ndarray): image to be normalized.
        mean (tuple[float] or float): mean values.
        std (tuple[float] or float): standard deviations.

    Return:
        (ndarray): the normalized RGB image.

    Note:
        For grayscale image, first rescale intensity to [0.0, 255.0].
        Then convert into a 3-channel RGB image before normalization.
    """
    img = img.astype(np.float32)

    if img.ndim == 2:
        img = normalize_grayscale(img, to_float=False) * 255.0
        img = mv.gray2rgb(img)

    return (img - mean) / std
Esempio n. 3
0
def imshow_bboxes(img,
                  bboxes,
                  score_thr=0,
                  colors=Color.green,
                  top_k=-1,
                  thickness=1,
                  font_scale=0.5,
                  show=True,
                  title='',
                  save_path=None):
    """ Draw bounding boxes on an image.

    To display detection result or compare detection results by different
    algorithms.

    Args:
        img (str or ndarray): image (or file path) to be displayed.
        bboxes (list or ndarray): a list of ndarray of shape (k, 4) or (n, 5).
        score_thr (float): minimum score of bboxes to be shown.
        colors (Color or list[Color]): color or list of colors.
        top_k (int): plot the first k bboxes only if set positive.
            Otherwise, plot all the bboxes.
        thickness (int): line thickness.
        font_scale (float): font scales of texts.
        show (bool): True: show the image; False: save the image.
        title (str): title of the plot.
        save_path (str, optional): path to save the image.
    """
    if isinstance(img, str):
        img = mv.imread(img)
    else:
        img = img if img.ndim == 3 else mv.gray2rgb(img)

    if isinstance(bboxes, np.ndarray):
        bboxes = [bboxes]

    if isinstance(colors, Color):
        colors = [colors] * len(bboxes)

    assert len(bboxes) == len(colors)

    plot_prob = True if bboxes[0].shape[1] == 5 else False

    if score_thr > 0:
        for i in range(len(bboxes)):
            assert bboxes[i].shape[1] == 5
            scores = bboxes[i][:, -1]
            indices = scores > score_thr
            bboxes[i] = bboxes[i][indices, :]

    img_with_result = img.copy()
    for i, _bboxes in enumerate(bboxes):
        _bboxes_int = _bboxes.astype(np.int32)
        if top_k <= 0:
            _top_k = _bboxes.shape[0]
        else:
            _top_k = min(top_k, _bboxes.shape[0])
        for j in range(_top_k):
            left_top = (_bboxes_int[j, 0], _bboxes_int[j, 1])
            right_bottom = (_bboxes_int[j, 2], _bboxes_int[j, 3])
            cv2.rectangle(img_with_result, left_top, right_bottom,
                          colors[i].value, thickness)
            if plot_prob:
                label_text = '%.2f' % _bboxes[j, -1]
                cv2.putText(img_with_result, label_text,
                            (_bboxes_int[j, 0], _bboxes_int[j, 1] - 2),
                            cv2.FONT_HERSHEY_COMPLEX, font_scale,
                            colors[i].value, thickness)

    if show:
        _imshow_switcher([img_with_result, img], title)

    if save_path is not None:
        mv.imwrite(save_path, img_with_result)