www.gusucode.com > images 案例代码 matlab源码程序 > images/ThresholdGrayscaleImageFrom256To8LevelsExample.m

    %% Threshold grayscale image from 256 to 8 levels
% 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.

% Copyright 2015 The MathWorks, Inc.

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);
%%
% 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);
%%
% Display both eight-level output images side by side.
imshowpair(quant8_I_min,quant8_I_max,'montage') 
title('Minimum Interval Value           Maximum Interval Value')