5
6

More than 5 years have passed since last update.

OpenCVによるデータの水増し(lambda)

Last updated at Posted at 2019-05-15

はじめに

 画像解析を実施するためにOpenCVを用いてデータの水増しをする。OpenCVによる画像処理の効果はOpenCVによる画像処理の効果にて確認した。また、データの水増しをするプログラムをOpenCVによるデータの水増し(for文)で作成した。しかし、本文が長すぎたため本文を短縮する改良をする。

手順

  • 画像処理の種類と条件、水増しの量を次の表の通りとする。
  • 表の中身をリストとして本文に記述し、変数parametersに格納する。
  • 実行する画像処理の関数を変数methodsに格納する。
  • 画像を保存する関数を作成する。
  • 画像タイトル用ナンバーを初期化する。
  • 元画像を読み込む。
  • for文のループで画像を水増しする。
画像処理の種類 条件 水増しの量
リサイズ 1/2、1/3、1/5、1/7倍 4枚
モザイク 1/3、1/5、1/7、1/10倍 4枚
回転 45、135、225、270° 4枚
反転 x軸中心、y軸中心、両方 3枚
色調変換 - 1枚
色変換 - 1枚
THRESH_BINARY 閾値50、100、150、200 4枚
THRESH_BINARY_INV 閾値50、100、150、200 4枚
THRESH_TRUNC 閾値50、100、150、200 4枚
THRESH_TOZERO 閾値50、100、150、200 4枚
THRESH_TOZERO_INV 閾値50、100、150、200 4枚
ぼかし カーネルサイズ11×11、31×31、51×51、71×71 4枚
グレースケール - 1枚
ノイズ除去 color、gray 2枚
膨張 フィルター周囲4つ、8つ 2枚
収縮 フィルター周囲4つ、8つ 2枚
合計 - 48枚

水増しした画像の一例

  • 元画像
    img.jpg

  • モザイク
    img5.jpgimg6.jpg
    img7.jpgimg8.jpg

  • 回転
    img9.jpgimg10.jpg
    img11.jpgimg12.jpg

  • THRESH_BINARY_INV
    img22.jpgimg23.jpg
    img24.jpgimg25.jpg

  • ぼかし
    img38.jpgimg39.jpg
    img40.jpgimg41.jpg

OpenCVによるデータの水増し

OpenCVによるデータの水増し(for文)に対しプログラムを短縮する改良をした。

import numpy as np
import cv2

#画像変換条件
filter1 = np.array([[0, 1, 0],
                   [1, 0, 1],
                   [0, 1, 0]], np.uint8)
filter2 = np.ones((3, 3))

list_resize = [2, 3, 5, 7]
list_mosaic = [3, 5, 7, 10]
list_rotation = [45, 135, 225, 315]
list_flip = [0, 1, -1]
list_cvt1 = [0]
list_cvt2 = [0]
list_THRESH_BINARY = [50, 100, 150, 200]
list_THRESH_BINARY_INV = [50, 100, 150, 200]
list_THRESH_TRUNC = [50, 100, 150, 200]
list_THRESH_TOZERO = [50, 100, 150, 200]
list_THRESH_TOZERO_INV = [50, 100, 150, 200]
list_gauss = [11, 31, 51, 71]
list_gray = [0]
list_nois_gray = [0]
list_nois_color = [0]
list_dilate = [filter1, filter2]
list_erode = [filter1, filter2]

parameters = [list_resize, list_mosaic, list_rotation, list_flip, list_cvt1, list_cvt2, list_THRESH_BINARY, \
              list_THRESH_BINARY_INV, list_THRESH_TRUNC, list_THRESH_TOZERO, list_THRESH_TOZERO_INV, list_gauss, \
              list_gray, list_nois_gray, list_nois_color, list_dilate, list_erode]

#水増し画像の合計
list_sum =len(list_resize) + len(list_mosaic) + len(list_rotation) + len(list_flip) + len(list_cvt1) + len(list_cvt2) + \
          len(list_THRESH_BINARY) + len(list_THRESH_BINARY_INV) + len(list_THRESH_TRUNC) + len(list_THRESH_TOZERO) + \
          len(list_THRESH_TOZERO_INV) + len(list_gauss) + len(list_gray) + len(list_nois_gray) + len(list_nois_color) + \
          len(list_dilate) + len(list_erode)
print("合計:{}枚".format(list_sum))

#実行する関数のリスト
methods = np.array([lambda i: cv2.resize(img, (img.shape[1] // i, img.shape[0] // i)),
                    lambda i: cv2.resize(cv2.resize(img, (img.shape[1] // i, img.shape[0] // i)), (img.shape[1],img.shape[0])),
                    lambda i: cv2.warpAffine(img, cv2.getRotationMatrix2D(tuple(np.array([img.shape[1] / 2, img.shape[0] /2])), i, 1), (img.shape[1], img.shape[0])),
                    lambda i: cv2.flip(img, i),
                    lambda i: cv2.cvtColor(img, cv2.COLOR_BGR2LAB),
                    lambda i: cv2.bitwise_not(img),
                    lambda i: cv2.threshold(img, i, 255, cv2.THRESH_BINARY)[1],
                    lambda i: cv2.threshold(img, i, 255, cv2.THRESH_BINARY_INV)[1],
                    lambda i: cv2.threshold(img, i, 255, cv2.THRESH_TRUNC)[1],
                    lambda i: cv2.threshold(img, i, 255, cv2.THRESH_TOZERO)[1],
                    lambda i: cv2.threshold(img, i, 255, cv2.THRESH_TOZERO_INV)[1],  
                    lambda i: cv2.GaussianBlur(img, (i, i), 0),
                    lambda i: cv2.imread("./CleansingData/img.jpg", i),
                    lambda i: cv2.fastNlMeansDenoising(cv2.imread("./CleansingData/img.jpg", i)),
                    lambda i: cv2.fastNlMeansDenoisingColored(img),
                    lambda i: cv2.dilate(img, i),
                    lambda i: cv2.erode(img, i)
                   ])

#水増し画僧の保存用関数
def save(cnv_img):
    cv2.imwrite("CleansingData/img" + str(num) + ".jpg", cnv_img)

#画像タイトル用ナンバーの初期化
num = 0

#元画像の読み込み
img = cv2.imread("./CleansingData/img.jpg")

#画像の水増しと保存
for ind, method in enumerate(methods):
    for parameter in parameters[ind]:
        num += 1
        cnv_img = method(parameter)
        save(cnv_img)

おわりに

 OpenCVによるデータの水増し(for文)に比べて短いプログラムとすることができた。

5
6
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
5
6