Histogram Matching Method

Histogram Matching

clear all
clc
I = imread('tire.tif');
Ref = imread ('cameraman.tif'); %'cameraman.tif' and 'pout.tif' and 'concordorthophoto.png'
% colomn = I [1,:] ---> row = 1 and colomn = : --- so, horizontal appearance

% Determining the number of pixels of Original Image
[rowI, colI] =size(I);
c=rowI*colI;
% matrix provided for Original Histogram
h=zeros(1,2^8);
%____________________________________________________________Original Image
% Process of Histogram for Original
for i=1:rowI
    for j=1:colI
        for k=1:length(h);
            if k == I(i,j);
               h(k) = h(k) + 1;
               pdfOr = h/c;
            end
        end
    end
end
cdfOr = zeros(1,2^8);
% Histogram cumulative Distribution Density for Image Original
% 'round' is for integer
for i = 2 : (length(h));
    cdfOr(i) = cdfOr(i-1)+ pdfOr(i-1); 
    cdf256Or = round(cdfOr*256);
end

% Determining the number of pixels of Image Ref.
[rowRef, colRef] =size(Ref);
cRef=rowRef*colRef;
% matrix provided for Histogram Ref
hRef=zeros(1,2^8);
%________________________________________________________________Ref. Image
% Process of Histogram for Ref Image
for i=1:rowRef
    for j=1:colRef
        for L=1:length(hRef);
            if L == Ref(i,j);
               hRef(L) = hRef(L) + 1;
               pdfRef = hRef/cRef;
            end
        end
    end
end
cdfRef = zeros(1,2^8);
% Histogram cumulative Distribution Density for Image Reffe
for i = 2 : (length(hRef));
    cdfRef(i) = cdfRef(i-1)+ pdfRef(i-1); 
    cdf256Ref = round(cdfRef*256);
end
%__________________________________________________________________________
for i=1:256
    tmp{i}=cdf256Ref-cdf256Or(i);
    tmp{i}=abs(tmp{i});         %To obtain the nearest point,the absolute value is taken
    [a index(i)]=min(tmp{i});   %find the nearest point of 2 CDF
end
%____________________________________________________________Image Matching
% For image of Histogram after Matching
Hm=uint8(zeros(size(I,1),size(I,2)));
% New Matrix of Image M
for i=1:rowI
    for j=1:colI     
          for m=1:length(h);
              if m == I(i,j);
                 Hm(i,j) = index(m);
              end
         end
    end
end

% matrix provided for Histogram Mathced
hmi=zeros(1,2^8);
% Process of Histogram from an Mathced Image
for i=1:rowI
    for j=1:colI
        for k=1:length(h);
            if k == Hm(i,j);
               hmi(k) = hmi(k) + 1;
               pdfhmi = hmi/c;
            end
        end
    end
end
cdfhmi = zeros(1,2^8);
% Histogram cumulative Distribution Density for Image M
for i = 2 : (length(h));
    cdfhmi(i) = cdfhmi(i-1)+ pdfhmi(i-1); 
    cdf256hmi = round(cdfhmi*256);
end

subplot(3,3,1), imshow(uint8(I)), title('Original Image (OI)');
subplot(3,3,2), plot(1:256,pdfOr), title('Histogram of OI');
subplot(3,3,3), plot(1:256,cdf256Or), title('CDF of OI');

subplot(3,3,4), imshow(uint8(Ref)), title('Ref. Image (RI)');
subplot(3,3,5), plot(1:256,pdfRef), title('Histogram of RI');
subplot(3,3,6), plot(1:256,cdf256Ref), title('CDF of RI');

subplot(3,3,7), imshow(uint8(Hm)), title('H.Mathced Image (HMI)');
subplot(3,3,8), plot(1:256,pdfhmi), title('Histogram of HMI');
subplot(3,3,9), plot(1:256,cdf256hmi), title('CDF of HMI')

The result is below:

1

Advertisements