[PYTHON] [Image processing] Posterization

1. Overview

Introducing "Posterization", the basic technology of image processing. Posterization is a conversion using a tone curve on the stairs, which limits the output pixel value to several levels.

As shown in the figure below, the pixel value of the input image is set to n levels (4 levels in the figure below) to limit the pixel value of the output image. By doing so, the borders become clearer and you can express colors like art.

Posterization is a book ["Digital Image Processing"](https://www.amazon.co.jp/%E3%83%87%E3%82%A3%E3%82%B8%E3%82%BF%E3% 83% AB% E7% 94% BB% E5% 83% 8F% E5% 87% A6% E7% 90% 86-% E6% 94% B9% E8% A8% 82% E7% AC% AC% E4% BA % 8C% E7% 89% 88-% E3% 83% 87% E3% 82% A3% E3% 82% B8% E3% 82% BF% E3% 83% AB% E7% 94% BB% E5% 83% 8F% E5% 87% A6% E7% 90% 86% E7% B7% A8% E9% 9B% 86% E5% A7% 94% E5% 93% A1% E4% BC% 9A / dp / 490347464X / ref = asc_df_490347464X /? tag = jpgo-22 & linkCode = df0 & hvadid = 342595526565 & hvpos = & hvnetw = g & hvrand = 5492134153117811443 & hvpone = & hvptwo = & hvqmt = & hvdev = c & hvdvcmdl = & hvlocint = & hvlocphy = 1937 It follows the processing method that is used. Since the tone curve that leaves pixel values 0 and 255 in the processed image is used, the image has a clear contrast.


2. Environment

The environment in which the posterization process was tried is as follows.

Python 3.6.8 numpy 1.18.1 matplotlib 3.1.2 opencv-python

Since OpenCV is used in the library, please install it in advance.

pip install opencv-python

3. Code

When I looked at various sites about posterization, many sites used LUTs that did not reflect white with a pixel value of 255 in the output image.

This process also reflects the pixel value of 255 "white" in the output image, so I devised a LUT creation. The input and output of bins are separated, but please refer to the comment section of the code for details.


#coding: utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt

def main():
    img = cv2.imread('image.jpg')                   #Loading images
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)    #Grayscale
    n = 4                                           #Number of pixel value divisions

    pos = posterization(gray, n)
    cv2.imwrite('pos.jpg', pos)          #Output of posterized image

def posterization(img, n):
    x = np.arange(256)                   #0,1,2...An array of integers up to 255

    ibins = np.linspace(0, 255, n+1)     #Input from LUT is 255/(n+1)Split with
    obins = np.linspace(0,255, n)        #Output from LUT is 255/Split by n

    num=np.digitize(x, ibins)-1          #Number the input pixel values to posterize
    num[255] = n-1                       #Correct the number of pixel value 255 that is off by digitize processing

    y = np.array(obins[num], dtype=int)   #Create a posterizing LUT
    pos_LUT(n, y)                         #Create LUT diagram
    pos = cv2.LUT(img, y)                 #Perform posterization

    return pos

#Posterization LUT diagram creation
def pos_LUT(n, y):
    x = np.arange(0,256,1)
    plt.savefig("pos_LUT.png ")

if __name__=='__main__':

4. References

★ Books ・ [Digital Image Processing [Revised Second Edition](Public Interest Incorporated Foundation Image Information Education Promotion Association (CG-ARTS))] [0] [0]:https://www.amazon.co.jp/%E3%83%87%E3%82%A3%E3%82%B8%E3%82%BF%E3%83%AB%E7%94%BB%E5%83%8F%E5%87%A6%E7%90%86-%E6%94%B9%E8%A8%82%E7%AC%AC%E4%BA%8C%E7%89%88-%E3%83%87%E3%82%A3%E3%82%B8%E3%82%BF%E3%83%AB%E7%94%BB%E5%83%8F%E5%87%A6%E7%90%86%E7%B7%A8%E9%9B%86%E5%A7%94%E5%93%A1%E4%BC%9A/dp/490347464X/ref=asc_df_490347464X/?tag=jpgo-22&linkCode=df0&hvadid=342595526565&hvpos=&hvnetw=g&hvrand=5492134153117811443&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=1009247&hvtargid=pla-889374643658&psc=1&th=1&psc=1

★ Site -[OpenCV-How to use cv2.LUT, gamma correction, negative / positive inversion, posterization] [1] [1]:https://pystyle.info/opencv-tone-transform/#outline__6 ・ [How to use digitize (numpy)] [2] [2]:http://arduinopid.web.fc2.com/Q3-36.html

Recommended Posts

[Image processing] Posterization
python image processing
Image processing 100 knocks ①
Image processing with MyHDL
Flat Field image processing
First Python image processing
Read digital image processing
Image processing with Python
Image Processing with PIL
Image processing with Python (Part 2)
opencv-python Introduction to image processing
Image processing with PIL (Pillow)
Digital image processing (spatial filtering)
100 image processing knocks !! (011 --020) Early game
100 image processing knocks !! (001 --010) Carefully and carefully
Image processing with Python (Part 1)
Image processing with Python (Part 3)
Image processing by python (Pillow)
Image Processing Collection in Python
Image expansion and contraction processing
[Python] Image processing with scikit-image
Real-time image processing basics with opencv
Personal notes for python image processing
Image processing with Python 100 knocks # 3 Binarization
Image processing 100 knocks Q9, Q10 (filter) speedup
Environmentally friendly scraping using image processing
Image processing with Python 100 knocks # 2 Grayscale
Image processing | predicting species from images
Basics of binarized image processing with Python
Image processing with Python 100 knock # 10 median filter
Image processing by Python 100 knock # 1 channel replacement
Create an image processing viewer with PySimpleGUI
Image processing with Python 100 knocks # 8 Max pooling
Grayscale by matrix-Reinventor of Python image processing-
Image processing with Python & OpenCV [Tone Curve]
Image processing with Python 100 knock # 12 motion filter
100 image processing by Python Knock # 6 Color reduction processing
Image denoising
Drawing with Matrix-Reinventor of Python Image Processing-
Easy image processing in Python with Pillow
Image recognition
Image processing engineer certification (expert) pass strategy
Image processing with Python 100 knocks # 7 Average pooling
Image processing 100 knock Q.6. Color reduction processing explanation
Image crawler
Light image processing with Python x OpenCV
Image processing with Lambda + OpenCV (gray image creation)
Matrix Convolution Filtering-Reinventor of Python Image Processing-
Image processing with Python 100 knocks # 9 Gaussian filter
XavierNX accelerates OpenCV image processing with GPU (CUDA)
(Image processing) Petit Thinning --Filter that performs loose thinning processing
I tried image processing like an event camera
Image processing from scratch with python (5) Fourier transform
Image processing from scratch with python (4) Contour extraction
Image processing? The story of starting Python for
Image Processing with Python Environment Setup for Windows
How to use PyTorch-based image processing library "Kornia"
Image processing by Python 100 knock # 11 smoothing filter (average filter)
Pack Japanese processing software into a Docker image
I tried simple image processing with Google Colaboratory.