簡體   English   中英

將圖片的 RGB 值更改為最接近的預定義 RGB 值

[英]Changing RGB values of a picture to closest predefined RGB values

所以我有一個預定義的調色板,我想用它為圖片着色。 我正在研究如何做到這一點,到目前為止發現了這一點。

import scipy.spatial as sp
import matplotlib.pyplot as plt
import cv2
import numpy

avg = image.open("sourcefile.png")

#Color palette I would ike to use

main_colors = [(100, 100, 100),
               (204, 0, 0),
               (0, 174, 0),
               (102, 51, 153),
               (255, 102, 0),
               (0, 0, 155)]

h, w, bpp = numpy.shape(avg)

# Change colors of each pixel
for py in range(0, h) :
    for px in range(0, w):
        input_color = avg[py][px][0], avg[py][px][1], avg[py][px][2]
        tree = sp.KDTree(main_colors)
        distance, result = tree.query(input_color)
        nearest_color = main_colors[result]

        avg[py][px][0] = nearest_color[0]
        avg[py][px][1] = nearest_color[1]
        avg[py][px][2] = nearest_color[2]

# show image
plt.figure()
plt.axis("off")
plt.imshow(avg)

我相信,我真的很接近解決方案,但是,我似乎做不到。 有人可以幫我調試嗎? 我不斷收到這個,我不知道如何解決這個問題

 File "/Average Pictures.py", line 22, in <module>
    input_color = avg[py][px][0], avg[py][px][1], avg[py][px][2]
TypeError: 'Image' object is not subscriptable

在查看@Quang Hong 的評論后,我也更改了代碼:

import scipy.spatial as sp
import matplotlib.pyplot as plt
import cv2
import numpy 

...
...
...


# Blending loaded Images
avg = Image.open(imlist[0])
for i in range(1, N):
    img = Image.open(imlist[i])
    avg = Image.blend(avg, img, 1.0 / float(i + 1))

avg1 = Image.new("RGB", avg.size)
avg1 = asarray(avg1)

main_colors =numpy.array([(100, 100, 100), 
               (204, 0, 0),  
               (0, 174, 0),  
               (102, 51, 153),
               (255, 102, 0), 
               (0, 0, 155),
               ])

main_colors = numpy.array(main_colors)
dist_mat = sp.distance_matrix(avg1.reshape(-1,3), main_colors)
color_idx = dist_mat.argmax(axis=1)

nearest_colors = main_colors[color_idx].reshape(avg1.shape)

fig, axes = plt.subplots(1, 2)
axes[0].imshow(avg)  # original image
axes[1].imshow(nearest_colors)  # nearest_color
plt.show()

但是現在的問題是,它不是 output 正確的圖像。

輸出

有人可以幫忙嗎? 對不起,我是一個絕對的菜鳥。

我將使用distance_matrix計算像素/參考顏色之間的距離,然后使用 argmin 來提取argmin

# sample image
np.random.seed(1)
avg = np.random.randint(0,255, (10,10,3), dtype=np.uint8)

# in your code, make sure `avg` is an np array
# you can use `cv2.imread` for that purpose, not the BGR color space
# for example
# avg = cv2.imread('image.png')
# agv = cv2.cvtColor(avg, cv2.BGR2RGB) # convert to RGB

# convert main_colors to np array for indexing
main_colors = np.array(main_colors)

# compute the distance matrix
dist_mat = distance_matrix(avg.reshape(-1,3), main_colors)

# extract the nearest color by index
color_idx = dist_mat.argmax(axis=1)

# build the nearest color image with indexing
nearest_colors = main_colors[color_idx].reshape(avg.shape)

# plot
fig, axes = plt.subplots(1,2)
axes[0].imshow(avg)             # original image
axes[1].imshow(nearest_colors)  # nearest_color
plt.show()

Output:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM