Help with Quantization of a sampled image

%for quantization
q_img = floor(s_img ./ 5);
imshow(q_img);
title('Quantized Image');

1 Comment

The accepted answer to the question in the link above is where i got the
  • q_img = floor(s_img ./ 5);
line of code from. I was wondering if anyone can explain to me what exactly is happening? I have a general idea of how a quantized image should look and how it works, but I am having a hard time fully understanding it

Sign in to comment.

Answers (1)

Do you mean use imquantize, like this:
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 = 15;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'cameraman.tif'; % Assign the one on the button that they clicked on.
% Get the full filename, with path prepended.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);
%===============================================================================
% Read in a demo image.
grayImage = imread(fullFileName);
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
caption = sprintf('Original Pseudocolor Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Column', 'FontSize', fontSize, 'Interpreter', 'None');
ylabel('Row', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Get the dimensions of the image. numberOfColorChannels should be = 3.
[rows, columns, numberOfColorChannels] = size(grayImage);
% Display the image.
subplot(2, 2, 2);
imshow(grayImage, []);
axis on;
caption = sprintf('Original Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
% Display the thermal image.
subplot(2, 2, 2);
imhist(grayImage);
axis on;
grid on;
title('Histogram of Original image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Quantize the image into 5 levels.
quantizedImage = imquantize(grayImage, linspace(0, 255, 6));
fprintf('Min = %.1f, max = %.1f\n', min(quantizedImage(:)), max(quantizedImage(:)));
% Find out what gray levels are in the image.
unique(quantizedImage)
% Now it's in the range 1-5. Convert it to the range 0-255
quantizedImage = uint8(255 * mat2gray(quantizedImage));
fprintf('Min = %.1f, max = %.1f\n', min(quantizedImage(:)), max(quantizedImage(:)));
% Find out what gray levels are in the image.
unique(quantizedImage)
% Display the quantized image.
subplot(2, 2, 3);
imshow(quantizedImage, []);
axis on;
caption = sprintf('Quantized Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Display the histogram of quantized image.
subplot(2, 2, 4);
imhist(uint8(quantizedImage));
axis on;
grid on;
title('Histogram of Quantized Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

4 Comments

Sorry forgot to mention that I cannot use the built-in matlab function
If you cannot use a built-in function, then you are probably doing it for a homework assignment, though you did not tag it as homework. So I'd imagine another rule is that you are not allowed to have someone else write the code for you, right? So . . . good luck.
Danikko Tiu
Danikko Tiu on 25 Feb 2018
Edited: Danikko Tiu on 25 Feb 2018
sorry first time posting will edit my question so that its not doesnt seem like i am just asking for code how do i tag as hw?
In the "Tags" section when you post initially, or in the Tags section on the upper right of this page, you can enter your tags. I put homework there for you.
By the way, floor() is a built in function.
Also dividing by 5 reduces the original range of the data, but I guess it was not specified if that was okay or not.

Sign in to comment.

Asked:

on 25 Feb 2018

Commented:

on 25 Feb 2018

Community Treasure Hunt

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

Start Hunting!