how to uniformly quantize an intensity image into K levels (where K=4, 16, etc)? and how to display these quantized images? i am able to generate the gray scale image, but please let me know how to quantize it uniformly.

11 views (last 30 days)
i am trying to do uniform quantization on a gray scale image. i have tried imquantize() but it did not help. i have generated an intensity image I from RGB image. now i have to quantize it in K levels . such that if K=2, then pixel whose values are below 128 will turn to 0 and whose values are above 128 will turn to 255. i have to generate different images for different K and display all images. please let me know how to do that in MATLAB.

Accepted Answer

Image Analyst
Image Analyst on 8 Feb 2013
I see no reason why imquantize() won't work. Please show your code that demonstrates why imquantize() fails.
Alternatively you can use mat2gray followed by multiplication, as this demo shows:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 15;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins', 'Pout');
if button == 1
baseFileName = 'cameraman.tif';
elseif button == 2
baseFileName = 'moon.tif';
elseif button == 3
baseFileName = 'eight.tif';
elseif button == 4
baseFileName = 'coins.png';
else
baseFileName = 'pout.tif';
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = baseFileName;
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- 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 in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Quantize
numberOfGrayLevels = 4;
quantizedImage = uint8(mat2gray(grayImage) * (numberOfGrayLevels-1));
% Display the original gray scale image.
subplot(2, 2, 3);
imshow(quantizedImage, []);
caption = sprintf('Image quantized into %d gray levels', numberOfGrayLevels);
title(caption, 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(quantizedImage);
subplot(2, 2, 4);
bar(pixelCount);
grid on;
caption = sprintf('Histogram of image quantized into %d gray levels', numberOfGrayLevels);
title(caption, 'FontSize', fontSize);
lastGL = find(pixelCount>0, 1, 'last');
xlim([0 lastGL+1]); % Scale x axis manually.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!