Histogram equalization and matching

Histogram equalization and matching

1. Histogram equalization

1. Algorithm description

The purpose of histogram equalization is to convert a picture with more concentrated gray levels and a small range into a picture with more scattered gray levels and a wide range to enhance the image contrast. Image contrast is greatest when the image histogram is completely uniform.

The transformation function f(x) of histogram equalization needs to meet the conditions:

1. f(x) increases monotonically on 0<=x<=L−1, where L represents the gray level

2. The range of f(x) is 0 to L-1

There is an important function that can satisfy the above conditions: y=f(x)=(L−1)∫px(t)dt

According to the above formula, the main algorithm ideas I use here are as follows:

1. First count the number of pixels on the gray level from 0 to 255 in the pixels used

2. Then find the probability density function

3. Then use the probability density function to find the cumulative distribution function

4. Convert the cumulative distribution function range to a mapped pixel table from 0 to 255

5. Then make each pixel of the original image find the mapped pixel value according to the original pixel value lookup table.

In this way, we have established a histogram balanced mapping function.

2. Matlab code

Function Equalization:

Equalization.m

function eql = Equalization(img)
    [height,width] = size(img);

    %Pixel counter
    Pixel_Num = zeros(1,256);
    for i = 1:height
        for j = 1: width
            Pixel_Num(img(i,j) + 1) = Pixel_Num(img(i,j) + 1) + 1;
        end
    end

    %Pixel PDF
    Pixel_PDF = Pixel_Num / (height * width);

    %Pixel CDF
    Pixel_CDF = zeros(1,256);
    Pixel_CDF(1) = Pixel_PDF(1);
    for i = 2:256
        Pixel_CDF(i) = Pixel_CDF(i - 1) + Pixel_PDF(i);
    end

    %255 * Pixel_CDF 
    Pixel_CDF = 255 * Pixel_CDF;

    %gray levels map
    for i = 1:height
        for j = 1: width
            img(i,j) = Pixel_CDF(img(i,j));
        end
    end
    eql = img;

2. Histogram matching

1. Algorithm description

Histogram matching, also known as histogram specification, is to transform the histogram of the original image into a certain form of histogram, so that the two images have similar hue and contrast. For some special cases, the uniform histogram obtained by histogram equalization cannot achieve the effect. In such cases, it is often necessary to specify the specific distribution of the output image histogram, which requires the use of histogram matching.

To match the histograms of image A and image C, the general algorithm idea is to convert image A into equalized image B, and also convert image C into equalized image B, and then use the inverse function from image B to image C, Let image A be transformed into an image after matching the histogram along the route of A->B->C, which has the same hue as image C.

The algorithm idea I used here is as follows:

1. First perform histogram equalization on the image A to be processed to obtain the cumulative distribution function CDF_From

2. Then make the histogram equalization of the image B that needs to be matched to obtain the cumulative distribution function CDF_To

3. Establish a mapping table, for each gray level, obtain the position with the smallest distance from a point on CDF_From to any point in CDF_To, and then write the mapping table

4. Let each pixel of the original image find the new pixel value after mapping according to the original pixel value lookup table.

In this way, we have established a mapping function for histogram matching.

The above histogram equalization will still be used here, but since the return value is not what I need, and I found a more concise way of writing the cumulative distribution function on the Internet, I will not call my Equalization function.

2. Matlab code

Function Matching:

Matching.m

function match = Matching(Img_From, Img_To)
    Hist_From = imhist(Img_From); 
    Hist_To = imhist(Img_To);
    CDF_From = cumsum(Hist_From) / numel(Img_From);
    CDF_To  = cumsum(Hist_To) / numel(Img_To);

    index = zeros(1,256);
    for i = 1 : 256
        [~,index(i)] = min(abs(CDF_From(i) - CDF_To));
    end

    [height,width] = size(Img_From);
    for i = 1 : height
        for j = 1 : width
            Img_From(i, j) = index(Img_From(i, j) + 1) - 1;
        end
    end
    match = uint8(Img_From);

Third, the image and its histogram

1. Script main.m

This is a script that calls the above Equalization function and Matching function, which can be run directly on matlab, similar to the main function in C language. After running, you will get the two images in the result below.

%Histogram Equalization
img = imread('river.jpg');
set(figure, 'name', 'Equalization');
subplot(2,2,1);
imshow(histeq(img));%histeq Image
title('Histeq Image');
subplot(2,2,2);
imhist(histeq(img));%histeq histogrm
subplot(2,2,3);
equal = Equalization(img);
imshow(equal);%Equalized Image
title('Equalized Image');
subplot(2,2,4);
imhist(equal);%Equalized Histogram

%Histogram Matching
Img_From = imread('EightAM.png');
Img_To = imread('LENA.png');
set(figure, 'name', 'Matching');
subplot(3,2,1);
imshow(Img_From);%Image To Be Matched
title('Raw Image');
subplot(3,2,2);
imhist(Img_From);%Histogrm To Be Matched
title('Raw Histogrm');
subplot(3,2,3);
imshow(Img_To);%Dest Image
title('Dest Image');
subplot(3,2,4);
imhist(Img_To);%Dest Histogram
title('Dest Histogrm');
subplot(3,2,5);
match = Matching(Img_From, Img_To);
imshow(match);%Matched Image
title('Matched Image');
subplot(3,2,6);
imhist(match);%Matched Histogram
title('Matched Histogrm');

2. Processing results

Comparison of histogram equalization renderings (comparison of using the matlab function histeq and using the Equalization function written by yourself):

write picture description here

Comparison of histogram matching renderings (before and after using the Matching function written by yourself and the comparison of the matching LENA.png image and histogram):

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325643200&siteId=291194637