Timothy Wong
Timothy Wong

Reputation: 709

How to display/manipulate an indexed image Octave

Situation: Trying to output and also manipulate an indexed image.

Problem: When using ind2rgb(image, colormap(x)), we received an error saying that the indexed image contains colors outside of colormap, where x is the colormap option.

Question: How to work around this?

Additional information: I used imshow(image, colormap(x)), where I used every colormap available on Octave in substitution of x.

Upvotes: 0

Views: 275

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

I believe this happens because you have more indexes than colors in colormap. Just make a bigger colormap. You can do that with the sysntax colormap(x(size_of_cmap))

ind2rgb(image, colormap(viridis(max(image(:))))

Alternatively, create the colormap and interpolate it. Caution: this can create artifacts in the color due to HSVs circular behavior.

m=max(image(:));
cm=colormap(x);

hsv=rgb2hsv(cm);
cm=interp1(linspace(0,1,size(cm,1)),hsv,linspace(0,1,m));
cm=hsv2rgb(cm);

ind2rgb(image,cm);

I suggest using the first solution if possible.

Upvotes: 1

Related Questions