,但是,一段时间后很难记住颜色的含义,因此我想添加一些传奇(用于离散的颜色)或配色栏(用于连续颜色)。
为此目的有现有的工具或软件包吗?如果没有,您能否帮助我使用实用程序功能,以在现有图像上绘制传奇和/或配色栏?
ANOPENCV-绘制图像通常是一个numpy阵列。我们可以假设一个测试案例:
def add_legends(image, legend_color_list, legend_label):
pass
def add_colorbar(image, cmap_func):
pass
image1=np.random.randint(0,5,(10000,20000,3))
image1_with_legend=add_legends(image1, legend_color_list, legend_label)
image2=np.random.randint(0,255,(10000,20000))
image2_colored=np.vectorize(cmap_func)(image2)[:,:,:3]
image2_with_colorbar=add_colorbar(image2_colored,cmap_func)
请不要受到此示例的限制。任何想法都将不胜感激。
可能是这样的:
import cv2
import numpy as np
def add_legends(image, legend_color_list, legend_labels, position=(50, 50), box_size=40, spacing=10, text_color=(255, 255, 255)):
img = image.copy()
x, y = position
for color, label in zip(legend_color_list, legend_labels):
# Draw color box
cv2.rectangle(img, (x, y), (x + box_size, y + box_size), color, -1)
# Put text next to the box
cv2.putText(img, label, (x + box_size + 10, y + box_size - 10),
cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 2, cv2.LINE_AA)
# Move to the next entry
y += box_size + spacing
return img
legend_colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0), (255, 255, 0)] # Red, Green, Blue, Yellow
legend_labels = ["Class 1", "Class 2", "Class 3", "Class 4"]
image1 = np.random.randint(0, 255, (1000, 2000, 3), dtype=np.uint8)
image_with_legends = add_legends(image1, legend_colors, legend_labels)
cv2.imwrite("image_with_legends.png", image_with_legends)