You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to display indexed images from .mat file using colormap?
4 views (last 30 days)
Show older comments
I have a vector matrix 'X' which contains 700 indexed images. I want to display some of images on grid. For displaying purpose I am using a function displayData which is meant for grayscale images. How can I display indexed images ? I am using following displayData function. https://github.com/aptiva0985/MachineLearning-Andrew-Ng-Coursera-/blob/master/ML003/mlclass-ex3/displayData.m
Answers (2)
Image Analyst
on 7 Sep 2018
Try using imshow() and colormap().
What is X? A vector matrix sounds contradictory. Do have a 1-D array of 700 cells, where inside each cell is one indexed image?
By the way, a gray scale image can be considered an indexed image - you just apply a colormap to it just like you do to any indexed image.
19 Comments
jawad ashraf
on 8 Sep 2018
Edited: jawad ashraf
on 8 Sep 2018
I have made 2-D vector X which stores pixel values of indexed images as I want to use them in neural network.I am using 700 training examples each 100 x 100. I have unrolled all pixels of each image in single rows. So X becomes 700 x 1000 where 700 is number of total images and 10000 is number of pixels. so X is a 700 rows and 10000 columns vector.
I want to select some images(rows of X) and display them before feeding into Neural network.
My images are indexed and the function displayData is not meant for indexed images. So I just want to ask if I use the same displayData function where should I call colormap(map)? and where to declare 'map' variable which we give as argument to colormap(map)
Image Analyst
on 8 Sep 2018
Again, you can use imshow() and colormap():
imshow(oneImage); % One image is a 100x100 grayscale (indexed) image.
colormap(hsv(256));
jawad ashraf
on 8 Sep 2018
Edited: jawad ashraf
on 8 Sep 2018
This is not working, Actually I don't want to show just one image, I want to show some 50 images at same time in a grid.
Secondly these images are clothes images. Originally RGB images are converted into indexed images so that RGB triplet matrix can be turned into single composite matrix to insert it into neural network.
The question is how can I display multiple indexed images keeping origional colors(as were in RGB originally) in a grid.
Currently displaydata is showing me grayscale(indexed) images, but I want to bring original look.. Should I make a separate colormap for each image present in vector X ??
Image Analyst
on 8 Sep 2018
You should have used rgb2gray() instead of rgb2ind(). A gray scale image could be useful for a deep learning CNN but an indexed image probably won't be. I would not use indexed images. Have you ever looked at the colorbar of an indexed image? It's basically random, so not what you want for a CNN.
jawad ashraf
on 9 Sep 2018
I am not using CNN, I am using simple NN with one hidden layer.
If I use rgb2gray() then images lose their color information completely, but with rgb2ind() original color of images can be retrieved back ,, my application demands color images because I am making a dress color matching application.
Image Analyst
on 9 Sep 2018
Well then keep the original RGB images. You say "Secondly these images are clothes images. Originally RGB images ..." so just put the original RGB images into the network. I just don't see what is to be gained by taking a full color image and creating an indexed image out of it. Sure it's 3 times less data but there is no way for a network to make sense out of indexes. They're essentially random. One close color could be a totally different index as another close color. That is deceptive to the network.
jawad ashraf
on 9 Sep 2018
Edited: jawad ashraf
on 9 Sep 2018
with true RGB image is becomes very difficult to construct a neural network, it becomes a mess.
Definitely every close color should have a different index and a different color map in the same way as images have different grayscale representation..
According to my research I will have to make a colormap for each image in my dataset and map it to its respective indexed image while display. I mean to say I should make a .mat file which would contain three subfiles 'X' which contains indexed images, second a colormap for each image and Y the target labels i.e clothing.mat= X(700 indexed images) + 700 colormaps + Y(target labels)
I want to confirm from you thst is it right approach??
Walter Roberson
on 9 Sep 2018
"According to my research I will have to make a colormap for each image in my dataset"
That is certainly not what I advised you. https://www.mathworks.com/matlabcentral/answers/414523-how-can-i-use-indexed-image-in-neural-network#answer_332451
"But don't expect anything useful even for pattern matching unless you passed a fixed colormap into rgb2ind(). And even in that case, if you did not do illumination correction, your results might be low quality."
jawad ashraf
on 10 Sep 2018
Edited: jawad ashraf
on 10 Sep 2018
@walter you said to me "you need to generate a colormap that you use for all of the images...". I ask if we have images of different colors how a single colormap can work for all images? Every image has its own color in my dataset (because these are clothes) and I want to bring back their original colors , should not I have a dedicated color map for each image which will bring original appearance? In your approach, when I tried that, original color gets lost and some other color comes, but I want original colors
Guillaume
on 10 Sep 2018
Something that has not been explained clearly and would most likely help us answer the question properly is why you do you want to convert the colour images to indexed. For the neural network to make use of the colour information it would have to convert the indexed images back to RGB so this seems like a waste of time. So, please explain the need for indexed images.
jawad ashraf
on 10 Sep 2018
I am making dress color matching application using simple neural network with one hidden layer. My dataset comprises clothes of different colors. I can't use true RGB color images as it produce triplet matrix(each for red,green and blue). Alternatively, instead of using RGB, I have used Indexed images which give us single composite matrix. Neural network is working well and for testing I have to give it indexed images also. Issue is not pertaining to neural network.
Real issue is that before inputting to NN I want to visualize some examples from my dataset (clothingdata.mat) but when I display my dataset before feeding to NN it appears colorless(like gray).
My simple question is that how can I display multiple images in grid whereas images are
1- indexed
2- multicolored
3- stored in a .mat file
A displaydData function should pick some 50 images from .mat file and diaplay to us with original colors using their colormaps.
single color map for all images dont work because each img having own different color
Guillaume
on 10 Sep 2018
Well, if your displayData function works with RGB images simply convert your indexed images back to RGB with ind2rgb. If not, and also doesn't work with indexed image then you have to use some other function such as imshow as suggested by Image Analyst.
Walter Roberson
on 10 Sep 2018
"I am making dress color matching application using simple neural network with one hidden layer."
There is no way to match colors between two images unless the same value represents the same color in each of the two images. When the color information is inconsistent between the images, the best you can do is to match pattern rather than color.
It is also not clear what you mean by color matching. In your current scheme, if one image had olive green, and another image had forest green, and the two were being distinguished in your rgb2ind(), then the two would be considered to be as different as would be red roses and blue roses. Is that your intention?
jawad ashraf
on 10 Sep 2018
Edited: jawad ashraf
on 10 Sep 2018

I have made one lac combinations of pant & shirt , I have concatinated two 50 x 50 image and made single of 100 x 100.
NN is just a classifier using supervised learning, It classifies data according to our labels, we have made two categories good combination and bad combination. and NN is doing the same....BUT issue is not how NN works and takes inputs. Just forget neural network. Please don't mix it with my real issue.
My issue is that I display dataset before feeding to NN (and doing anything else) it should be colored rather than colorless as shown in figure i.e images are being displayed in grid but colorless.
If you people have implemented Andrew NG's assignments 3 & 4 regarding neural networks you see he saves his dataset into a .mat file (vector matrix) then he displays some of it in a grid and then in next step inserts these to neural network. I am also working on the same lines,, indexed images are being displayed in grid but not with original appearance, how can I display these in original colors.
keep in mind when we convert RGB to indexed images, two things are generated 1- indexes & 2- colormap and we store them separately(indexes in 'I' and colormap in 'map') and when we display we join them like imshow(I,colormap(map)) similarly when I converted my RGB images of clothes using for-loop into indexed it certainly created both things but in .mat file I view only indexes and colormaps of images are lost somewhere. The question is how can I store both things and join them in display function. Hope now you'll understand otherwise I can share my whole scripting files.
Walter Roberson
on 11 Sep 2018
How you handle your neural network is relevant, because it makes the difference between whether you have a single colormap to use to display, or if you have to pull out different colormap for different images.
Image Analyst
on 11 Sep 2018
I'm not buying it. Somehow everyone else in the world can use RGB images successfully, but you say you can't because it becomes a mess or a triplet. I wonder how everyone else could do it.
And the display of the images is independent of the training of the network. Let's say you were able to use imshow() with the indexed image and the colormap to have your entire montage/mosaic look right, then this is just for display. You can't then train your network with indexed images. Simply displaying them one way doesn't automatically somehow automagically make them digestible for your network.
As an example, let's say one shirt was red and green. In one image the index might be 35 for red and 45 for green. However that same shirt with just slightly different things in it (like wrinkles, shadows, etc.) might index 194 for red and 17 for green. So how could you compare those images if you left them as indexes and not convert them to RGB? As far as I know, you can't. Like we're all saying, to compare colors you'll need to convert the image to RGB. Actually I don't even know why you're using a neural network for that when you can just compare colors using quite ordinary traditional image analysis methods.
Guillaume
on 11 Sep 2018
"Just forget neural network. Please don't mix it with my real issue."
I believe I've answered your question.
However, we're not going to forget the NN as we do think that you have an issue there as well (and solving both issues at once would be more productive). If the NN is using colour data, then it will have to convert the images back to a full colour image (in RGB, HSV, LAB or other colour space) to make sense of the colour. With an indexed image, a program cannot know if colour index 28 is anywhere similar to colour index 29, since indices on their own are meaningless. Most likely, if your NN is working with indexed images it's because it doesn't use the colour information but the shapes formed by the colour. In that case, you may as well convert your images to grayscale, it will have the same result.
Also note that by converting to index you are necessarily reducing the number of colours in your image. In matlab, an index image can have at most 65,536 colours whereas an 8-bit RGB image can have over 16 million colours.
Walter Roberson
on 11 Sep 2018
You can use up to uint64 for indexed images. However rgb2ind is limited to 65536 colours.
Walter Roberson
on 11 Sep 2018
It would be completely valid for rgb2ind to return different color indices for any given color for two images that are exactly the same except one is the upside down of the other. There is no inherent ordering of the colors. For the same image in different rotations then the same colors should be found, but the index order is not fixed by the algorithm.
Guillaume
on 10 Sep 2018
Having looked at the code for your displayData, it is not possible to use it with indexed images with different colour maps without a major rewrite. That function builds a single matrix out of all the images so whatever colour map you'd use would always apply to all images.
The simplest way to do what you want would be to use subplot and imshow to let matlab build the grid of images. The one downside with that is that by default matlab leaves a lot of spacing between the subplots. The code to be a 5x10 array of images would be something like:
%inputs:
%clothingimages: a cell array of images
%colourmaps: a cell array of colourmaps
imgindex = 1;
figure;
for row = 1:5
for col = 1:10
subplot(5, 10, imgindex);
imshow(clothingimages{imgindex}, colourmaps{imgindex});
imgindex = imgindex + 1;
end
end
See Also
Categories
Find more on Modify Image Colors 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)