imquantize() - Image Processing
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)examplequant_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 an array index such
that:quant_A = values(index)Code Generation support:
Yes.MATLAB Function Block support:
Yes.
Syntax
quant_A = imquantize(A,levels) examplequant_A = imquantize(___,values) example[quant_A,index]
= imquantize(___) example
Example
Segment Image into Three Levels Using Two ThresholdsOpen This Example
Read image and display it.I = imread('circlesBrightDark.png');
imshow(I)
axis off
title('Original Image')
Calculate two threshold levels.thresh = multithresh(I,2);
Segment the image into three levels using imquantize .seg_I = imquantize(I,thresh);
Convert segmented image into color image using label2rgb and display it.RGB = label2rgb(seg_I);
figure;
imshow(RGB)
axis off
title('RGB Segmented Image')
Compare Thresholding Entire Image Versus Plane-by-Plane ThresholdingOpen This Example
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);
Generate thresholds for each plane of the RGB image.threshForPlanes = zeros(3,7);
for i = 1:3
threshForPlanes(i,:) = multithresh(I(:,:,i),7);
end
Process the entire image with the set of threshold values computed from entire image.value = [0 threshRGB(2:end) 255];
quantRGB = imquantize(I, threshRGB, value);
Process each RGB plane separately using the threshold vector computed from the given plane. Quantize each RGB plane using threshold vector generated for that plane.quantPlane = zeros( size(I) );
for i = 1:3
value = [0 threshForPlanes(i,2:end) 255];
quantPlane(:,:,i) = imquantize(I(:,:,i),threshForPlanes(i,:),value);
end
quantPlane = uint8(quantPlane);
Display both posterized images and note the visual differences in the two thresholding schemes.imshowpair(quantRGB,quantPlane,'montage')
axis off
title('Full RGB Image Quantization Plane-by-Plane Quantization')
To compare the results, calculate 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.dim = size( quantRGB );
quantRGBmx3 = reshape(quantRGB, prod(dim(1:2)), 3);
quantPlanemx3 = reshape(quantPlane, prod(dim(1:2)), 3);
colorsRGB = unique(quantRGBmx3, 'rows' );
colorsPlane = unique(quantPlanemx3, 'rows' );
disp(['Unique colors in RGB image : ' int2str(length(colorsRGB))]);
disp(['Unique colors in Plane-by-Plane image : ' int2str(length(colorsPlane))]);
Unique colors in RGB image : 188
Unique colors in Plane-by-Plane image : 231
Threshold grayscale image from 256 to 8 levelsOpen This Example
Reduce the number of discrete levels in an image from 256 to 8. This example uses two different methods 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')
Split the image into eight levels by obtaining seven thresholds from multithresh.thresh = multithresh(I,7);
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 the valuesMin vector such that the minimum value in each quantization interval is assigned to the eight levels of the output image. Instead of calling imquantize again with the vector valuesMin , use the output argument index to assign those values to the output image.valuesMin = [min(I(:)) thresh]
quant8_I_min = valuesMin(index);
valuesMin =
23 65 88 119 149 169 189 215
Display both eight-level output images side by side.imshowpair(quant8_I_min,quant8_I_max,'montage')
title('Minimum Interval Value Maximum Interval Value')
Output / Return Value
Limitations
Alternatives / See Also
Reference