【问题标题】:Images are changed to different colors (with pillow), how to get it back to the original colors?图像换成不同颜色(带枕头),如何恢复原来的颜色?
【发布时间】:2021-02-10 20:44:15
【问题描述】:

我正在尝试在视频的帧中找到主色。这很好用,但是,我的框架以某种方式转换为不同的颜色。黄色/粉红色变成蓝色/紫色,但黑色和白色保持不变(因此它不是倒置的颜色)。

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

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 需要 RGB。
  • 谢谢!这有帮助,我在 ret, frame = video.read(): frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 下面添加了一行,现在颜色正确了,太好了!

标签: python colors python-imaging-library video-processing


【解决方案1】:

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

在你的情况下:

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

【讨论】:

    猜你喜欢
    • 2012-10-02
    • 1970-01-01
    • 2018-11-26
    • 2019-08-26
    • 2012-06-07
    • 1970-01-01
    • 2017-10-14
    • 2021-07-09
    相关资源
    最近更新 更多