11 kinds of image sharpness evaluation function attached MATLAB code

 

Bloggers first blog!                                      

Undergraduate thesis was "based autofocus technology for image processing research" an important stage in the process of focusing the image clarity is evaluated, bloggers themselves with MATLAB four categories sharpness function: definition image gradient-based evaluation function , frequency domain evaluation function, information entropy function, statistical evaluation function, a total of 11 kinds. MATLAB bloggers have not used before, entirely undergraduate thesis needs, no way students look at another code, although never learned the basics of matlab, but after several learned the language, are similar, or be able to read of. Which part of the knowledge required to search the Internet, although not the same, but will be relevant to the search, and then read, to realize their own code.

Based on which a large part of the image clarity evaluation function gradient bloggers learn from others upload complete set of code, attached hereto link.

http://www.pudn.com/Download/item/id/1394579.html

First, the evaluation function based on the sharpness of the image gradient 

The essence of such an algorithm is knowledge of image processing, image processing because the direction of the election when bloggers post-graduate education, undergraduate thesis so look for an advisor in this regard.

A positive power than clear images sharper blur sharp edges defocused image, an edge pixel gray value changes, and therefore a greater gradient value. During image processing, the image is regarded as a two-dimensional discrete matrix with a gradient of image intensity information acquiring function, in order to judge the image sharpness. Gradient signal in discrete form of differential expression. Gradient functions are used: energy gradient function EOG, Roberts function Tenengrad function, Brenner function Variance Variance function, like the Laplace Laplace function. The following describes them separately, which f(x,y)indicates that the corresponding pixel gray value.

1. The energy gradient function (Energy of Gradient, EOG)

The difference between the x-direction and y-direction of the adjacent pixel grayscale values [ ] of squares as the gradient value of each pixel, the gradient of the pixel values of all the accumulated value as the sharpness function, expression is as follows:

matlab code is as follows:

%EOG(Energy Of Grad)
 N1 = 5;      %要处理的图片张数
 A = zeros(1,N1);  %存储每一幅图像清晰度评价值
 X = zeros(1,N1);  %存储做归一化处理后的评价值
tic     %计时
for L=1: N1 
 I=imread([int2str(L),'.jpg']); %连续读取图片
 I=double(I); 
 [M N]=size(I); 
 FI=0; 
 for x=1:M-1 
     for y=1:N-1 
          % x方向和y方向的相邻像素灰度值只差的的平方和作为清晰度值
         FI=FI+(I(x+1,y)-I(x,y))*(I(x+1,y)-I(x,y))+(I(x,y+1)-I(x,y))*(I(x,y+1)-I(x,y));
     end 
 end 
  
 A(1,L) = FI; 
end 
time=toc
%-------------------------------- 
%对图像清晰度值做归一化处理,线性函数归一化公式
 for W = 1:N1 
     C = max(A); 
     D = min(A); 
     E = C-D; 
     R = (A(1,W) - D)/(E); 
     X(1,W) = R; 
 end 
%做曲线拟合输出函数曲线  
x1=[-20 -10 0 10 20 ]; 
y1 = [X(1,1) X(1,2) X(1,3) X(1,4) X(1,5)];
[p,S]=polyfit(x1,y1,2); 
Y=polyconf(p,x1,y1); 
plot(x1,y1,'y');
hold on;

2.Roberts function

Roberts gradient function similar to the function of the energy, which is the difference between the grayscale values ​​in the diagonal direction using the pixel. Gradation value four adjacent cross points of the subtracted pixels square and a gradient value of each pixel, the gradient of the pixel values ​​of all the accumulated value as the sharpness function, expression of the following formula:

%Roberts
 N1 = 5; 
 A = zeros(1,N1); 
 X = zeros(1,N1);
 tic
 for L=1: N1 
 I=imread([int2str(L),'.jpg']); 
 I=double(I); 
 [M N]=size(I); 
 FI=0; 
 %Robert算子原理,对角方向相邻的两像素之差 
 for x=1:M-1 
     for y=1:N-1 
         FI= FI + (abs(I(x,y)-I(x+1,y+1))+abs(I(x+1,y)-I(x,y+1))); 
     end 
 end 
 A(1,L) = FI;  
 end 
 time=toc
 
  for W = 1:N1 
     C = max(A); 
     D = min(A); 
     E = C-D; 
     R = (A(1,W) - D)/(E); 
     X(1,W) = R; 
 end 
 
x1=[-20 -10 0 10 20 ]; 
y1 = [X(1,1) X(1,2) X(1,3) X(1,4) X(1,5)];
[p,S]=polyfit(x1,y1,2); 
Y=polyconf(p,x1,y1); 
plot(x1,y1,'c'); 
hold on;

3.Tenengrad function

Sobel operator using the extracted pixel values ​​of gradient in the horizontal direction and the vertical direction, the square Tenengrad function is defined as the gradient and the pixels, and T-regulatory function of a set gradient threshold sensitivity. Expression is shown by the following formula:

Code:

%Tenengrad
 N1 =5; 
 A = zeros(1,N1); 
 X = zeros(1,N1); 
 tic
 for L=1: N1 
 I=imread([int2str(L),'.jpg']); 
 I=double(I); 
 [M N]=size(I); 
 %利用sobel算子gx,gy与图像做卷积,提取图像水平方向和垂直方向的梯度值
GX = 0;   %图像水平方向梯度值
GY = 0;   %图像垂直方向梯度值
FI = 0;   %变量,暂时存储图像清晰度值
T  = 0;   %设置的阈值
 for x=2:M-1 
     for y=2:N-1 
         GX = I(x-1,y+1)+2*I(x,y+1)+I(x+1,y+1)-I(x-1,y-1)-2*I(x,y-1)-I(x+1,y-1); 
         GY = I(x+1,y-1)+2*I(x+1,y)+I(x+1,y+1)-I(x-1,y-1)-2*I(x-1,y)-I(x-1,y+1); 
         SXY= sqrt(GX*GX+GY*GY); %某一点的梯度值
         %某一像素点梯度值大于设定的阈值,将该像素点考虑,消除噪声影响
         if SXY>T 
           FI = FI + SXY*SXY;    %Tenengrad值定义
         end 
     end 
 end 
 A(1,L) = FI; 
 end 
 time=toc
 
% X = zeros(1,N1); 
 for W = 1:N1 
     C = max(A); 
     D = min(A); 
     E = C-D; 
     R = (A(1,W) - D)/(E); 
     X(1,W) = R; 
 end 
x1=[-20 -10 0 10 20 ]; 
y1 = [X(1,1) X(1,2) X(1,3) X(1,4) X(1,5)];
[p,S]=polyfit(x1,y1,2); 
Y=polyconf(p,x1,y1); 
plot(x1,y1,'g'); 
hold on;

 

4.Brenner function

Gradient method, also known as a filter, a difference of two pixels on the difference only needs to calculate the x-direction, i.e., second order gradient calculation, calculation amount decreases, the expression as follows:

Code:

%Brenner
 N1 = 5;           %N1为要处理的图片张数
 A = zeros(1,N1);   %zeros()定义指定行列的零矩阵;A矩阵用来存储每一幅图像的清晰度原值
 X = zeros(1,N1);   %X用来存储做归一化处理后的函数值
 %------------------------------
 tic
 for L=1: N1        
 I=imread([int2str(L),'.jpg']); %读取图片,将值转换为字符串接受向量和矩阵输入
I=double(I);         %精度存储问题
 [M N]=size(I);     %M等于矩阵行数,N等于矩阵列数;size()获取矩阵行列
 FI=0;        %变量,暂时存储每一幅图像的Brenner值
 for x=1:M-2      %Brenner函数原理,计算相差两个位置的像素点的灰度值
     for y=1:N 
         FI=FI+(I(x+2,y)-I(x,y))*(I(x+2,y)-I(x,y)); 
     end 
 end 
 A(1,L) = FI; 
 end
 time=toc
 %对原始数据做归一化处理,线性函数归一化公式
  for W = 1:N1 
     C = max(A); 
     D = min(A); 
     E = C-D; 
     R = (A(1,W) - D)/(E); 
     X(1,W) = R; 
  end 
%曲线拟合
x1=[-20 -10 0 10 20 ]; 
y1 = [X(1,1) X(1,2) X(1,3) X(1,4) X(1,5)]; 
[p,S]=polyfit(x1,y1,2);   %polyfit(x,y,n)曲线拟合函数,已知离散点坐标拟合曲线;x,y横纵坐标,n为拟合阶数,一阶直线拟合,二阶抛物线拟合 ,返回幂次从高到低的多项式系数向量P,矩阵S用于生成预测值的误差估计
Y=polyconf(p,x1,y1); %置信区间
plot(x1,y1,'r');     %画出拟合曲线,红线red
title('梯度评价函数');
xlabel('成像面位置');
ylabel('归一化后的图像清晰度评价值');
hold on;

5.Variance variogram

Variance discrete function represents the degree of the image intensity distribution. Focus image range of small gradation value conversion, low degree of dispersion, a small variance; Taisho power converting image gray value range, a high degree of dispersion, a large variance. Thus it can be used as an evaluation function, for the M * N size of the image shown in the following expression formulas:

 Code:

%Variance
 N1 = 5; 
 A = zeros(1,N1); 
 X = zeros(1,N1); 
tic
 for L=1: N1
 I=imread([int2str(L),'.jpg']);  
 I=double(I); 
 [M N]=size(I);  
 gama = 0;   %gama图像平均灰度值
 %求gama
 for x=1:M 
     for y=1:N 
         gama = gama + I(x,y); 
     end 
 end 
 gama = gama/(M*N); 
  
 FI=0; 
 for x=1:M 
     for y=1:N 
         FI=FI+(I(x,y)-gama)*(I(x,y)-gama); 
     end 
 end 
  A(1,L) = FI;
 end 
 time=toc
 for W = 1:N1 
     C = max(A); 
     D = min(A); 
     E = C-D; 
     R = (A(1,W) - D)/(E); 
     X(1,W) = R; 
 end 
  
x1=[-20 -10 0 10 20 ]; 
y1 = [X(1,1) X(1,2) X(1,3) X(1,4) X(1,5)];
 [p,S]=polyfit(x1,y1,2); 
 Y=polyconf(p,x1,y1); 
 plot(x1,y1,'b');
 hold on;

6. Laplace Laplace function

Laplace operator using the gradation value of each pixel of the image convolution matrix is referred to as a gradient G(x,y), to take the square of each pixel of the gradient and as a function of the evaluation, the following formula:

Code:

%Laplace 
 N1 = 5; 
 A = zeros(1,N1); 
 X = zeros(1,N1);
 tic
 for L=1: N1 
 I=imread([int2str(L),'.jpg']); 
 I=double(I); 
 [M N]=size(I); 
 FI=0; 
 for x=2:M-1 
     for y=2:N-1 
         IXXIYY = -4*I(x,y)+I(x,y+1)+I(x,y-1)+I(x+1,y)+I(x-1,y); 
             FI=FI+IXXIYY*IXXIYY;        %取各像素点梯度的平方和作为清晰度值    
     end 
 end  
A(1,L) = FI; 
 end 
 time=toc
 
 for W = 1:N1 
     C = max(A); 
     D = min(A); 
     E = C-D; 
     R = (A(1,W) - D)/(E); 
     X(1,W) = R; 
 end 
 
x1=[-20 -10 0 10 20 ]; 
y1 = [X(1,1) X(1,2) X(1,3) X(1,4) X(1,5)];
[p,S]=polyfit(x1,y1,2); 
Y=polyconf(p,x1,y1); 
plot(x1,y1,'m'); 
hold off;
 

II: evaluation function in the frequency domain

1. Image sharpness evaluation function based on the two-dimensional discrete Fourier transform

 Wherein the weighting factor \sqrt{{u}^{2}+{v}^{2}}represents the distance from the center pixel to the pixel, whose role is to emphasize the high-frequency component of the frequency spectrum. Because the Fourier transformation and after the image shift, the frequency distribution from the center to the peripheral edges of diffusion from the low to the high frequency; M, N is the size of the image; P (u, v) represents the square of the frequency spectrum of the image.

Code:

%DFT
N1 = 5;      %N1要处理的图像张数        
A = zeros(1,N1);  %A向量用来存储每一幅图像的清晰度原值
X = zeros(1,N1);  %X用来存储做归一化处理后的清晰度函数值
%----------------------
tic
for L=1: N1        
I=imread([int2str(L),'.jpg']); 
I=rgb2gray(I);
I=double(I);        
[M N]=size(I);          
fftI = fft2(I);   %进行二维离散傅里叶变换
sfftI = fftshift(fftI);   %移位,直流分量移到图像中心
magnitude = abs(sfftI);      %取模值
FI=0; 
for u=1:M
    for v=1:N
        FI=FI+sqrt(u*u+v*v)*magnitude(u,v);      %基于离散傅里叶变换的清晰度评价函数
    end
end
A(1,L) = FI/(M*N);
end
time=toc
%对原始数据做归一化处理,线性函数归一化公式
%-------------------------
  for W = 1:N1 
     C = max(A); 
     D = min(A); 
     E = C-D; 
     R = (A(1,W) - D)/(E); 
     X(1,W) = R; 
  end 
%曲线拟合,0为正焦位置,离焦-正焦-离焦
x1=[-20 -10 0 10 20 ]; 
y1 = [X(1,1) X(1,2) X(1,3) X(1,4) X(1,5)];
[p,S]=polyfit(x1,y1,2);   %polyfit(x,y,n)曲线拟合函数,已知离散点坐标拟合曲线;x,y横纵坐标,n为拟合阶数,一阶直线拟合,二阶抛物线拟合 ,返回幂次从高到低的多项式系数向量P,矩阵S用于生成预测值的误差估计
Y=polyconf(p,x1,y1); %置信区间
plot(x1,y1,'b');     %画出拟合曲线,红线red
title('频域评价函数');
xlabel('成像面位置');
ylabel('归一化后的图像清晰度评价值');
hold on;

2. discrete cosine transform DCT

DFT evaluation function based on the high sensitivity, but the Fourier transform coefficients are complex, computationally intensive. A discrete cosine transform DCT to improve this, the transform coefficients are real numbers, and can also reduce the computation image representing the frequency distribution information. For an M * N of the image, based on the discrete cosine transform DCT transform sharpness function is defined as follows:

%DCT
N1 = 5;      %N1要处理的图像张数        
A = zeros(1,N1);  %A向量用来存储每一幅图像的清晰度原值
X = zeros(1,N1);  %X用来存储做归一化处理后的清晰度函数值
%----------------------
tic
for L=1: N1        
I=imread([int2str(L),'.jpg']); 
I=rgb2gray(I);
I=double(I)+10*randn(size(I));        
[M N]=size(I);          
dctI = dct2(I);   %进行二维离散傅里叶变换
magnitude = abs(dctI);      %取模值
FI=0; 
for u=1:M
    for v=1:N
        FI=FI+(u+v)*magnitude(u,v);      %基于离散傅里叶变换的清晰度评价函数
    end
end
A(1,L) = FI/(M*N);
end
time=toc
%对原始数据做归一化处理,线性函数归一化公式
%-------------------------
  for W = 1:N1 
     C = max(A); 
     D = min(A); 
     E = C-D; 
     R = (A(1,W) - D)/(E); 
     X(1,W) = R; 
  end 
%曲线拟合,0为正焦位置,离焦-正焦-离焦
x1=[-20 -10 0 10 20 ]; 
y1 = [X(1,1) X(1,2) X(1,3) X(1,4) X(1,5)]; 
[p,S]=polyfit(x1,y1,2);   %polyfit(x,y,n)曲线拟合函数,已知离散点坐标拟合曲线;x,y横纵坐标,n为拟合阶数,一阶直线拟合,二阶抛物线拟合 ,返回幂次从高到低的多项式系数向量P,矩阵S用于生成预测值的误差估计
Y=polyconf(p,x1,y1); %置信区间
plot(x1,y1,'r');     %画出拟合曲线,红线red
hold off;

Third, based on information entropy sharpness function

In information theory, entropy is used to describe the richness of information. Information entropy function has a positive power images based on gray level distribution diversity, wide distribution of pixel gray value range, a large difference between the tone value, at this time a large entropy; opposite defocused image. Thus based on Information Entropy sharpness function, defined as follows:

Code: 

%entropy
N1 = 5;           %N1为要处理的图片张数
A = zeros(1,N1);   %zeros()定义指定行列的零矩阵;A矩阵用来存储每一幅图像的清晰度原值
X = zeros(1,N1);   %X用来存储做归一化处理后的函数值
%处理图片
tic
for L=1: N1        
 I=imread([int2str(L),'.jpg']); %读取图片,将值转换为字符串接受向量和矩阵输入
 I=rgb2gray(I);
 I=double(I); 
 A(1,L)=entr(I);    %调用求熵值函数
end
time=toc
%对原始数据做归一化处理,线性函数归一化公式
 for W = 1:N1 
   C = max(A); 
   D = min(A); 
   E = C-D; 
   R = (A(1,W) - D)/(E); 
   X(1,W) = R; 
  end 
x1=[-20 -10 0 10 20 ]; 
y1 = [X(1,1) X(1,2) X(1,3) X(1,4) X(1,5)]; 
[p,S]=polyfit(x1,y1,2);   %polyfit(x,y,n)曲线拟合函数,已知离散点坐标拟合曲线;x,y横纵坐标,n为拟合阶数,一阶直线拟合,二阶抛物线拟合 ,返回幂次从高到低的多项式系数向量P,矩阵S用于生成预测值的误差估计
Y=polyconf(p,x1,y1); %置信区间
plot(x1,y1,'r');     %画出拟合曲线,红线red
title('基于信息熵的评价函数');
xlabel('成像面位置');
ylabel('归一化后的图像清晰度评价值');


%定义子函数entr(),求一幅图像的熵值
%-------------------------------------------
function[H_img]= entr(I)   
[C,R]=size(I); %求图像的规格
Img_size=C*R; %图像像素点的总个数
L=256; %图像的灰度级0-255
H_img=0;  %图象熵
nk=zeros(L,1); %存储图像灰度出现次数
for i=1:C
for j=1:R
Img_level=I(i,j)+1; %获取图像的灰度级
nk(Img_level)=nk(Img_level)+1; %统计每个灰度级像素的点数
end
end
for k=1:L
Ps(k)=nk(k)/Img_size; %计算每一个灰度级像素点所占的概率
if Ps(k)~=0 %去掉概率为0的像素点
H_img=-Ps(k)*log2(Ps(k))+H_img; %求熵值的公式
end
end
end

Fourth, based on statistical definition evaluation function

1.Range function

Range function based on the change in size gradation histogram reflecting band sharpness, is defined as follows:

 Code:

%Range
N1 =5; 
gray_level = 32; %灰度直方图中划分的灰度等级
temp=zeros(1,gray_level);
A = zeros(1,N1);  
X = zeros(1,N1);
tic
for L=1: N1        
 I=imread([int2str(L),'.jpg']); %读取图片,将值转换为字符串接受向量和矩阵输入
 I=rgb2gray(I); 
 I=double(I);   %-------没做数值类型,出错,曲线相反----------
 [count,K] = imhist(I,gray_level);%imhist()画灰度分布直方图,count表示某一灰度区间的像素个数,K表示灰度区间取值
 for y=1:gray_level 
  temp(1,y)=count(y)*K(y);
 end
 A(1,L)=max(temp)-min(temp);
end
time=toc
for W = 1:N1 
     C = max(A); 
     D = min(A); 
     E = C-D; 
     R = (A(1,W) - D)/(E); 
     X(1,W) = R; 
end 
x1=[-20 -10 0 10 20 ]; 
y1 = [X(1,1) X(1,2) X(1,3) X(1,4) X(1,5)];
[p,S]=polyfit(x1,y1,2);   %polyfit(x,y,n)曲线拟合函数,已知离散点坐标拟合曲线;x,y横纵坐标,n为拟合阶数,一阶直线拟合,二阶抛物线拟合 ,返回幂次从高到低的多项式系数向量P,矩阵S用于生成预测值的误差估计
Y=polyconf(p,x1,y1); %置信区间
plot(x1,y1,'g');     %画出拟合曲线,红线red
title('统计学评价函数');
xlabel('成像面位置');
ylabel('归一化后的图像清晰度评价值');
hold on

2 Vollaths function

Vollaths function known as auto-correlation function, reflecting the similarity of two points in space. A positive power sharp edges of the image, the low degree of correlation between pixel points; defocused image pixel high degree of correlation. Sharpness function as follows:

 Code:

%vollaths
 N1 = 5;           %N1为要处理的图片张数
 A = zeros(1,N1);   %zeros()定义指定行列的零矩阵;A矩阵用来存储每一幅图像的清晰度原值
 X = zeros(1,N1);   %X用来存储做归一化处理后的函数值
 %用一个for循环处理每一张图片
 tic
 for L=1: N1        
 I=imread([int2str(L),'.jpg']); %读取图片,将值转换为字符串接受向量和矩阵输入
 I=double(I);        %精度存储问题
 [M N]=size(I);     %M等于矩阵行数,N等于矩阵列数;size()获取矩阵行列数
 %begintime=clock; 
  
 FI=0;        %变量,暂时存储每一幅图像的Brenner值
 for x=1:M-2      %Brenner函数原理,计算相差两个位置的像素点的灰度值
     for y=1:N 
         FI=FI+I(x,y)*abs(I(x+1,y)-I(x+2,y)); 
     end 
 end 
 %time=etime(clock,begintime); 
 A(1,L) = FI; 
 end 
 time=toc
 %对原始数据做归一化处理,线性函数归一化公式
  for W = 1:N1 
     C = max(A); 
     D = min(A); 
     E = C-D; 
     R = (A(1,W) - D)/(E); 
     X(1,W) = R; 
  end 
 
x1=[-20 -10 0 10 20 ]; 
y1 = [X(1,1) X(1,2) X(1,3) X(1,4) X(1,5)];
[p,S]=polyfit(x1,y1,2);   %polyfit(x,y,n)曲线拟合函数,已知离散点坐标拟合曲线;x,y横纵坐标,n为拟合阶数,一阶直线拟合,二阶抛物线拟合 ,返回幂次从高到低的多项式系数向量P,矩阵S用于生成预测值的误差估计
Y=polyconf(p,x1,y1); %置信区间
plot(x1,y1,'r');     %画出拟合曲线,红线red
hold off
 

The following is a blogger simulation results:

gradient

Frequency Domain

Entropy

statistics

 Initial contact with the contents of this area, deficiencies, please understand.

Guess you like

Origin blog.csdn.net/kungfu_rabbit/article/details/90243838