0
0

【Python】機械学習で使える簡単な教師データ作成

Last updated at Posted at 2021-11-01

概要

セグメンテーションタスクで使える入力画像・教師画像を作るのがいつもめんどくさいと感じていたため、保存用としておいておきます。
ランダムな場所にランダムな丸を作成するプログラムです。

  • 丸の大きさはテキトーにしてるので、都度直してください。
  • cv2.circleだけでなく、cv2.rectangle等を混ぜるとより良いかも。

こんなものができます(ちっちゃい!)。
input correct

想定内容

今回は図形(丸)の色を塗りつぶすセグメンテーションタスクを想定しています。
今回のソースをベースにしたら色んなパターンに応用できると思います。

ソースコード

OpenCVのthicknessを1(枠線1)と-1(塗りつぶし)にすることで2種類の画像を作成。
位置や丸の大きさはランダムに設定していますが、閾値等は使い方に合わせてください。

python
import cv2
import random

size = 32
dataNum = 500
imgDir = ./data

for i_num in range(dataNum):
        
    img = np.zeros((size, size))
    corImg = np.zeros((size, size))
        
    x, y = random.randint(0, size), random.randint(0, size)
    cclSize = random.randint(3, 8)
        
    cv2.circle(img, (x, y), cclSize, 255, thickness=1)
    cv2.circle(corImg, (x, y), cclSize, 255, thickness=-1)
        
    inputPath = imgDir + "/inputs/" + str(i_num).zfill(4) + ".png"
    correctPath = imgDir + "/corrects/" + str(i_num).zfill(4) + ".png"
    cv2.imwrite(inputPath, img)
    cv2.imwrite(correctPath, corImg)

その他

ソースコードはGitHubにもありますので是非。

0
0
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
0
0