gray image to rgb using ind2rgb
Show older comments
please can someone show me how to convert a rgb image to gray and convert that gray image back to rgb using ind2rgb.... how to obtain map for the function ind2rgb..... Please do reply....
Accepted Answer
More Answers (2)
Image Analyst
on 31 Aug 2013
Try this well-commented demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 24;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(1, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Ask user for a number.
defaultValue = 5;
titleBar = 'Enter a value';
userPrompt = 'Enter the number of colors: ';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
integerValue = round(str2double(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end
% Call rgb2ind
[indexedImage, customizedColorMap] = rgb2ind(rgbImage, integerValue);
% Display it
subplot(1, 2, 2);
imshow(indexedImage, []);
colormap(customizedColorMap);
colorbar;
caption = sprintf('Indexed image with %d colors', integerValue)
title(caption, 'FontSize', fontSize);
Naushad Varish
on 4 Jan 2017
Edited: Naushad Varish
on 4 Jan 2017
Please anyone provide the Tutorial or mathematical concept or background how they quantize the images usig three quantization techniques i.e. uniform quantization, minimum variance quantization, and colormap approximations. how we Can apply these quantization approaches to Intensity image or grayscale image alone.
RGB2IND Convert RGB image to indexed image. RGB2IND converts RGB images to indexed images using one of three different methods: uniform quantization, minimum variance quantization, and colormap approximation. RGB2IND dithers the image unless you specify 'nodither' for DITHER_OPTION.
[X,MAP] = RGB2IND(RGB,N) converts the RGB image to an indexed image X
using minimum variance quantization. MAP contains at most N colors. N
must be <= 65536.
X = RGB2IND(RGB,MAP) converts the RGB image to an indexed image X with
colormap MAP by matching colors in RGB with the nearest color in the
colormap MAP. SIZE(MAP,1) must be <= 65536.
[X,MAP] = RGB2IND(RGB,TOL) converts the RGB image to an indexed image X
using uniform quantization. MAP contains at most (FLOOR(1/TOL)+1)^3
colors. TOL must be between 0.0 and 1.0.
[...] = RGB2IND(...,DITHER_OPTION) enables or disables
dithering. DITHER_OPTION is a string that can have one of these values:
'dither' dithers, if necessary, to achieve better color
resolution at the expense of spatial
resolution (default)
'nodither' maps each color in the original image to the
closest color in the new map. No dithering is
performed.
Class Support
-------------
The input image can be of class uint8, uint16, or double. The output
image is of class uint8 if the length of MAP is less than or equal to
256, or uint16 otherwise.
Example
-------
RGB = imread('flowers.tif');
[X,map] = rgb2ind(RGB,128);
imshow(X,map)
See also CMUNIQUE, DITHER, IMAPPROX, IND2RGB, RGB2GRAY.
Categories
Find more on Images 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!