Как адаптировать изображение относительно оболочки программы? Есть изображения различных размеров и при малом размере изображения, анализ и диаграмма анализа идет отлично, но при большом изображении, диаграммы становится не видно.
from collections import Counter
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import numpy as np
import tkinter as tk
from tkinter import filedialog
import cv2
window.mainloop()
# Менять количество цветов (кластеров) тут
number_of_colors = 3
# Менять id видеопотока тут 0-....
cap = cv2.VideoCapture(0)
fig = plt.figure(figsize=(4, 4))
def get_one_image(img_list):
max_height = 0
total_width = 0 # padding
for img in img_list:
if img.shape[0] > max_height:
max_height = img.shape[0]
total_width += img.shape[1]
final_image = np.zeros((max_height, total_width, 3), dtype=np.uint8)
current_x = 0
for image in img_list:
image = np.vstack((image, np.full((max_height - image.shape[0], image.shape[1], 3), 255)))
final_image[:, current_x:current_x + image.shape[1], :] = image
current_x += image.shape[1]
return final_image
def rgb_to_hex(rgb_color):
hex_color = "#"
for i in rgb_color:
i = int(i)
hex_color += ("{:02x}".format(i))
return hex_color
def prep_image(raw_img):
raw_img = cv2.cvtColor(raw_img, cv2.COLOR_BGR2RGB)
modified_img = cv2.resize(raw_img, (900, 600), interpolation=cv2.INTER_AREA)
modified_img = modified_img.reshape(modified_img.shape[0] * modified_img.shape[1], 3)
return modified_img
def color_analysis(img):
clf = KMeans(n_init=10, n_clusters=number_of_colors)
color_labels = clf.fit_predict(img)
center_colors = clf.cluster_centers_
counts = Counter(color_labels)
ordered_colors = [center_colors[i] for i in counts.keys()]
hex_colors = [rgb_to_hex(ordered_colors[i]) for i in counts.keys()]
plt.pie(counts.values(), labels=hex_colors, colors=hex_colors, autopct='%1.1f%%')
fig.canvas.draw()
res = cv2.cvtColor(np.asarray(fig.canvas.buffer_rgba()), cv2.COLOR_RGBA2BGR)
plt.clf()
comb = get_one_image([image, res])
return comb
root = tk.Tk()
root.withdraw()
while True:
path = filedialog.askopenfilename()
image = cv2.imread(path)
modified_image = prep_image(image)
res_image = color_analysis(modified_image)
cv2.imshow("Result", res_image)
cv2.imwrite('res.jpg', res_image)
cv2.waitKey(0)