微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

图像被更改为不同的颜色带有枕头,如何使其恢复为原始颜色?

如何解决图像被更改为不同的颜色带有枕头,如何使其恢复为原始颜色?

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。