When using ind2rgb, how do you use map as output argument?

I am confused on what the 'map' is and how to create it. When I use the ind2rgb fucntion, I get an error becuase it says I did not use 'map.' I already have searched up the documentation for this and I am still confused on how to use it.

 Réponse acceptée

The map input to ind2rgb is the colormap for the image. The first input to ind2rgb represents indices into that colormap. So for example if I had a colormap:
map = [1 0 0; 0 1 0; 0 0 1]; % Color 1 is red, 2 is green, 3 is blue
Then my image will be values 1, 2, and 3 indicating whether this part of the image is red, green, or blue respectively.
rng default
im = randi(3, [4 4]) % Random image
im = 4×4
3 2 3 3 3 1 3 2 1 1 1 3 3 2 3 1
rgb = ind2rgb(im, map)
rgb =
rgb(:,:,1) = 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 1 rgb(:,:,2) = 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 rgb(:,:,3) = 1 0 1 1 1 0 1 0 0 0 0 1 1 0 1 0
All the 1's in im are translated to a value of 1 in the first page of rgb and 0's in the second and third pages. Similarly the 2's in im set the value of the corresponding elements of the second page of rgb to 1 and leave the first and third pages as 0. You can see if you look at im that the first row is blue, green, blue, and blue which you can see if you visualize the image.
image(rgb)

7 commentaires

Thank you very much! Can you explain why you did
rng default
to be a part of the code?
I did that so if you ran the code you'd get the same random im matrix that I did when I ran the code. That's how I knew that no matter how many times I ran the code, "the first row is blue, green, blue, and blue".
@Image Analyst called out that map is a lookup table. An analogy I thought of as I went down to lunch was McDonald's. You could walk up to the register at a McDonald's restaurant and ask for "Combo #1 and two Combos #2." Those numbers, Combo #1 and Combo #2, are the indices in your indexed image im. In order to "translate" from the combo numbers to the actual food you ordered (around here, those would probably be a Big Mac and two Quarter Pounders with Cheese) you need a list associating combo numbers to the items those combos contain. That's the colormap.
Here in Natick Massachusetts the mapping map is Combo #1 <-> Big Mac and Combo #2 <-> Quarter Pounder with Cheese. But not all McDonald's restaurants would necessarily have the same colormap. In Japan, for example, the mapping might be Combo #1 <-> Bai Big Mac Set and Combo #2 <-> Bai Teriyaki McBurger Set. That difference in colormap could result in a very different bag of food for the order "Combo #1 and two Combos #2."
Steven, thank you for the awesome ananology. I have another question. How does
map = [1 0 0; 0 1 0; 0 0 1]
reflect the colors of red, green, and blue?
Alexandar
Alexandar le 1 Juil 2022
Modifié(e) : Alexandar le 1 Juil 2022
For further clafication of my question, how is the code you wrote for map associated with the colors of RGB to 1, 2, and 3 @Steven Lord? Is it a common rule to know that [1 0 0] is read, [0 1 0] is green, and [0 0 1] is blue? Thank you for your help in advance!
Each row represents an RGB triplet. The first, [1 0 0] is essentially 100% red, 0% green, 0% blue. The second is 0% red, 100% green, 0% blue.
If you have vectors where two or all three components are present, that's a mix of colors. Red plus blue gives you magenta. I've made the line wider so it's easier to see.
plot(1:5, 1:5, 'Color', [1, 0, 1], 'LineWidth', 6, 'DisplayName', 'magenta')
If you have mostly green with some blue, you get cyan.
hold on % so all the lines show up in the same axes
plot(1:5, 10:-1:6, 'Color', [0, 1, 1], 'LineWidth', 6, 'DisplayName', 'cyan')
Red + green is yellow.
plot(6:10, 5:-1:1, 'Color', [1, 1, 0], 'LineWidth', 6, 'DisplayName', 'yellow')
If you specified say 0.5 for each, that's a form of gray.
plot(6:10, 6:10, 'Color', [0.5, 0.5, 0.5], 'LineWidth', 6, 'DisplayName', 'gray')
And there are a few predefined colors that let you specify them more simply in calls to plot. I'm just going to display this as one marker in the center, which is why I'm using the marker related properties in the plot call.
plot(5.5, 5.5, 'Marker', 'o', 'MarkerSize', 20, ...
'MarkerFaceColor', 'k', ... % 'k' is black, RGB [0 0 0]
'DisplayName', 'black')
legend show % Show the legend
Wow, I can't tell you how helpful this is and how much new coding techniques I learned. You're the absolute best @Steven Lord!

Connectez-vous pour commenter.

Plus de réponses (1)

It is the colormap to apply to your indexed image. See here. The indeces in the indexed image indicate which row of the colormap to use color that pixel. For a uint8 image, that means your colormap must have 256 rows.
[X,cmap] = imread('corn.tif');
imshow(X,cmap)
X(1,1) % index of pixel 1
ans = uint8 105
cmap(X(1,1),:) % corresponding color
ans = 1×3
0.2510 0.2392 0.2902
The easiest way to create it is to use the built in colormaps, though you can create your own in you want.
map = jet(256)
map = 256×3
0 0 0.5156 0 0 0.5312 0 0 0.5469 0 0 0.5625 0 0 0.5781 0 0 0.5938 0 0 0.6094 0 0 0.6250 0 0 0.6406 0 0 0.6562
figure
imshow(X,map)

6 commentaires

Thank you very much for your answer! Can you explain to me why the
map = jet(256)
creates the blue image that is part of your answer? Also, why for one pixel do we get three number values (0.2510 0.2392 0.2902)? Does this pertain to the mixture of colors being used to create the color in the image? Thank you in adance for your help!
You might also find the section I linked to in my first post helpful.
A color is defined by three components, which are basically fractions of the max/brightest possible color, for [red, green, blue] respectively, with the values ranging from 0 to 1. A colormap is basically a look-up table where the row is the gray level of the indexed or grayscale image. So if you go to row 17, the r, g, and b values in row 17 of the colormap will tell it what color a gray level of 17 should become when displayed.
Jet has a lot of blue in the lower rows, and it happens that the indexed image of corn happened to have a lot of low values so a lot of them get mapped to blue. In the original colormap that it was stored with most of the pixels would have been mapped to yellow to red colors instead of blue because that is the correct colormap, while jet is not.
You can see the original indexed image, NOT pseudocolored by the colormap, and the colormap it was stored with like this:
[indexedImage, cmap] = imread('corn.tif');
% Show the indexed image.
h1 = subplot(3, 1, 1);
imshow(indexedImage, 'Colormap', gray(256));
colorbar(h1);
title(h1, 'Indexed Image and its index values')
% Show a histogram of the indexed values.
h2 = subplot(3, 1, 2);
histogram(indexedImage);
title(h2, 'Histogram of index values in the indexed image');
grid on;
% Now show the indexed image with it's applied colormap
h3 = subplot(3, 1, 3);
imshow(indexedImage);
colormap(h3, cmap);
colorbar(h3);
title(h3, 'Indexed image with colormap applied');
Note that the upper part of the colormap is all black. That means any values greater than 128 (of which there are no pixels as you can see from the histogram) would be shown as black. Also note that the colormap is essentially a lookup table and so there is no need or requirement that colors next to each other in the lookup table be similar, so you will usually not see some kind of smooth gradient in colors within the colorbar. It will usually look fairly random like in this example. The key thing though is that if you use that lookup table to map image values into a color, it will give a normal looking image, if the colormap is correct like it would be is you had created it with the rgb2ind function.
@Image Analyst thank you very much for your explanation!
@Cris LaPierre, when you say you can create your own color maps, can you keep the same color map but change certain pixels to be brighter? Is that possible?
@Alexandar you can change certain indexed image values to be brighter. It will affect all pixels in the image with that value, not just certain pixels. For example this will make the turbo map but make it so that image pixels with the value of 0 show up as blue, 255 show up as red, and 128 show up as green.
cmap = turbo(256);
cmap(1,:) = [0,0,1] % Gray level 0 shows up as blue.
cmap(129,:) = [0,0,1] % Gray level 128 shows up as green.
cmap(256,:) = [1,0,0] % Gray level 255 shows up as red.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Images dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by