フィルターのクリア

Why is colormap not displaying a value of 1 as color #1?

5 ビュー (過去 30 日間)
Sam Brian
Sam Brian 2023 年 11 月 18 日
コメント済み: Walter Roberson 2023 年 11 月 22 日
Having trouble with colormap and I have no clue why. I set an 8x8 matrix with every element either 0 or 1. I run it through colormap and 1 gets assigned to color#2 . What am I missing here? Code below.
GBColorMap = [0.0, 0.0, 1.0 % color# 0 is blue
0.5, 0.5, 0.0 % color# 1 is brown-ish
1.0, 0.0, 0.0 ] ; % color# 2 is red
size = 8 ;
boats = 10 ;
counter = 0 ;
GB = zeros(size) ;
while counter < boats
col = randi(size) ;
row = randi(size) ;
if GB(row,col) == 0
GB(col, row) = 1 ;
counter = counter + 1 ;
end
end
GB
GB = 8×8
0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0
figure(1)
colormap(GBColorMap);
imagesc(GB)
  2 件のコメント
Voss
Voss 2023 年 11 月 18 日
"1 gets assigned to color#2"
That's right: 0 maps to blue and 1 maps to red.
"Why is colormap not displaying anything with a value of 1?"
I see red in the output.
GBColorMap = [0.0, 0.0, 1.0 % color# 0 is blue
0.5, 0.5, 0.0 % color# 1 is brown-ish
1.0, 0.0, 0.0 ] ; % color# 2 is red
size = 8 ;
boats = 10 ;
counter = 0 ;
GB = zeros(size) ;
while counter < boats
col = randi(size) ;
row = randi(size) ;
if GB(row,col) == 0
GB(col, row) = 1 ;
counter = counter + 1 ;
end
end
GB
GB = 8×8
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
figure(1)
colormap(GBColorMap);
imagesc(GB)
colorbar
Sam Brian
Sam Brian 2023 年 11 月 18 日
I guess I should re-phrase. why is a value of 1 being displayed as red and not brown-ish here?

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2023 年 11 月 18 日
your gb only has two values, 0 and 1. imagesc() sets the color axes bounds to min() and max() of the array, so [0 1] in this case. Coloring is then according to fraction of the way through that range. But your entries are all 0 (one end of the range) or 1 (the other end of the range). You have nothing in the range 1/3 to 2/3 that might get mapped to the central color.
  4 件のコメント
Sam Brian
Sam Brian 2023 年 11 月 22 日
編集済み: Sam Brian 2023 年 11 月 22 日
super helpful guys, thank you so much! Still a bit confused as to why a uint8 formatted matrix gets bounded differently than a short formatted matrix of the same values tbh. But at least I can get my program to work! also really appreciate the bit about imagesc() being for scaling. My proff said to think of it as image to screen. Image at scale is a much more helpful way of thinking about it. Thanks!
Walter Roberson
Walter Roberson 2023 年 11 月 22 日
Color data mapping method, specified as 'direct' or 'scaled'. Use this property to control the mapping of color data values in CData into the colormap. CData must be a vector or a matrix defining indexed colors. This property has no effect if CData is a 3-D array defining true colors.
The methods have these effects:
  • 'direct' — Interpret the values as indices into the current colormap. Values with a decimal portion are fixed to the nearest lower integer.
  • 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.
  • 'scaled' — Scale the values to range between the minimum and maximum color limits. The CLim property of the axes contains the color limits.
So part of the answer is just "That's how it is documented."
As to why it is that way:
When you are using single() precision then there are 24 bits of precision available, and single() can directly represent all positive numbers between 1 and 16777216 (after that single() precision stops been able to directly represent all integers.) 16777216 different colors in a color map is more than plenty, so there is no need to use any space-saving measures. Instead, when you use single() then the values are just directly used as indices, with the standard 1-based indexing that MATLAB uses.
The situation with double() is similar, but with an upper limit of 9007199254740992 entries on the colormap -- which is larger than the amount of memory that you can install on any publicly-known x64 architecture machine.
But when you are using uint8(), which is the most common representation for images, then only the values 0 to 255 are available. If you were to say that 0 is not a valid value in such a case, you would be limited to colormaps of length 255; if instead you detect that you are working with uint8 and you allow 0 as the first index then you can get colormaps of length 256. colormaps of length 256 are common.
So for the integer data classes, the algorithm is effectively take double() of the data value, and add 1, to get the index into the colormap (using standard 1-based indexing.) But for single() and double() the algorithm is to use the value directly as an index without adding 1.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by