Система прогнозирования цвета с OpenCv PythonPython

Программы на Python
Ответить
Anonymous
 Система прогнозирования цвета с OpenCv Python

Сообщение Anonymous »


Изображение
Привет, я разработчик начального этапа, пытающийся сделать систему прогнозирования цвета, но я обнаружил какую-то ошибку, которую не могу понять. Я думаю, вы могли бы мне помочь с этим.... Я поделюсь с вами своим правильным кодом.
Это ошибка: IndexError: индекс 1 выходит за пределы оси 0 с размером 1
Ошибка отображается в функции RGB2HEX(color):, но я не могу ее решить.
Вот код:
def RGB2HEX(color):
return "#{:02x}{:02x}{:02x}".format(int(color[0]), int(color[1]), int(color[2]))

def get_image(image_path):
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image

IMAGE_DIRECTORY = 'C:/Users/Dell/Desktop/CPS 02'
COLORS = {
'GREEN': [0, 128, 0],
'BLUE': [0, 0, 128],
'YELLOW': [255, 255, 0]
}
images = []

for file in os.listdir(IMAGE_DIRECTORY):
if not file.startswith('.'):
images.append(get_image(os.path.join(IMAGE_DIRECTORY, file)))

# extracting colors from image
def get_colors(images, number_of_colors, show_char = True):
for j in range(len(images)):
modified_image = cv2.resize(images[j], (600, 400), interpolation = cv2.INTER_AREA)
modified_image = modified_image.reshape(modified_image.shape[0]*modified_image.shape[1],1)

clf = KMeans(n_clusters = number_of_colors)
labels = clf.fit_predict(modified_image)

counts = Counter(labels)

center_colors = clf.cluster_centers_
# We get ordered colors by iterating through the keys
ordered_colors = [center_colors for i in counts.keys()]
hex_colors = [RGB2HEX(ordered_colors) for i in counts.keys()]
rgb_colors = [ordered_colors for i in counts.keys()]

# matching an image by its color
def match_image_by_color(image, color, threshold = 60, number_of_colors = 10):

image_colors = get_colors(image, number_of_colors, False)
selected_color = rgb2lab(np.uint8(np.asarray([[color]])))

select_image = False
for i in range(number_of_colors):
curr_color = rgb2lab(np.uint8(np.asarray([[image_colors]])))
diff = deltaE_cie76(selected_color, curr_color)
if (diff < threshold):
select_image = True

return select_image

# Selecting an image
def show_selected_images(images, color, threshold, colors_to_match):
index = 1

for i in range(len(images)):
selected = match_image_by_color(images, color, threshold, colors_to_match)
if (selected):
plt.subplot(1, 5, index)
plt.imshow(images)
index += 1

# printing the result
plt.figure(figsize = (20, 10))
show_selected_images(images, COLORS['BLUE'], 60, 5)


Подробнее здесь: https://stackoverflow.com/questions/678 ... ncv-python
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»