图像被改变成不同的颜色(带枕头),如何恢复到原来的颜色?

Images are changed to different colors (with pillow), how to get it back to the original colors?

我试图在视频的帧中找到主色。这很有效,但是,我的框架以某种方式转换为不同的颜色。 Yellow/pink 变为 blue/purple-ish,但黑色和白色保持不变(因此不是反转颜色)。

有谁知道它的来源以及如何更改它以保留原始颜色?这是我的代码:

import cv2
from sklearn.cluster import KMeans
from collections import Counter
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.patches as patches

video = cv2.VideoCapture('video.mp4')

def show_blurred_image(image, dominant_color):
    frame_to_blur = Image.fromarray(image)
    
    blurred_frame = cv2.blur(image, (200,200))
    blurred_frame = Image.fromarray(blurred_frame)
    
    plt.subplot(121),plt.imshow(frame_to_blur),plt.title('Original')
    plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(blurred_frame),plt.title('Blurred')
    plt.xticks([]), plt.yticks([])

    
    R = round(dominant_color[0])
    G = round(dominant_color[1])
    B = round(dominant_color[2])

    custom_color = '#%02x%02x%02x' % (R, G, B)
    print(custom_color)
    rect = patches.Rectangle((1620,0),300,1080,linewidth=1,
                             fill = True,
                             edgecolor=custom_color,
                             facecolor=custom_color)
    ax = plt.gca()
    ax.add_patch(rect)
    
    plt.show()
    

def get_dominant_color(image, k=4, image_processing_size = None):
    """
    takes an image as input
    returns the dominant color of the image as a list
    
    dominant color is found by running k means on the 
    pixels & returning the centroid of the largest cluster

    processing time is sped up by working with a smaller image; 
    this resizing can be done with the image_processing_size param 
    which takes a tuple of image dims as input

    >>> get_dominant_color(my_image, k=4, image_processing_size = (25, 25))
    [56.2423442, 34.0834233, 70.1234123]
    """
    #resize image if new dims provided
    if image_processing_size is not None:
        image = cv2.resize(image, image_processing_size, 
                            interpolation = cv2.INTER_AREA)
    
    #reshape the image to be a list of pixels
    image = image.reshape((image.shape[0] * image.shape[1], 3))

    #cluster and assign labels to the pixels 
    clt = KMeans(n_clusters = k)
    labels = clt.fit_predict(image)

    #count labels to find most popular
    label_counts = Counter(labels)

    #subset out most popular centroid
    dominant_color = clt.cluster_centers_[label_counts.most_common(1)[0][0]]

    return list(dominant_color)

dominant_colors = []
show_frame = 10
frame_nb = 0

while(video.isOpened()):
    ret, frame = video.read()
    
    if ret == True: 
        if (frame_nb == show_frame):
            dominant_color = get_dominant_color(frame)
            show_blurred_image(frame, dominant_color)
    
        frame_nb += 1
    else:
        break
    
        
video.release()
cv2.destroyAllWindows()

OpenCV 以 BGR 格式加载图像,而 PIL 和 matplotlib 使用 RGB 格式。如果您想一起使用这些库,则需要在正确的色彩空间中转换图像。

你的情况:

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)