I'd like to create an image that displays instance seg annotations on top of the RGB. I…
Private research annotation
Sign in to add a private research label or note.
- User
I'd like to create an image that displays instance seg annotations on top of the RGB. I have the image, and to represent the instance segmentation buffer, I have "instance_ids" -> this is a Matrix of shape
(H x W x 1), whereHis the height andWis the width of corresponding camera image. The third axis contains the instance ID for each pixel asint.To color the instance segmentation, I'd like to also create a colormap that maps from the instance ID to a unique color.
Create a python function that takes in an image (represented as numpy RGB) and "instance_ids", then outputs an image (represented as numpy RGB) that combines the instance segmentation and original image, representing each instance ID with a unique colormapped value.
- Assistant
Sure, here's a python function that should do what you're looking for:
pythonimport numpy as np import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap def visualize_instance_segmentation(image, instance_ids): # create a colormap with a unique color for each instance ID num_instances = len(np.unique(instance_ids)) colors = plt.get_cmap('tab20')(np.linspace(0, 1, num_instances)) cmap = ListedColormap(colors) # apply the colormap to the instance IDs to get a color image instance_colors = cmap(instance_ids.squeeze()) # blend the color image with the original image using alpha blending alpha = 0.5 blended = (1 - alpha) * image + alpha * instance_colors # clip values outside the valid range of [0, 1] blended = np.clip(blended, 0, 1) return blended - User
I got this error ValueError: operands could not be broadcast together with shapes (1080,1920,3) (1080,1920,4)
- Assistant
- User
Also, I see that you blended the instance seg image and original image. I don't want that. I want the instance seg image to overwrite the rgb image wherever there's an instance ID
- Assistant
Sure, in that case, you can simply replace the original RGB values with the instance segmentation colors wherever the instance IDs are non-zero.
Here's an updated version of the
visualize_instance_segmentationfunction that should do what you're looking for:python