我试图从视频中获取最主要的颜色,当开始播放时想要实时绘制视频中的颜色,例如 6 个最主要的颜色,3 个等,我搜索了很多,但所有教程都只检测到三种颜色,红色、蓝色和绿色,有人可能会检测更多,因为他们自己设置值,使用 HSV 映射来设置检测哪些颜色,我的问题是这是一个视频,所以我不知道范围
while(True):
# Capture the video frame
# by frame
ret, frame = vid.read();
prev = time.time();
capture = cv.VideoCapture(args['file'])
img = cv.imread("./assets/taxi.jpeg");
rgb_color = cv.cvtColor(frame, cv.COLOR_BGR2RGB);
height, width, channel = rgb_color.shape;
histogram = cv.calcHist([frame],[0],None,[256],[0,256]);
plt.plot(histogram);
cv.imshow("histogram", plt);
Run Code Online (Sandbox Code Playgroud)
现在只需打开网络摄像头并显示直方图
小智 5
您可以尝试使用 kmeans 聚类来获取主色:
.reshape(-1, 3)代码:
import numpy as np
import cv2
cap = cv2.VideoCapture("BigBuckBunny.mp4")
n_clusters = 5
while True:
status, image = cap.read()
if not status:
break
# to reduce complexity resize the image
data = cv2.resize(image, (100, 100)).reshape(-1, 3)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
flags = cv2.KMEANS_RANDOM_CENTERS
compactness, labels, centers = cv2.kmeans(data.astype(np.float32), n_clusters, None, criteria, 10, flags)
cluster_sizes = np.bincount(labels.flatten())
palette = []
for cluster_idx in np.argsort(-cluster_sizes):
palette.append(np.full((image.shape[0], image.shape[1], 3), fill_value=centers[cluster_idx].astype(int), dtype=np.uint8))
palette = np.hstack(palette)
sf = image.shape[1] / palette.shape[1]
out = np.vstack([image, cv2.resize(palette, (0, 0), fx=sf, fy=sf)])
cv2.imshow("dominant_colors", out)
cv2.waitKey(1)
Run Code Online (Sandbox Code Playgroud)
您也可以考虑使用其他距离和色彩空间。例如,与 LAB 颜色空间的 L2 距离更好地反映了人们如何感知颜色相似性。
https://en.wikipedia.org/wiki/CIELAB_color_space#Perceptual_differences
图片取自视频“Big Buck Bunny”:https://peach.blender.org/