How To Color Objects In An Image With Different Color Each
I'm having following problem: I have black/white images, which I have to colorize: Every white 'blob' in the image represents an instance of an object and I want to color every ob
Solution 1:
Since you are generating a random colormap, it's no surprise the colors are not consistent.
You want to create one colormap with your colors of choice, and pass that colormap to each image, regardless of the number of blobs present. Note however, that by default the colormaps are normalized to the range of your data. Since the range of the data changes depending on the number of blobs found, you need to explicitly set the normalization using vmin=
and vmax
. Here is a demonstration using 4 different images:
import imageio
from scipy import ndimage
colors = ['black','red', 'green', 'yellow', 'pink', 'orange']
vmin = 0
vmax = len(colors)
cmap = matplotlib.colors.ListedColormap(colors)
fig, axs = plt.subplots(4,1, figsize=(3,3*4))
for file,ax inzip(['test1.png','test2.png','test3.png','test4.png'], axs):
im = imageio.imread(file)
blobs, number_of_blobs = ndimage.label(im)
ax.imshow(blobs, cmap=cmap, vmin=vmin, vmax=vmax)
Post a Comment for "How To Color Objects In An Image With Different Color Each"