9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Flipと回転による画像水増し

Last updated at Posted at 2018-06-20

超解像で遊んでいたら、学習データの拡張でflipと回転をする必要があったのでメモ。
超解像のための学習データを拡張する場合には、画像が劣化するinterpolationは利用しない。このため、flipと0, 90, 180, 270度の回転の組み合わせで8通りのデータ拡張を行う。
これらの回転は cv2.flipcv2.transpose で実現する。

import numpy as np
import cv2


def main():
    base_w, base_h = 40, 30
    patch_size = max(base_w, base_h)
    base_image = np.ones((base_h, base_w, 3), dtype=np.uint8) * 255
    font = cv2.FONT_HERSHEY_PLAIN
    cv2.putText(base_image, "test", (0, base_h), font, 1, (255, 0, 0))
    output_image = np.zeros((patch_size, patch_size * 8, 3), dtype=np.uint8)
    i = 0

    for image1 in [base_image, cv2.transpose(base_image)]:
        for image2 in [image1, cv2.flip(image1, 0)]:
            for image3 in [image2, cv2.flip(image2, 1)]:
                h, w, _ = image3.shape
                output_image[:h, i * patch_size:i * patch_size + w] = image3
                i += 1

    cv2.imshow("output image", output_image)
    cv2.waitKey(-1)


if __name__ == '__main__':
    main()

Input:
base.png

Output:
aug.png

ちゃんとflipと回転角を指定する場合は、ちゃんと組み合わせを考える必要があるが、実はnumpy.rot90という関数があり、その実装が参考になる。

flipまで組み合わせると、全部やってくれる実装はないので、自分で実装する必要がある。rot90 + flipの組み合わせだと、微妙に無駄な処理が発生することがある。気にしないといけないケースは殆どないと思われるが…

まとめると下記のような結果になる。

rot_flip.png

9
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?