Difference from proposed duplicate
Matlab choose random color for plotting asks how to create a colormap with randomly selected colors. I am asking how to choose visually distinct colors for a colormap. Randomly selected colors are not guaranteed to be visually distinctive. In fact, I use random colors as a counter example in my question.
If my question is going to be a duplicate, it should be a duplicate of Automatically plot different colored lines because at least one of the answers, answers my question about visually distinct colors even though the question does not ask for that detail. But none of the answers of Matlab choose random color for plotting are helpful, so don't use that one!
Original Question
The lines
colormap alternates 7 colors that are visually distinct in a predefined order. flag
and prism
do this also. The colors repeat after 7 distinct colors for lines
, 6 for prism
and 4 for flag
.
These colormaps are very useful for distinguishing between labeled segments in an image because labels often have consecutive values which have low visual distinctness in the jet
or parula
colorspaces.
For example, using the first image from the NYUv2 dataset, you can see that dishwasher and counter have almost the same color using colormap('parula')
It gets better for dishwasher and counter using colormap('lines')
, but worse for chair and trashcan, because the value of trashcan is 12 and chair is 5. 12 mod 7 = 5, so they get the same color assignment
I have n labels, so I would like to define a colormap that has n alternating visually distinct colors. Then, I can avoid the problem of two labels sharing the same modulus.
I know how to create a custom colormap, but the challenge is making the colors visually distinct. One thing I tried is randomly sampling colors from jet (similar to the solutions suggested to Matlab choose random color for plotting).
c_jet = colormap('jet');
idx = randperm(size(c_jet, 1));
c_new = c_jet(idx(1:30),:);
colormap(c_new);
But the colors are not visually distinct enough. The ceiling and the wall are practically identical.
It may be the case that 30 distinct colors is simply too many to ask for, but I'd like a general approach that gets as close as possible.