From 7abc3ba7e99b517a134a462da50761123516acdb Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Sun, 2 May 2021 17:54:08 -0700 Subject: [PATCH] Can get colors and HSV --- radio.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/radio.py b/radio.py index 0abf838..33e243e 100644 --- a/radio.py +++ b/radio.py @@ -3,7 +3,7 @@ import numpy as np import math import itertools as it -DENSITY = 5 +DENSITY = 4 RED = [0, 0, 255] N_COLORS = 3 @@ -13,16 +13,17 @@ def draw_rectangle(frame, sp, ep): return cv2.rectangle(frame, sp, ep, RED) -def analyze_block(frame, sp, ep): +def analyze_block(frame, index, sp, ep): """ Analyze a block """ - block = np.float32(frame[sp[1]:ep[1], sp[0]:ep[0], 0:3]) - block = block.reshape((-1, 3)) - criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, .2) - compactness, labels, palette = cv2.kmeans(block, N_COLORS, None, criteria, - 2, cv2.KMEANS_RANDOM_CENTERS) - unique, counts = np.unique(labels.flatten(), return_counts=True) - for index, count in zip(unique, counts): - print(index, count, palette[index]) + block = frame[sp[1]:ep[1], sp[0]:ep[0], 0:3] + average_color = np.average(block, (0, 1)) + sp2 = tuple(int(x) for x in (2 * np.array(sp) + np.array(ep)) / 3) + ep2 = tuple(int(x) for x in (np.array(sp) + 2 * np.array(ep)) / 3) + average_color = [int(x) for x in average_color] + frame = cv2.rectangle(frame, sp2, ep2, average_color, 5) + average_color = np.uint8([[average_color]]) + h, s, v = cv2.cvtColor(average_color, cv2.COLOR_BGR2HSV)[0][0] + print(index, h, s, v) return frame @@ -33,11 +34,11 @@ def analyze(frame): m = math.ceil(n * (height / width)) dx = width / n dy = height / m - for x, y in it.product(range(n), range(m)): + for index, (x, y) in enumerate(it.product(range(n), range(m))): sp = (int(x * dx), int(y * dy)) ep = (int(x * dx + dx), int(y * dy + dy)) frame = draw_rectangle(frame, sp, ep) - frame = analyze_block(frame, sp, ep) + frame = analyze_block(frame, index, sp, ep) return frame