[Small story] Test image generation with Python / OpenCV

Test image generation

--The created image processing algorithm often verifies the effect with a test image. [^ 1] --In some cases, it may be more effective at the initial stage of verification to use a simpler pattern than a natural image, such as when you want to verify the calculation result of an algorithm with fine numerical values. [^ 1]: [lena --wikipedia](https://ja.wikipedia.org/wiki/%E3%83%AC%E3%83%8A_(%E7%94%BB%E5%83%8F%E3) % 83% 87% E3% 83% BC% E3% 82% BF)) is often used

--For example, it is a checker pattern or a test pattern used for broadcasting. [^ 2] [^ 2]: [Test pattern (broadcast) --wikipedia](https://ja.wikipedia.org/wiki/%E3%83%86%E3%82%B9%E3%83%88%E3%83% 91% E3% 82% BF% E3% 83% BC% E3% 83% B3_ (% E6% 94% BE% E9% 80% 81)) --So I wrote the code using Python and OpenCV. (Almost personal memorandum)

Operating environment

$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  

code

Sample code to generate grayscale and color bars

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()

grayscale

gray_scale.png

Color bar

full_color_bar.png

Please check your monitor

Recommended Posts

[Small story] Test image generation with Python / OpenCV
Image editing with python OpenCV
JPEG image generation by specifying quality with Python + OpenCV
[Python] Using OpenCV with Python (Image Filtering)
[Python] Using OpenCV with Python (Image transformation)
[Small story] Get timestamp with Python
Find image similarity with Python + OpenCV
Gradation image generation with Python [1] | np.linspace
Image processing with Python & OpenCV [Tone Curve]
Image acquisition from camera with Python + OpenCV
Light image processing with Python x OpenCV
Primality test with Python
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
Binarization with OpenCV / Python
python small story collection
How to crop an image with Python + OpenCV
Primality test with python
I tried "binarizing" the image with Python + OpenCV
Image processing with Python
Create miscellaneous Photoshop videos with Python + OpenCV ② Create still image Photoshop
Image processing with Python (Part 2)
"Apple processing" with OpenCV3 + Python3
Camera capture with Python + OpenCV
Sorting image files with Python (2)
Sorting image files with Python (3)
Stumble story with Python array
Image processing with Python (Part 1)
Tweet with image in Python
Sorting image files with Python
Image processing with Python (Part 3)
Face detection with Python + OpenCV
Get image features with OpenCV
Using OpenCV with Python @Mac
Image recognition with Keras + OpenCV
[Python] Image processing with scikit-image
I tried "gamma correction" of the image with Python + OpenCV
[Small story] [Python] Replace strings in 2D arrays with numbers
[Python] Easy reading of serial number image files with OpenCV
Shining life with Python and OpenCV
Cut out an image with python
Real-time image processing basics with opencv
Neural network with OpenCV 3 and Python 3
Image processing with Python 100 knocks # 3 Binarization
Easy Python + OpenCV programming with Canopy
Let's do image scraping with Python
Try face recognition with python + OpenCV
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7
Load gif images with Python + OpenCV
Password generation in texto with python
CSRF countermeasure token generation with Python
Try blurring the image with opencv2
Use OpenCV with Python 3 in Window
Image processing with Python 100 knocks # 2 Grayscale
Draw an illustration with Python + OpenCV
Introduction to image analysis opencv python
Track baseball balls with Python + OpenCV
Graph Based Segmentation with Python + OpenCV
Send image with python, save with php
Draw arrows (vectors) with opencv / python