Is there an alternative to rgb2ind that produces a linear, monotonic colormap?
Show older comments
The function rgb2ind outputs indices for each pixel and a corresponding colormap. The colormap is nonlinear and nonmonotonic. Thus because adjacent pixels have random relationships with one another the indexed matrix cannot be manipulated (e.g., filtered), as stated in the note here:
Is there a way to force color indexing to be ordered so that I could do some kind of spatial filtering operation on the indexed matrix?
Ultimately what I want is take an nxnx3 RGB matrix, for instance X:
X = imread('peppers.png');
and collapse it down to nxn without discarding the original color information. Indexing the colors seems like a good solution, but only if the resultant indexed colormap is well ordered.
I have looked at the solutions presented here:
and while this article does provide a means of processing indexed color images, the original color information is irreversibly thrown out and replaced by another well-ordered colormap such as grayscale.
Answers (3)
Jan
on 25 Apr 2013
I do not get the problem. Why don't you use:
[X, M] = rgb2ind(RGB)
and sort the color map M like you want to afterwards? Then renumbering X is trivial also.
4 Comments
Erin
on 25 Apr 2013
Jan
on 25 Apr 2013
What does sorting mean in the 3D color space? Why should the colormap be "smooth", when the corresponding pixels are not existing in the picture?
Walter Roberson
on 25 Apr 2013
Edited: Walter Roberson
on 25 Apr 2013
But smoothness should not be necessary to preserve the relationship whereby "nearby" colors (in color space) are represented by "nearby" numbers (on the number line) in the index matrix, X.
Nothing can preserve that relationship if you are mapping an infinite space to fewer dimensions. It is some theorem or other: distances will increase without bound.
In a finite space (0-255 in each of 3 panes gives you 16777216 possibilities, but that is still finite), it can be done, but only at the cost of defining "nearby on the number line" to be on the order of N^3/2 positions away for a cube of length N on each side.
Walter Roberson
on 25 Apr 2013
0 votes
Color representation for humans requires at least 3 attributes, such as R,G,B or H,S,v or Y,Cr,Cb . Even if it only required 2 attributes, the problem of sorting it would be like the problem of sorting complex numbers: you cannot do it without having a "jump" between values that are "nearby" in the original space.
Image Analyst
on 25 Apr 2013
0 votes
You don't filter the indexed image. You don't even view it with a colormap that is wasn't built with. For example if you view it in gray scale (using the gray(256) colormap), or look at the array in the variable editor, it will look like garbage. It doesn't make sense to do anything with that indexed image at all EXCEPT to view it with the colormap that was created at the same time as it.
If you want a smooth image to filter or whatever, then use rgb2gray() instead.
5 Comments
Erin
on 25 Apr 2013
Image Analyst
on 25 Apr 2013
Well then I don't understand. What do you want to do? What image do you want to filter? The color one or the gray scale one? What does "collapse it down to nxn" mean? You mean imresize()? Or do you mean you want to quantize the colors, like discussed in http://en.wikipedia.org/wiki/Color_quantization. Or do you want to do it via
[indexedImage, cmap] = rgb2ind(colorImage);
quantizedRGBImage = ind2rgb(indexedImage, cmap);
?????
Erin
on 25 Apr 2013
Image Analyst
on 25 Apr 2013
You can't have a single matrix that represents color, like Walter was telling you. You can have a true color 3D RGB image with separate planes for each color band, or you can have an indexed image where you have one integer image (not even really a grayscale image) with an associated color map (the pair of which make up an indexed image), or you can have the hue information, saturation information, and value (lightness) information in a 3D HSV image. Or you can have three arrays in other color spaces (CIE LAB, HSL, HSI, YCbCr, etc.). But you can't have color in just a single 2D numerical matrix. I'm not that familiar with wavelets, so you might be best off posting a new question with a subject line and Products list that really targets those people who deal with wavelets (I think Wayne, Greg, etc.). A subject something like "Want wavelet decomposition of color image" and list Wavelet Toolbox in the Products list. Also give motivation for WHY you want that so they can tell you if doing that is the best way to achieve what you want to achieve - perhaps there is another way, maybe even one that doesn't involve wavelets at all, who knows?
Erin
on 25 Apr 2013
Categories
Find more on White in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!