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