# 图像对比度计算

matlab中求解方式：

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%计算图像对比度
%方法一：中心像素灰度值与周围4近邻像素灰度值之差的平方之和，除以以上平方项的个数。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[m,n] = size(f);%求原始图像的行数m和列数n
%扩展只是对原始图像的周边像素进行复制的方法进行
[r,c] = size(g);%求扩展后图像的行数r和列数c
g = double(g);  %把扩展后图像转变成双精度浮点数
k = 0;  %定义一数值k，初始值为0
fori=2:r-1
forj=2:c-1
k = k+(g(i,j-1)-g(i,j))^2+(g(i-1,j)-g(i,j))^2+(g(i,j+1)-g(i,j))^2+(g(i+1,j)-g(i,j))^2;
end
end
cg = k/(4*(m-2)*(n-2)+3*(2*(m-2)+2*(n-2))+4*2);%求原始图像对比度
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%计算图像对比度
%方法二：中心像素灰度值与周围8近邻像素灰度值之差的平方之和，除以以上之差的个数。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[m,n] = size(f);%求原始图像的行数m和列数n
%扩展只是对原始图像的周边像素进行复制的方法进行
[r,c] = size(g);%求扩展后图像的行数r和列数c
g = double(g);  %把扩展后图像转变成双精度浮点数
k=0;  %定义一数值k，初始值为0
fori=2:r-1
forj=2:c-1
k = k+(g(i,j-1)-g(i,j))^2+(g(i-1,j)-g(i,j))^2+(g(i,j+1)-g(i,j))^2+(g(i+1,j)-g(i,j))^2+...
(g(i-1,j-1)-g(i,j))^2+(g(i-1,j+1)-g(i,j))^2+(g(i+1,j-1)-g(i,j))^2+(g(i+1,j+1)-g(i,j))^2;
end
end
cg = k/(8*(m-2)*(n-2)+6*(2*(m-2)+2*(n-2))+4*3);%求原始图像对比度