Summary image enhancement algorithm (histogram equalization, Laplace, Log transformation, gamma gamma conversion) is attached MATLAB code

First, image enhancement algorithms introduced

Image enhancement algorithm common in the brightness, contrast, saturation, hue adjustment, etc., to increase their clarity, reduce noise and the like. After combining a plurality of image enhancement algorithms often, the above function, for example to dry the image is equivalent to a low pass filter, a high pass filter to increase sharpness was, of course, enhanced image is a final image acquiring useful information service oriented. The process may be a general algorithm: image to dry, increase the sharpness (contrast), or acquired grayscale image edge feature or convolving the image, binarization and the like, often above four steps can be achieved in different steps subsequent experiments will be the topic for this aspect, citing its application scenarios and handling characteristics.
This article is part of a comprehensive blog articles Garden, integrate, there will be subsequent amendments, there is guidance please advise in the comments area, very grateful.

1.1 based on image histogram equalization enhancement

Image contrast enhancement method can be divided into two categories: direct contrast enhancement method, the indirect method of contrast enhancement. Histogram stretching and histogram equalization contrast enhancement is a common method of indirect. Histogram stretching using stretching the histogram to adjust the contrast, gray scale expansion difference between foreground and background, this method may be implemented by linear and nonlinear methods, wherein ps is the use of this method for improving the contrast; histograms equalization is to use a function of the cumulative value of the gradation adjustment, contrast enhancement.
Histogram equalization principle: the original grayscale image from a certain gradation range concentrated uniformly distributed throughout the gray space, to achieve non-linear stretching of the image, image pixel value redistribution.

Algorithms scenarios:

1, the essence of the algorithm is re-distribution image pixel value, increased the number of local contrast, the overall contrast of no much change, so the application of the image contrast of the image is similar useful data, such as: X-ray image, the exposure may be over- or underexposed photos better display, or the background and foreground image is too bright or too dark is very useful.
2, the algorithm of course, there are disadvantages, specifically as follows: the converted gray level image reduction, reduce certain details; some images high peak after the process unnatural excessive contrast enhancement.

Algorithm features:

1, equalization procedure: Histogram equalization to ensure that the magnitude relationship between the original image pixel remains constant in the mapping process, i.e., the lighter areas still brighter, darker still dark, but the contrast is increased, brightness can not be reversed; guaranteed pixel mapping function in the range between 0 and 255. Cumulative distribution function is a single growth function, and range is 0-1.
2, the cumulative distribution function of the implementation process:
Comparison of the probability distribution function and cumulative distribution function, the former two-dimensional image is uneven, which is monotonically increasing. Histogram equalization process, the mapping method is
Here Insert Picture Description
wherein, n being the sum of the pixels in the image, the gray level is the number of pixels of the current, L is the image of the total number of possible gray levels.

By stretching a look at how to achieve the above formula. Suppose the following images:
Here Insert Picture Description
obtain statistical information as shown in the image, and the complete mapping according to the gray value statistics:
Here Insert Picture Description
image mapping is as follows:
Here Insert Picture Description

Algorithm pseudo-code:

    1、计算原始灰度图像的像素概率分布

     2、根据像素概率分布获取图像累积分布函数

    3、根据映射函数获取变换后的图像

Based on MATLAB code:

%直方图均衡化  
I = imread('rice.png');  
[height,width] = size(I);  
figure  
subplot(221)  
imshow(I)%显示原始图像  
subplot(222)  
imhist(I)%显示原始图像直方图  
  
%进行像素灰度统计;  
NumPixel = zeros(1,256);%统计各灰度数目,共256个灰度级  
for i = 1:height  
    for j = 1: width  
        NumPixel(I(i,j) + 1) = NumPixel(I(i,j) + 1) + 1;%对应灰度值像素点数量增加一  
    end  
end  
%计算灰度分布密度  
ProbPixel = zeros(1,256);  
for i = 1:256  
    ProbPixel(i) = NumPixel(i) / (height * width * 1.0);  
end  
%计算累计直方图分布  
CumuPixel = zeros(1,256);  
for i = 1:256  
    if i == 1  
        CumuPixel(i) = ProbPixel(i);  
    else  
        CumuPixel(i) = CumuPixel(i - 1) + ProbPixel(i);  
    end  
end  
%累计分布取整  
CumuPixel = uint8(255 .* CumuPixel + 0.5);  
%对灰度值进行映射(均衡化)  
for i = 1:height  
    for j = 1: width  
        I(i,j) = CumuPixel(I(i,j));  
    end  
end  
  
subplot(223)  
imshow(I)%显示原始图像  
subplot(224)  
imhist(I)%显示原始图像直方图

1.2 Laplacian image enhancement based on

Using Laplacian image enhancement utilizing an essentially quadratic differential image is degenerate, in the field of differential image sharpening, blur is the integral of degenerate quadratic differential image by using neighboring pixels improve i.e. contrast. In opencv also has Laplace function, but when generating the gray scale image, more edge is obtained.

Convolution kernel applied to this experiment:
Here Insert Picture Description

Based on MATLAB code

%拉普拉斯算子锐化图像,用二阶微分
%四邻接g(x,y)=[f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x,y-1)]-4f(x,y)
clear
clc
I1=imread('./lena.jpg');
I=im2double(I1);
[m,n,c]=size(I);
A=zeros(m,n,c);
%分别处理R、G、B
%先对R进行处理
for i=2:m-1
    for j=2:n-1
        A(i,j,1)=I(i+1,j,1)+I(i-1,j,1)+I(i,j+1,1)+I(i,j-1,1)-4*I(i,j,1);
    end
end

%再对G进行处理
for i=2:m-1
    for j=2:n-1
        A(i,j,2)=I(i+1,j,2)+I(i-1,j,2)+I(i,j+1,2)+I(i,j-1,2)-4*I(i,j,2);
    end
end

%最后对B进行处理
for i=2:m-1
    for j=2:n-1
        A(i,j,3)=I(i+1,j,3)+I(i-1,j,3)+I(i,j+1,3)+I(i,j-1,3)-4*I(i,j,3);
    end
end
B=I-A;

 imwrite(B,'lena.tif','tif');
 imshow('./lena.jpg');title('原图');figure
 imshow('lena.tif');title('laplus图像')

Implemented in matlab comes fspecial function:

%matlab直接调用拉普拉斯方法
clear
clc
f=imread('./lena.jpg');
f2=im2double(f); %将f转换归一化的double类图像,然后进行滤波
w=fspecial('laplacian',0);
g1=imfilter(f,w,'replicate');
g=f-g1;
imshow(f);figure
imshow(g);

1.3 image object Log transform to enhance

Logarithmic transformation value of low gradation image portion can be extended, showing in more detail the low gradation part, which portion of the compressed high gradation value, reducing the details in part gradation value, so as to achieve the low grayscale enhanced image the purpose of the section. Conversion method:
Here Insert Picture Description
logarithmic transformation on the part of low gradation image detail enhancement function can be intuitively understood from the over-logarithmic graph:

0.4 x axis corresponds to the y-axis about 0.8, i.e. low gray picture portion (0-0.4) is elapsed after the extended operand (0 to 0.8) portion, and the whole (0.4-1) High portion is projected to be in the gray (0.8 to 1) interval, so that to achieve extended and enhanced low gradation part, the value of the high gradation compression section.
Can also be seen from the figure, for different base number, the greater the base number, the more low gradation portions extended, high gradation compression portion will be.

Code based on MATLAB

% 规则化200×200
I=imread('lena.jpg');

%log变换
I_3=mat2gray(I); %对数变换不支持uint8类型的数据,将一个矩阵归一化为灰度图像的数据格式
J=log(I_3+1);
subplot(1,2,1);
imshow(I);
title('原图像');
subplot(1,2,2);
imshow(J);
title('对数变换后的图像');

1.4 gamma conversion based on the image enhancement

Gamma conversion mainly for correcting an image, the gradation is too high or too low gradation corrected image, enhancing the contrast. Is a conversion formula for each of the pixel values of the original image do product computation:
Here Insert Picture Description
gamma conversion for correcting the image effect is actually achieved by the low gray detail enhancement or high gradation can be intuitively understood from the gamma curve:
Here Insert Picture Description
gamma] values 1 as a boundary, the smaller the value, the stronger the effect on the low gradation extension portion of the image, the larger the value, the stronger the effect of expansion of the high gradation image portion , different values of γ, you can achieve enhanced low ash effect of the high gradation part or detail.
Gamma conversion to the image contrast is low, and the overall brightness value is high (for the camera overexposure) in the case of image enhancement effect.

Based on MATLAB code

 f = imread('lena.jpg');  
I=rgb2gray(f);
f = mat2gray(I);

gamma1 = 0.1;
g_0_1 = f.^gamma1; 
gamma2 = 0.2;
g_0_2 = f.^gamma2; 
gamma3 = 0.4;
g_0_4 = f.^gamma3;
gamma4 = 0.6;
g_0_6 = f.^gamma4;
gamma5 = 0.8;
g_0_8 = f.^gamma5; 
gamma6 = 1;
g_1 = f.^gamma6;  
gamma7 = 2.5;
g_2_5 = f.^gamma7; 
gamma8 = 5;
g_5 = f.^gamma8; 
  
figure();  
subplot(3,3,1);  
imshow(f,[0 1]);  
xlabel('a).原图');  

subplot(3,3,2);  
imshow(g_0_1,[0 1]);  
xlabel('b).\gamma =0.1'); 

subplot(3,3,3);  
imshow(g_0_2,[0 1]);  
xlabel('c).\gamma =0.2'); 

subplot(3,3,4);  
imshow(g_0_4,[0 1]);  
xlabel('d).\gamma=0.4'); 

subplot(3,3,5);  
imshow(g_0_6,[0 1]);  
xlabel('e).\gamma=0.6'); 

subplot(3,3,6);  
imshow(g_0_8,[0 1]);  
xlabel('f).\gamma=0.8');  
  
subplot(3,3,7);  
imshow(g_1,[0 1]);  
xlabel('g).\gamma=1 i.e. 原图');  
  
subplot(3,3,8);  
imshow(g_2_5,[0 1]);  
xlabel('h).\gamma=2.5'); 

subplot(3,3,9);  
imshow(g_5,[0 1]);  
xlabel('i).\gamma=5'); 

Second, the experimental results

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Third, the experimental results

The results analysis:
1, log function of the change in terms of image enhancement, image contrast is more weakened, after all, a function of a small manifestation of a pixel value larger point, large pixel value gets smaller, so the contrast is reduced to increase the brightness, as a displayed image. So now it seems log function is used with caution, if the function can be used to improve the piecewise function ah.
2, gamma image enhancement function, the function principle can be seen that when r is greater than 1, can be used as an exponential function, compared with less than a log function. In the tests we used r> 1, the image contrast enhancement achieved. As can be seen from the image, with darker blacks and whiter whites.
3, laplus image enhancement function, the differential nature, so to image sharpening, in the image sharpening image is clearly highlights the details, thus improving image contrast on the visual.
Summary: Different image enhancement methods, different applications, better applications need to have flexible approach.

Fourth, the reference to articles and blog

1, image enhancement algorithm : This is the principle of most of the contents of the blog, the principle part of a small increase in change, based on OF matlab code adding section
2, translation Correction CorrectionGamma the Gamma
. 3, digital image processing - gamma conversion gradation conversion of ( gamma transformation)

Released nine original articles · won praise 13 · views 9509

Guess you like

Origin blog.csdn.net/ABV09876543210/article/details/104858685