
Some indices in indexed images made with cmunique are 0, what does that really mean? Isn't is a bug? I cannot index a colormap with index=0. Documentation is contradictory.
8 次查看(过去 30 天)
显示 更早的评论
I transform RGB images to indexed images with cmunique. Some of the indices in the indexed image are 0. It is not mentioned in the help for cmunique, but there is a contradictory statement in the help for rgb2ind, bold in the abbreviated citation below:
"[X,map] = rgb2ind(RGB,n) : Indexed image, returned as an m-by-n matrix X of nonnegative integers. If the length of map is less than or equal to 256, then the output image is of class uint8. Otherwise, the output image is of class uint16. n must be less than or equal to 65,536.
The value 0 in the output array X corresponds to the first color in the colormap."
In my case, the length of smallest color map is of the order of ~1500, so uint16 indices are expected. What do I do with elements of X that are 0 (zero), is there a way to prevent this happening?
Here is my relevant code:
% code snippet
Afull=imread('myfigure.png'); % myfigure.png is a color image
rectf=[133 56 521 521];
[fig_xrefout,fig_yrefout,Imfig,rectfig] = imcrop(Afull,rectf);
[Imfig_indexed,fig_map] =cmunique(Imfig);
dum=find(Imfig_indexed<=0);
~isempty(dum) % is 1!!!
1 个评论
Stephen23
2024-9-11
编辑:Stephen23
2024-9-11
It is worth noting that the standard for indexed images is that 0 refers to the first color in the colormap. MATLAB must comply with this otherwise e.g. a standard 256 color indexed image would not be possible to import without messing up the data type or losing colors. Lets try with this internet-sourced 256 color indexed image:

fnm = 'Adaptative_8bits_palette_sample_image.png';
MATLAB tells us that this image contains a colormap with 256 colors, and that the (integer) image data uses 8 bits:
imfinfo(fnm)
Lets get the image (i.e. index) and colormap:
[A,map] = imread(fnm)
class(A)
min(A(:))
So the lowest index/value in that image is 0. But does this actually correspond to a color? It must otherwise there would be no way for a uint8 index to distinguish all 256 colors from that colormap. But lets check it anyway: we make the first row some easily distinguishable color and make the first few pixels 0:
A(1:20,1:20) = 0;
map(1,:) = [0,1,1]; % cyan
imshow(A,map)
Which means that the MATLAB documentation here:
is wrong: "...such that the value 1 points to the first row in map, the value 2 points to the second row, and so on".
回答(1 个)
Steven Lord
2024-9-11
0 is a nonnegative integer value.
isZeroNegative = 0 < 0 % false
isZeroInteger = 0 == round(0) % true
If you're using Imfig_indexed to directly index into fig_map then you would need to add 1 because 0 is not a valid index into an array in MATLAB.
If you're using this data in the context of displaying the image in MATLAB using image or the like, then by the default way color data values are mapped into the rows of the colormap (controlled by the CDataMapping property of the image object) this is correct. From that documentation page for the default value of the CDataMapping property, 'direct':
- If the values are of type double or single, then values of 1 or less map to the first color in the colormap. Values equal to or greater than the length of the colormap map to the last color in the colormap.
- If the values are of type uint8, uint16, uint32, uint64 , int8, int16, int32, or int64, then values of 0 or less map to the first color in the colormap. Values equal to or greater than the length of the colormap map to the last color in the colormap (or up to the range limits of the type).
- If the values are of type logical, then values of 0 map to the first color in the colormap and values of 1 map to the second color in the colormap.
[Emphasis added.]
3 个评论
Walter Roberson
2024-9-12
X — Indexed image
m-by-n matrix of integers
Indexed image, specified as an m-by-n matrix of integers.
- If you specify X as an array of integer data type, then the value 0 corresponds to the first color in the colormap map. For a colormap containing c colors, values of image X are clipped to the range [0, c-1].
- If you specify X as an array of data type single or double, then the value 1 corresponds to the first color in the colormap. For a colormap containing c colors, values of image X are clipped to the range [1, c].
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Red 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
