├── README.md ├── dominant_colors.py ├── input ├── mountain.jpg └── mountain_2.jpg └── output ├── bar.jpg └── bar_2.jpg /README.md: -------------------------------------------------------------------------------- 1 | # Dominant_colors 2 | Extract dominant colors from the image using OpenCV 3 | -------------------------------------------------------------------------------- /dominant_colors.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | def create_bar(height, width, color): 6 | bar = np.zeros((height, width, 3), np.uint8) 7 | bar[:] = color 8 | red, green, blue = int(color[2]), int(color[1]), int(color[0]) 9 | return bar, (red, green, blue) 10 | 11 | img = cv2.imread('input/mountain.jpg') 12 | height, width, _ = np.shape(img) 13 | # print(height, width) 14 | 15 | data = np.reshape(img, (height * width, 3)) 16 | data = np.float32(data) 17 | 18 | number_clusters = 5 19 | criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) 20 | flags = cv2.KMEANS_RANDOM_CENTERS 21 | compactness, labels, centers = cv2.kmeans(data, number_clusters, None, criteria, 10, flags) 22 | # print(centers) 23 | 24 | font = cv2.FONT_HERSHEY_SIMPLEX 25 | bars = [] 26 | rgb_values = [] 27 | 28 | for index, row in enumerate(centers): 29 | bar, rgb = create_bar(200, 200, row) 30 | bars.append(bar) 31 | rgb_values.append(rgb) 32 | 33 | img_bar = np.hstack(bars) 34 | 35 | for index, row in enumerate(rgb_values): 36 | image = cv2.putText(img_bar, f'{index + 1}. RGB: {row}', (5 + 200 * index, 200 - 10), 37 | font, 0.5, (255, 0, 0), 1, cv2.LINE_AA) 38 | print(f'{index + 1}. RGB{row}') 39 | 40 | cv2.imshow('Image', img) 41 | cv2.imshow('Dominant colors', img_bar) 42 | # cv2.imwrite('output/bar.jpg', img_bar) 43 | 44 | cv2.waitKey(0) -------------------------------------------------------------------------------- /input/mountain.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegiovanni/Dominant_colors/ea83485f94d1316cf7182b524d450a46340c9043/input/mountain.jpg -------------------------------------------------------------------------------- /input/mountain_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegiovanni/Dominant_colors/ea83485f94d1316cf7182b524d450a46340c9043/input/mountain_2.jpg -------------------------------------------------------------------------------- /output/bar.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegiovanni/Dominant_colors/ea83485f94d1316cf7182b524d450a46340c9043/output/bar.jpg -------------------------------------------------------------------------------- /output/bar_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codegiovanni/Dominant_colors/ea83485f94d1316cf7182b524d450a46340c9043/output/bar_2.jpg --------------------------------------------------------------------------------