|
@@ -3,7 +3,7 @@ import numpy as np |
|
|
import math |
|
|
import math |
|
|
import itertools as it |
|
|
import itertools as it |
|
|
|
|
|
|
|
|
DENSITY = 5 |
|
|
|
|
|
|
|
|
DENSITY = 4 |
|
|
RED = [0, 0, 255] |
|
|
RED = [0, 0, 255] |
|
|
N_COLORS = 3 |
|
|
N_COLORS = 3 |
|
|
|
|
|
|
|
@@ -13,16 +13,17 @@ def draw_rectangle(frame, sp, ep): |
|
|
return cv2.rectangle(frame, sp, ep, RED) |
|
|
return cv2.rectangle(frame, sp, ep, RED) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def analyze_block(frame, sp, ep): |
|
|
|
|
|
|
|
|
def analyze_block(frame, index, sp, ep): |
|
|
""" Analyze a block """ |
|
|
""" 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 |
|
|
return frame |
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -33,11 +34,11 @@ def analyze(frame): |
|
|
m = math.ceil(n * (height / width)) |
|
|
m = math.ceil(n * (height / width)) |
|
|
dx = width / n |
|
|
dx = width / n |
|
|
dy = height / m |
|
|
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)) |
|
|
sp = (int(x * dx), int(y * dy)) |
|
|
ep = (int(x * dx + dx), int(y * dy + dy)) |
|
|
ep = (int(x * dx + dx), int(y * dy + dy)) |
|
|
frame = draw_rectangle(frame, sp, ep) |
|
|
frame = draw_rectangle(frame, sp, ep) |
|
|
frame = analyze_block(frame, sp, ep) |
|
|
|
|
|
|
|
|
frame = analyze_block(frame, index, sp, ep) |
|
|
return frame |
|
|
return frame |
|
|
|
|
|
|
|
|
|
|
|
|
|
|