![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/256778/0000%20Screenshot.png)
Retain rgb image after processing
5 views (last 30 days)
Show older comments
MatlabEnthusiast
on 22 Dec 2019
Commented: Image Analyst
on 22 Dec 2019
Hi all. how are you?
How can I retain a colored image from a processed image.
I know how to extract RGB color components(channels as they are referred to in most posts i have read so far). I also know i can get the color map from an original image after indexing it using the
`rgb2ind`
function. This would work for me but the problem is the end result after my processing is an MxN double matrix. so using
`ind2rgb`
just gave me a totally black image.
How can i apply the originally extracted colors or colormap to this double matrix to retain something similar to the original image.
I searched around but couldnt come to an answer.
Thank you very much.
0 Comments
Accepted Answer
Image Analyst
on 22 Dec 2019
I don't understand what you want. Generally you don't do image processing on an indexed image gotten from using rgb2ind(). The image is basically gibberish as far as understanding and seeing the photo, as you can see from the upper right image in my demo below. I'm not sure what you want but maybe this code snippet will help you understand. Note that the final/bottom image is an indexed image (NOT RGB image) that is being displayed with the same colormap that the function rgb2ind() decided upon. rgb2ind() has some algorithm to choose colors based on what it thinks will give a color image that is most like the original. Of course it will have fewer colors and have a somewhat "posterized" appearance. Note from the colorbar on the bottom that each of the 16 gray levels in the indexed image will be assigned the corresponding color from the colorbar. So for example, a value of 10 in the indexed image will show up as yellow, and values of 1 will appear reddish, and values of 14 will appear pink.
% Read in original RGB image.
rgbImage = imread('peppers.png');
h1 = subplot(2, 2, 1);
imshow(rgbImage);
caption = sprintf('Original RGB Image\n(Of course there is no colormap for RGB Images)');
title(caption, 'FontSize', 16);
% Turn into an indexed image:
numColors = 16; % Set to whatever you want.
[indexedImage, cmap] = rgb2ind(rgbImage, numColors);
% Display it without a colormap.
h2 = subplot(2, 2, 2);
noMap = gray(numColors);
imshow(indexedImage, [], 'Colormap', noMap);
impixelinfo;
colorbar(h2);
caption = sprintf('Indexed Image\nwith No Colormap Applied Yet');
title(caption, 'FontSize', 16);
% Display it without a colormap.
h3 = subplot(2, 2, 3:4);
imshow(indexedImage, [], 'Colormap', cmap);
colorbar(h3);
caption = sprintf('Indexed Image\nwith %d-Color Colormap from rgb2ind() Applied', numColors);
title(caption, 'FontSize', 16);
![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/256778/0000%20Screenshot.png)
3 Comments
Image Analyst
on 22 Dec 2019
Sure, you don't need to call imshow() -- the variable will still exist as an indexed image, and the colormap will still exist as cmap.
More Answers (0)
See Also
Categories
Find more on Blue in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!