I have a function that draws point clouds using OpenCV. There could be 10k~10m
points in the image, and I need to align the pixels to the actual length, so I have to use OpenCV. The image size would be around (10k ~ 50k
x 10k ~ 50k
).
However, it's difficult to remember the meaning of the colors after a while, so I would like to add some legends (for discrete colors) or colorbars (for continuous colors).
Are there any existing tools or packages for this purpose? If not, could you help me with a utility function to draw legends and/or colorbars on an existing image?
An OpenCV - drawn image is typically a NumPy array. We can assume a test case:
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)
Please don't be limited by this example. Any ideas would be greatly appreciated.
I have a function that draws point clouds using OpenCV. There could be 10k~10m
points in the image, and I need to align the pixels to the actual length, so I have to use OpenCV. The image size would be around (10k ~ 50k
x 10k ~ 50k
).
However, it's difficult to remember the meaning of the colors after a while, so I would like to add some legends (for discrete colors) or colorbars (for continuous colors).
Are there any existing tools or packages for this purpose? If not, could you help me with a utility function to draw legends and/or colorbars on an existing image?
An OpenCV - drawn image is typically a NumPy array. We can assume a test case:
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)
Please don't be limited by this example. Any ideas would be greatly appreciated.
Share Improve this question asked Mar 19 at 14:47 LeoLeo 731 silver badge6 bronze badges 1- did you try using opencv's drawing functions? I would also recommend that you ask some LLM to generate that code for you. pretty much all of them are free of charge, even Github Copilot. – Christoph Rackwitz Commented Mar 19 at 16:42
1 Answer
Reset to default 1Maybe something like that:
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
Example usage:
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)