How to display indexed images from .mat file using colormap?
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
0 votes
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
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
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
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
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
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
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
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
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!