Documentation Center

  • Trial Software
  • Product Updates

imquantize

Quantize image using specified quantization levels and output values

Syntax

  • quant_A = imquantize(A,levels) example
  • quant_A = imquantize(___,values) example
  • [quant_A,index] = imquantize(___) example

Description

example

quant_A = imquantize(A,levels) quantizes image A using specified quantization values contained in the N element vector levels. Output image quant_A is the same size as A and contains N + 1 discrete integer values in the range 1 to N + 1 which are determined by the following criteria:

  • If A(k) ≤ levels(1), then quant_A(k) = 1.

  • If levels(m-1) < A(k)  ≤  levels(m) , then quant_A(k) = m.

  • If A(k) > levels(N), then quant_A(k) = N + 1.

Note that imquantize assigns values to the two implicitly defined end intervals:

  • A(k)levels(1)

  • A(k) > levels(N)

example

quant_A = imquantize(___,values) adds the N + 1 element vector values where N = length(levels). Each of the N + 1 elements of values specify the quantization value for one of the N + 1 discrete pixel values in quant_A.

  • If A(k) ≤ levels(1), then quant_A(k) = values(1).

  • If levels(m-1) < A(k)  ≤  levels(m) , then quant_A(k) = values(m).

  • If A(k) > levels(N), then quant_A(k) = values(N + 1).

example

[quant_A,index] = imquantize(___) returns matrix index such that:

quant_A = values(index)

Examples

expand all

Image Threshold

Compute multiple thresholds for an image and apply those thresholds to the image to get segment labels.

I = imread('circlesBrightDark.png');  

Quantize the image into three discrete levels using two thresholds.

thresh = multithresh(I,2);

seg_I = imquantize(I,thresh);		% apply the thresholds to obtain segmented image

RGB = label2rgb(seg_I); 	 % convert to color image

figure;
imshowpair(I,RGB,'montage');  % display images side-by-side
axis off;
title('Original Image                               RGB Segmented Image')

Compare Thresholding an Entire Image and Plane-by-plane Thresholding

Quantize truecolor RGB image to 8 levels and compare results between thresholding the entire RGB image versus plane-by-plane thresholding.

Read truecolor RGB image and display it.

I = imread('peppers.png');
imshow(I); axis off;
title('RGB Image');

Generate thresholds for seven levels from the entire RGB image.

threshRGB = multithresh(I,7)		% 7 thresholds from entire RGB image
threshRGB =

   24   51   79  109  141  177  221

Repeat this process for each plane in the RGB image.

threshForPlanes = zeros(3,7);			% initialize
% Compute thresholds for each R, G and B plane
for i = 1:3
    threshForPlanes(i,:) = multithresh(I(:,:,i),7);
end
threshForPlanes
threshForPlanes =

   40   69   92  125  159  195  231
   27   49   74  100  128  164  209
   18   38   60   86  127  165  222

Process entire image with the set of threshold values computed from entire image. Add black (0) and white (255) to value vector which assigns values to output image.

value = [0 threshRGB(2:end) 255]; 
% Quantize entire image using one threshold vector
quantRGB = imquantize(I, threshRGB, value);

Process each RGB plane separately using threshold vector computed from the given plane.

quantPlane = zeros( size(I) );
% Quantize each RGB plane using threshold vector generated for that plane
for i = 1:3
    value = [0 threshForPlanes(i,2:end) 255]   % output value to assign
    quantPlane(:,:,i) = imquantize(I(:,:,i),threshForPlanes(i,:),value);
end

quantPlane = uint8(quantPlane);	% convert from double to uint8

Display both posterized images and note the visual differences in the two thresholding schemes.

imshowpair(quantRGB,quantPlane,'montage'); 
set(gcf,'Color',[1.0 1.0 1.0]); % set background color of figure
title('Full RGB Image Quantization        Plane-by-Plane Quantization')

The following code snippet computes the number of unique RGB pixel vectors in each output image. Note that the plane-by-plane thresholding scheme yields about 23% more colors than the full RGB image scheme.

% convert images to mx3 matrices
dim = size( quantRGB );
quantRGBmx3   = reshape(quantRGB,   prod(dim(1:2)), 3);
quantPlanemx3 = reshape(quantPlane, prod(dim(1:2)), 3);

% extract only unique 3 element RGB pixel vectors from each matrix
colorsRGB   = unique( quantRGBmx3,   'rows' );
colorsPlane = unique( quantPlanemx3, 'rows' );

disp(['Number of unique colors in RGB image            : ' int2str(length(colorsRGB))]);
disp(['Number of unique colors in Plane-by-Plane image : ' int2str(length(colorsPlane))]);
Number of unique colors in RGB image            : 188
Number of unique colors in Plane-by-Plane image : 231

Threshold a Grayscale Image from 256 to 8 levels

This example reduces the number of discrete levels in an image from 256 to 8. It then demonstrates two different schemes for assigning values to each of the eight output levels.

Read image and display it.

I = imread('coins.png');
imshow(I); axis off;
title('Grayscale Image');

Obtain seven thresholds from multithresh to split the image into eight levels.

thresh = multithresh(I,7);  % 7 thresholds result in 8 image levels

Construct the valuesMax vector such that the maximum value in each quantization interval is assigned to the eight levels of the output image.

valuesMax = [thresh max(I(:))]
[quant8_I_max,index] = imquantize(I,thresh,valuesMax);
valuesMax =

   65   88  119  149  169  189  215  255

Similarly, construct valuesMin such that the minimum value in each quantization interval is assigned to the eight levels of the output image

valuesMin = [min(I(:)) thresh]
valuesMin =

   23   65   88  119  149  169  189  215

Instead of calling imquantize again with the vector valuesMin, use the output argument index to assign those values to the output image.

quant8_I_min = valuesMin(index);

Display both eight-level output images side by side.

imshowpair(quant8_I_min,quant8_I_max,'montage')
set(gcf,'Color',[1.0 1.0 1.0]) % set background color of figure
title('Minimum Interval Value           Maximum Interval Value')

Input Arguments

expand all

A — Input imageimage

Input image, specified as a numeric array of any dimension.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

levels — Quantization levelsvector

Quantization levels, specified as an N element vector. Values of the discrete quantization levels must be in monotonically increasing order.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

values — Quantization valuesvector

Quantization values, specified as an N + 1 element vector.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

expand all

quant_A — Quantized output image image

Quantized output image, returned as a numeric array the same size as A. If input argument values is specified, then quant_A is the same data type as values. If values is not specified, then quant_A is of class double.

index — Mapping matrixmatrix

Mapping matrix, returned as a matrix the same size as input image A. It contains integer indices which access values to construct the output image: quant_A = values(index). If input argument values is not defined, then index = quant_A.

Data Types: double

See Also

| |

Was this topic helpful?