[英]Most dominant color in RGB image - OpenCV / NumPy / Python
[英]get most dominant colors from video - openCV PYTHON
我试图从视频中获取最主要的 colors,当开始播放时想要从视频中实时绘制 colors,例如 6 最主要的,3 等,我搜索了很多,但所有教程都只检测到三个 colors,红色,蓝色和绿色,有人可能会检测到更多,因为他们自己设置值,使用 hsv map 到他们可以设置哪个 Z62848E3CE5804AA985513A7922FF87B2 知道,我的视频是我的问题范围
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);
现在只需打开网络摄像头并显示直方图
您可以尝试使用 kmeans 聚类来获得优势 colors:
.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)
您也可以考虑使用其他距离和色彩空间。 例如,与 LAB 颜色空间的 L2 距离更好地反映了人如何感知颜色相似性。
https://en.wikipedia.org/wiki/CIELAB_color_space#Perceptual_differences
图片取自视频“Big Buck Bunny”: https://peach.blender.org/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.