When using ind2rgb, how do you use map as output argument?
9 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Steven Lord
on 1 Jul 2022
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
rgb = ind2rgb(im, map)
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 Comments
Steven Lord
on 1 Jul 2022
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
More Answers (1)
Cris LaPierre
on 1 Jul 2022
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
cmap(X(1,1),:) % corresponding color
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)
figure
imshow(X,map)
6 Comments
Image Analyst
on 1 Jul 2022
@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.
See Also
Categories
Find more on Orange in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!