May-31-2020, 08:56 AM
(May-30-2020, 11:25 PM)scidam Wrote: Ok.Here's a very simplified bit of code. You will need a folder with some images (no more than 5 will work):find_closest
expects thatrgb
has shape (m, n) andcolor
has shape (n,). In our case n = 3. The only way here is to investigate whyrgb
has shape (xxxx, ), i.e. why it is one dimensional array (it should be 2D array). Could you please provide minimal reproducible example here that leads to the problem?
import numpy as np import cv2 import glob rgb, links = [], [] def find_closest(color, rgb): return rgb[np.argmin(np.abs(rgb - color).sum(axis=1))] def get_average(image, path): avg_col_per_row = np.average(image, axis=0) avg = np.average(avg_col_per_row, axis=0) rgb.append(avg.astype(int).tolist()) links.append(path) for l in glob.glob("your_image_path"): get_average(cv2.imread(l, cv2.IMREAD_UNCHANGED), str(l)) some_colour = [100,123,14] print(find_closest(some_colour, np.array(rgb)))In theory, this should make rgb a 2D array since it's an array, full of arrays with 3 integers inside.