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)
sample = im // 128 * 128 + 64
cv2_imshow(sample)
sample = im // 85 * 85 + 42
cv2_imshow(sample)
sample = im // 64 * 64 + 32
cv2_imshow(sample)
sample = im // 43 * 43 + 22
cv2_imshow(sample)
sample = im // 32 * 32 + 16
cv2_imshow(sample)
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