LoginSignup
3
4

More than 3 years have passed since last update.

【小ネタ】Python/OpenCVでテスト画像生成

Last updated at Posted at 2020-04-24

テスト画像生成

  • 作成した画像処理アルゴリズムは、テスト用画像で効果を検証することが多い。1
  • アルゴリズムの演算結果を細かい数値で検証したい場合など、自然画像よりも単純なパターンであったほうが検証の初期段階では有効な場合がある。
  • 例えば、チェッカーパターンであったり、放送に使われるテストパターンなどである。2
  • そこで Python と OpenCV を使ってコードを書いてみた。 (ほとんど個人的な備忘録)

動作環境

$python --version
Python 3.8.2

$pip list
Package       Version 
------------- --------
numpy         1.18.3  
opencv-python 4.2.0.34
pip           20.0.2  
setuptools    46.1.3  
wheel         0.34.2  

コード

グレースケールとカラーバーを生成するサンプルコード

import cv2
import numpy as np
from enum import Enum

def gray_scale_chart():
    width = 1920
    height = 1080
    gray_img = np.zeros((height,width), dtype=np.uint8)
    val = 0
    bar_width = 120
    step = 17
    for i in range(0, width, bar_width):
        gray_img[0:height,i:i+bar_width]=val
        val += step
    cv2.imwrite("gray_scale.bmp", gray_img)


class Color(Enum):
    WHITE=(255,255,255)
    YELLOW=(0,255,255)
    CYAN=(255,255,0)
    GREEN=(0,255,0)
    MAGENTA=(255,0,255)
    RED=(0,0,255)
    BLUE=(255,0,0)
    BLACK=(0,0,0)


def full_color_bar():
    width = 1920
    height = 1080
    color_img = np.zeros((height,width,3), dtype=np.uint8)
    bar_width = 240
    i = 0
    for c in Color:
        color_img[0:height,i:i+bar_width]=c.value
        i += bar_width
    cv2.imwrite("full_color_bar.bmp",color_img)


if __name__=='__main__':
    gray_scale_chart()
    full_color_bar()

グレースケール

gray_scale.png

カラーバー

full_color_bar.png

モニタのチェックにどうぞ

3
4
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
3
4