100 image processing by Python Knock # 6 Color reduction processing

Introduction

Hi, I'm Ramu. This time, we will implement color reduction processing to reduce the number of colors in the image. By the way, the reason why one is skipped from the last time is that the fifth HSV conversion could not be implemented.

6th: Color reduction processing

As the name implies, color reduction processing is processing that reduces the number of colors. In a normal image, there are 256 colors of [0: 255] each in BGR, and there is a combination of $ 256 ^ 3 = 16,777,216 $ colors for one pixel value. In this process, each BGR is reduced to 4 colors of [32,96,160,224], and one pixel value is reduced to $ 4 ^ 3 = 64 $ colors.

This time, color reduction is performed according to the following formula.

pix = {  32 (  0 <= pix <  64)
         96 ( 64 <= pix < 128)
        160 (128 <= pix < 192)
        224 (192 <= pix < 256)

Source code

decreaseColor.py


import numpy as np
import cv2
import matplotlib.pyplot as plt

def decreaseColor(img):
  dst = img.copy()

  idx = np.where((0<=img) & (64>img))
  dst[idx] = 32
  idx = np.where((64<=img) & (128>img))
  dst[idx] = 96
  idx = np.where((128<=img) & (192>img))
  dst[idx] = 160
  idx = np.where((192<=img) & (256>img))
  dst[idx] = 224

  return dst

#Image reading
img = cv2.imread('../assets/imori.jpg')

#Color reduction processing
img = decreaseColor(img)

#Image display
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()

ファイル名 ファイル名

The image on the left is the input image, and the image on the right is the output image. You can see that the color is reduced well. The output result looks like a solid color on a similar color.

in conclusion

If you have any questions, please feel free to contact us. imori_imori's Github has the official answer, so please check that as well. .. Also, since python is a beginner, please kindly point out any mistakes.

Recommended Posts

100 image processing by Python Knock # 6 Color reduction processing
Image processing 100 knock Q.6. Color reduction processing explanation
Image processing by python (Pillow)
Image processing by Python 100 knock # 11 smoothing filter (average filter)
100 Language Processing Knock Chapter 1 by Python
python image processing
Image processing with Python 100 knock # 10 median filter
Grayscale by matrix-Reinventor of Python image processing-
Communication processing by Python
First Python image processing
Image processing with Python
Image processing with Python (Part 2)
100 Language Processing with Python Knock 2015
Image processing by matrix Basics & Table of Contents-Reinventor of Python image processing-
100 Language Processing Knock Chapter 1 (Python)
100 Language Processing Knock Chapter 2 (Python)
Image processing with Python (Part 1)
Image processing with Python (Part 3)
Image Processing Collection in Python
[Python] Image processing with scikit-image
100 Language Processing Knock with Python (Chapter 1)
100 Language Processing Knock Chapter 1 in Python
100 Language Processing Knock with Python (Chapter 3)
Personal notes for python image processing
Python beginner tried 100 language processing knock 2015 (05 ~ 09)
Image processing with Python 100 knocks # 2 Grayscale
Python beginner tried 100 language processing knock 2015 (00 ~ 04)
Straight line drawing by matrix-Inventor's original research of Python image processing-
Basics of binarized image processing with Python
Socket communication and multi-thread processing by Python
100 Language Processing Knock-89: Analogy by Additive Constitutiveness
Image processing with Python 100 knocks # 8 Max pooling
"Data Science 100 Knock (Structured Data Processing)" Python-007 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-006 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-001 Explanation
100 Language Processing Knock with Python (Chapter 2, Part 2)
Image processing with Python & OpenCV [Tone Curve]
100 Language Processing Knock with Python (Chapter 2, Part 1)
"Data Science 100 Knock (Structured Data Processing)" Python-002 Explanation
Drawing with Matrix-Reinventor of Python Image Processing-
Easy image processing in Python with Pillow
Analysis of X-ray microtomography image by Python
[Python] Data Science 100 Knock (Structured Data Processing) 021 Explanation
Image processing with Python 100 knocks # 7 Average pooling
"Data Science 100 Knock (Structured Data Processing)" Python-005 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-004 Explanation
[Python] Data Science 100 Knock (Structured Data Processing) 020 Explanation
Light image processing with Python x OpenCV
[Python] Data Science 100 Knock (Structured Data Processing) 025 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-003 Explanation
Matrix Convolution Filtering-Reinventor of Python Image Processing-
Image processing with Python 100 knocks # 9 Gaussian filter
[Python] Data Science 100 Knock (Structured Data Processing) 019 Explanation
[Image processing] Posterization
Python inexperienced person tries to knock 100 language processing 14-16
Extract dominant color of image by k-means clustering
Color page judgment of scanned image with python
Image processing from scratch with python (5) Fourier transform
100 Language Processing Knock (2020): 28
Python file processing
Affine transformation by matrix (scaling, rotation, shearing, movement) -Reinventor of Python image processing-