图像直方图匹配

直方图匹配又叫直方图规定化(Histogram Normalization/Matching)是指对一副图像进行变换,使其直方图与另一幅图像的直方图或特定函数形式的直方图进行匹配。应用场景如不同光照条件下的两幅图像。

function Iadjusted = histogram_matching(I, type)
% Histogram match the image I against a theoretically natural histogram
% which is believed to mimic real pencil drawings
%
% I must be a grayscale image within 0 and 255

% Prepare the histogram of image 'I', which is 'ho'
if ~isa(I,'uint8')
    I = im2uint8(I);
end

po = imhist(I)/ numel(I);
ho = cumsum(po);


% Prepare the 'natural' histogram which is 'histo'
p1 = @(x) 1 / 9 * exp(-(256-x)/9) * heaviside(256-x);
p2 = @(x) 1 / (256 - 105) * (heaviside(x-105) - heaviside(x-256));
% p2 = @(x) 1 / (225 - 105) * (heaviside(x-105) - heaviside(x-225));
p3 = @(x) 1 / sqrt(2*pi*11)*exp(-((x-90)^2)/(2*121));

if strcmp(type,'colour')
    %p = @(x) (52*p1(x) + 37*p2(x) + 11*p3(x));
    p = @(x) (62*p1(x) + 30*p2(x) + 5*p3(x));
else
    %p = @(x) (62*p1(x) + 30*p2(x) + 5*p3(x));
    p = @(x) (76*p1(x) + 22*p2(x) + 2*p3(x));
end

prob = zeros(1, 256); histo = zeros(1, 256);

for i=1:256
    prob(i) = p(i);
end
prob = prob/ sum(prob);
histo = cumsum(prob);


% Do the histogram matching
Iadjusted = zeros(size(I,1), size(I,2));
for y=1:size(I,1)
    for x=1:size(I,2)
        histogram_value = ho(I(y,x)+1);
        [v,i] = min(abs(histo - histogram_value));
        Iadjusted(y,x) = i;
    end
end
Iadjusted = Iadjusted / 255;

另外,matlab自身提供了histeq(I,hgram)函数可以实现直方图规定化.

你可能感兴趣的:(图像直方图匹配)