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

    %% Compare Thresholding Entire Image Versus Plane-by-Plane Thresholding
% 
%% 
% Read truecolor (RGB) image and display it.

% Copyright 2015 The MathWorks, Inc.

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))]);