Help with Quantization of a sampled image
Show older comments
%for quantization
q_img = floor(s_img ./ 5);
imshow(q_img);
title('Quantized Image');
1 Comment
Danikko Tiu
on 25 Feb 2018
Answers (1)
Image Analyst
on 25 Feb 2018
Edited: Image Analyst
on 25 Feb 2018
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
Danikko Tiu
on 25 Feb 2018
Image Analyst
on 25 Feb 2018
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
on 25 Feb 2018
Edited: Danikko Tiu
on 25 Feb 2018
Image Analyst
on 25 Feb 2018
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.
Categories
Find more on Computer Vision with Simulink 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!