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 object with a new color, but for every image i want to use the same color scheme:
For example:
- 1st image: 3 Objects -> used colors: red, green, yellow
- 2nd image: 2 Objects -> used colors: red, green
- 3rd image: 5 objects -> used colors: red, green, yellow, pink, orange
I've colored a couple of images by hand to show what the result should look like:
Black / white mask that has to be colorized
2 objects, 2 colors: green, yellow
4 objects, 4 colors: green, yellow, red, light grey
To do it automatically i've tried the approach here:
import scipy
from scipy import ndimage
import matplotlib.pyplot as plt
import matplotlib
from random import random
colors = [(1,1,1)] + [(random(),random(),random()) for i in xrange(255)]
new_map = matplotlib.colors.LinearSegmentedColormap.from_list('new_map', colors, N=256)
im = scipy.misc.imread('blobs.jpg',flatten=1)
blobs, number_of_blobs = ndimage.label(im)
plt.imshow(blobs, cmap=new_map)
plt.imsave('jj2.png',blobs, cmap=new_map)
plt.show()
Problem with that is, that if I run in on my images, the objects get colorized differently depending on how many objects there are in each image:
For example:
1st image: 3 Objects -> used colors: red, green, yellow
2nd image: 2 Objects -> used colors: orange, yellow
3rd image: 5 objects -> used colors: red, orange, green, limegreen, yellow
4th image: 3 objects -> used colors: red, green, yellow
Here are some pictures to visualize the incorrect coloring of the 3rd image:
2 objects, coloured orange and pink
Another image with 2 objects, coloured orange and pink
Image with 3 objects, now colors change: orange, yellow and green (what I need: orange, pink and new color