How to fix the error
There are two possible causes for this error:
- The file name is spelled incorrectly.
- The image file is not in the current working directory.
, , ( ) ( : IDE ).
, " ", , . , , "". , , . , .
- . . , , , .
. :
import cv2
import numpy as np
from skimage import io
img = io.imread('/img/8290756f2f65c46a0d638f95cf573a2e.png')[:, :, :-1]
, , , @Ruan B.:
average = img.mean(axis=0).mean(axis=0)
k- ( n_colors 5).
pixels = np.float32(img.reshape(-1, 3))
n_colors = 5
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .1)
flags = cv2.KMEANS_RANDOM_CENTERS
_, labels, palette = cv2.kmeans(pixels, n_colors, None, criteria, 10, flags)
_, counts = np.unique(labels, return_counts=True)
, , - , :
dominant = palette[np.argmax(counts)]
, :

, , , , k-, , :
In [30]: average
Out[30]: array([91.63179156, 69.30190754, 58.11971896])
In [31]: dominant
Out[31]: array([179.3999 , 27.341282, 2.294441], dtype=float32)
, , . . , . . , ( ). , , , Lego.

:
import matplotlib.pyplot as plt
avg_patch = np.ones(shape=img.shape, dtype=np.uint8)*np.uint8(average)
indices = np.argsort(counts)[::-1]
freqs = np.cumsum(np.hstack([[0], counts[indices]/counts.sum()]))
rows = np.int_(img.shape[0]*freqs)
dom_patch = np.zeros(shape=img.shape, dtype=np.uint8)
for i in range(len(rows) - 1):
dom_patch[rows[i]:rows[i + 1], :, :] += np.uint8(palette[indices[i]])
fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(12,6))
ax0.imshow(avg_patch)
ax0.set_title('Average color')
ax0.axis('off')
ax1.imshow(dom_patch)
ax1.set_title('Dominant colors')
ax1.axis('off')
plt.show(fig)
TL; DR
, , - @Ruan B. - , . ().