Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fit_output (False/True) option to PerspectiveTransform augmenter #452

Merged
merged 2 commits into from
Oct 13, 2019
Merged

Add fit_output (False/True) option to PerspectiveTransform augmenter #452

merged 2 commits into from
Oct 13, 2019

Conversation

ZhengRui
Copy link
Contributor

@ZhengRui ZhengRui commented Oct 9, 2019

Recalculate the transformation matrix to cover all the input content after transformation.

Sample

fit_output=False:
image
image
fit_output=True:
image
image

import cv2
import numpy as np
import imgaug as ia


def ia_augmentation_text(images, random_state, parents, hooks):
    for image in images:
        h, w = image.shape[:2]
        y_text = np.random.randint(int(0.75*h), h-20)
        s_text = np.random.randint(20, max(int(0.1*h), 30))
        n_text = 2 * (w-20) // s_text

        image[...] = ia.draw_text(image, y_text, 10, random_string(n_text), color=(255, 255, 255), size=s_text)

    return images

ia_augmenter_text = iaa.Lambda(func_images=ia_augmentation_text)
ia_sometimes = lambda aug: iaa.Sometimes(0.5, aug)
ia_augmenter_img = iaa.Sequential([
    # geometrical
    ia_sometimes(
        iaa.OneOf([
            # iaa.Affine(
            #     rotate=(-30, 30),
            #     shear=(-16, 16),
            #     order=[0, 1],
            #     cval=(0, 255),
            #     mode='constant',
            #     fit_output=True,
            # ),
            iaa.PerspectiveTransform(scale=(0.01, 0.15),
                                     fit_output=True)
        ]),
    ),
    # blur mosaic
    ia_sometimes(
        iaa.OneOf([
            iaa.GaussianBlur((0, 5.0)),
            iaa.MotionBlur(k=15),
            iaa.JpegCompression(compression=(85, 100)),
        ]),
    ),
    # noise blackout
    ia_sometimes(
        iaa.OneOf([
            iaa.AdditiveGaussianNoise(scale=0.2*255, per_channel=0.5),
            iaa.Dropout(p=(0, 0.2), per_channel=0.5),
            iaa.CoarseDropout(0.2, size_percent=0.05, per_channel=0.5),
            iaa.CoarseSaltAndPepper(0.2, size_percent=0.05, per_channel=0.5),
        ])
    ),
    # contrast brightness
    ia_sometimes(
        iaa.OneOf([
            iaa.LinearContrast((0.5, 1.5)),
            iaa.GammaContrast((0.5, 2.0)),
            iaa.AddToSaturation((-50, 50))
        ])
    ),
    # color changes
    ia_sometimes(
        iaa.OneOf([
            iaa.UniformColorQuantization(n_colors=16),
            iaa.Grayscale(alpha=(0.0, 1.0))
        ])
    ),
    # text
    ia_sometimes(
        ia_augmenter_text
    ),
    # horizontal flip
    ia_sometimes(
        iaa.Fliplr(0.5)
    )
])

bbox=np.array([210,163,129,187])
im = cv2.imread('d535880338a040b426d215e35cd122c1.jpg')
cv2.rectangle(im, (bbox[0], bbox[1]), (bbox[0]+bbox[2], bbox[1]+bbox[3]), (0,255,0), 1, cv2.LINE_AA)
images = im[np.newaxis, ...]
bbs=[[ia.BoundingBox(x1=bbox[0], y1=bbox[1], x2=bbox[0]+bbox[2], y2=bbox[1]+bbox[3])]]

while True:
    ims_out, bboxes_out = ia_augmenter_img(images=images, bounding_boxes=bbs)
    im_out = ims_out[0]
    bbox_out = bboxes_out[0][0]
    print(bbox_out)
    cv2.rectangle(im_out, (int(bbox_out.x1), int(bbox_out.y1)), (int(bbox_out.x2), int(bbox_out.y2)), (0,0,255), 1, cv2.LINE_AA)
    cv2.imshow('aug', im_out)
    if cv2.waitKey(0) & 0xFF == ord("q"):
        break

@ZhengRui
Copy link
Contributor Author

ZhengRui commented Oct 9, 2019

The above example is tested on 0.3.0. When test on current master 9b64c15, the box (red) looks different from before, not sure if it is a recent bug or due to some API change.
image
image

@codecov-io
Copy link

Codecov Report

Merging #452 into master will decrease coverage by 0.08%.
The diff coverage is 25%.

@@            Coverage Diff             @@
##           master     #452      +/-   ##
==========================================
- Coverage   96.24%   96.16%   -0.08%     
==========================================
  Files          34       34              
  Lines       11080    11092      +12     
==========================================
+ Hits        10663    10666       +3     
- Misses        417      426       +9

@aleju aleju merged commit d08d962 into aleju:master Oct 13, 2019
@aleju
Copy link
Owner

aleju commented Oct 13, 2019

Looks good to me. Merged into master. Thanks for the help!

The problem in the second set of images looks like it augments BBs based only on their top-left and bottom-right coordinates instead of using all four corners. I'll fix that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants