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

    %% Map Colors Using Histogram Matching with Varying Number of Bins
% This example shows how to map colors using histogram matching,
% highlighting the impact when you vary the number of bins in the
% histograms. |imhistmatch| transforms the input image such that the
% histogram of the output image is a match to the histogram of the
% reference image. 
%% 
% Read the image to be transformed and the reference image into the
% workspace. These images were taken with a digital camera and represent
% two different exposures of the same scene. The image to be transformed is
% dark when compared with the reference image.

% Copyright 2015 The MathWorks, Inc.

A = imread('office_2.jpg');   
ref = imread('office_4.jpg');
%%
% View the images with histograms of each plane of the images. Note how the
% darker image, |A|, has a preponderance of its pixels in the lower bins.
% The reference image, |ref|, is a properly exposed image that fully
% populates all of the available bins in all three RGB channels.
figure, subplot(4,2,1), imshow(A), title('A: Dark Image')
subplot(4,2,2), imshow(ref), title('ref: Reference Image')
subplot(4,2,3), imhist(A(:,:,1),256), title('RGB Histograms: 256 bins')
subplot(4,2,4), imhist(ref(:,:,1),256), title('RGB Histograms: 256 bins')
subplot(4,2,5), imhist(A(:,:,2),256)
subplot(4,2,6), imhist(ref(:,:,2),256)
subplot(4,2,7), imhist(A(:,:,3),256)
subplot(4,2,8), imhist(ref(:,:,3),256)
%%
% Generate the output image |B| using three different values for |N|, the
% number of bins. |N| represents the upper limit of the number of discrete
% data levels present in image |B|.
B64  = imhistmatch(A, ref,  64);   
B128 = imhistmatch(A, ref, 128);
B256 = imhistmatch(A, ref, 256);
%%
% View the output images along with the histograms of each plane of these
% images. Note that as |N| increases, the number of levels in each RGB
% channel of output image |B| also increases.
figure, subplot(4,3,1), imshow(B64), title('Output Image B64')
subplot(4,3,2), imshow(B128), title('Output Image B128')
subplot(4,3,3), imshow(B256), title('Output Image B256')
subplot(4,3,4), imhist(B64(:,:,1),64), title('RGB Histograms')
subplot(4,3,5), imhist(B128(:,:,1),128), title('RGB Histograms')
subplot(4,3,6), imhist(B256(:,:,1),256), title('RGB Histograms')
subplot(4,3,7), imhist(B64(:,:,2),64)
subplot(4,3,8), imhist(B128(:,:,2),128)
subplot(4,3,9), imhist(B256(:,:,2),256)
subplot(4,3,10), imhist(B64(:,:,3),64)
subplot(4,3,11), imhist(B128(:,:,3),128)
subplot(4,3,12), imhist(B256(:,:,3),256)