Image processing 100 knock Q.6. Color reduction processing explanation

Image processing 100 knock Q.6. This is a commentary article on color reduction processing.

Color reduction processing limits the types of possible values for R (red), G (green), and B (blue). As a result, the colors that can be expressed are reduced.

In the main subject, it is instructed to reduce R, G, B from 256 kinds of values (0 to 255) to 4 kinds of values (32,96,160,224) each.

In the original article, the color reduction processing is performed by this processing. (following)

def dicrease_color(img):
    out = img.copy()
    out = out // 64 * 64 + 32
    return out

I was surprised that there was less processing than I expected. The code in the following part of this function performs color reduction processing.

out = out // 64 * 64 + 32

The number 64 is 256 (0 to 255) divided by 4.

In other words, ʻout // 64` is checking which class (assuming there are 4 classes such as 0, 1, 2, 3) when 256 is divided into four.

And by setting * 64 + 32, it is set to the specified value of the class. (Class 0 is 32, Class 1 is 96, Class 2 is 160, Class 3 is 224.) This time, it seems that the value is near the center of the class. (Values belonging to class 0 are 0 to 63, with 32 and 33 in the center) For example, if ʻout // 64 is 2 (class 2), that is, if ʻout is 128 ~ 191, then ʻout // 64 becomes 2, and * 64 + 32` It is set to the specified value (160) of class 2.

If you can understand the above, color reduction as you like, such as 2 values each, 6 values each, and 8 values each You can do the processing. The code is presented below for reference. (* I am using Google Colaboratory. Some processing is required to load images, etc. I [How to use cv2.imshow () with Google Colab](https://qiita.com/ITF_katoyu/items/ I referred to 115528c98d2e558c6fc6).)

#Import support patches
from google.colab.patches import cv2_imshow

#Image import
!curl -o logo.png https://colab.research.google.com/img/colab_favicon_256px.png
import cv2

#Load the image and store it in img
img = cv2.imread("imori.jpg ", cv2.IMREAD_UNCHANGED)

#Copy img to a variable called im
im = img.copy()

#Display image
cv2_imshow(im)

ダウンロード (1).png

Classified into 2 values each

sample = im // 128 * 128 + 64
cv2_imshow(sample)

ダウンロード (2).png

Classified into 3 values each

sample = im // 85 * 85 + 42
cv2_imshow(sample)

ダウンロード (3).png

Classified into 4 values each (as in the original article)

sample = im // 64 * 64 + 32
cv2_imshow(sample)

ダウンロード (4).png

Classified into 6 values each

sample = im // 43 * 43 + 22
cv2_imshow(sample)

ダウンロード (5).png

Classified into 8 values each

sample = im // 32 * 32 + 16
cv2_imshow(sample)

ダウンロード (6).png It is surprising that the image quality is quite good, only expressing up to 8 values for each.

that's all. Let's continue to work on image processing 100 knocks.

Recommended Posts

Image processing 100 knock Q.6. Color reduction processing explanation
100 image processing by Python Knock # 6 Color reduction processing
Image processing 100 knocks Q9, Q10 (filter) speedup
Image processing with Python 100 knock # 10 median filter
Image processing by Python 100 knock # 1 channel replacement
"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
Image processing with Python 100 knock # 12 motion filter
"Data Science 100 Knock (Structured Data Processing)" Python-002 Explanation
[Python] Data Science 100 Knock (Structured Data Processing) 021 Explanation
"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
[Python] Data Science 100 Knock (Structured Data Processing) 025 Explanation
"Data Science 100 Knock (Structured Data Processing)" Python-003 Explanation
[Python] Data Science 100 Knock (Structured Data Processing) 019 Explanation
[Image processing] Posterization
python image processing
100 Language Processing Knock (2020): 28
100 Language Processing Knock (2020): 38
Image processing 100 knocks ①
100 language processing knock 00 ~ 02
Image processing by Python 100 knock # 11 smoothing filter (average filter)
100 language processing knock 2020 [00 ~ 39 answer]
100 language processing knock 2020 [00-79 answer]
100 language processing knock 2020 [00 ~ 69 answer]
Image processing with MyHDL
100 Amateur Language Processing Knock: 17
100 language processing knock 2020 [00 ~ 49 answer]
Flat Field image processing
100 Language Processing Knock-52: Stemming
First Python image processing
100 Language Processing Knock Chapter 1
Read digital image processing
100 Amateur Language Processing Knock: 07
100 Language Processing Knock 2020 Chapter 3
Extract image color (RGB)
Image processing with Python
100 Amateur Language Processing Knock: 09
100 Amateur Language Processing Knock: 47
100 Language Processing Knock-53: Tokenization
100 Amateur Language Processing Knock: 97
100 language processing knock 2020 [00 ~ 59 answer]
100 Amateur Language Processing Knock: 67
Image Processing with PIL
[Python] Data Science 100 Knock (Structured Data Processing) 001-010 Impressions + Explanation Link Summary