评价图像去雾质量

计算PSNR、SSIM、熵、MSE、梯度

去雾处理中需要对图片进行批量读取,批量处理,批量储存,这个代码是我在毕设过程中使用的代码,个人感觉质量还不错

%选择某一组图片进行评价
clc;clear all;close all
path0= 'D:\learnapp\matlab\bin\code\defog\persontask\clear2\';  %清晰图
path00= 'D:\learnapp\matlab\bin\code\defog\persontask\fog2\';  %雾图

path1='D:\learnapp\matlab\bin\code\defog\persontask\work1\pic_1\';
path2='D:\learnapp\matlab\bin\code\defog\persontask\work2\pic_1\';
path3='D:\learnapp\matlab\bin\code\defog\persontask\work3\pic_1\';
path4='D:\learnapp\matlab\bin\code\defog\persontask\work4\pic_1\';
path5='D:\learnapp\matlab\bin\code\defog\persontask\work5\pic_1\';
path6='D:\learnapp\matlab\bin\code\defog\persontask\work6\pic_1\';
path101='D:\learnapp\matlab\bin\code\defog\persontask\work101\pic_1\';%复原图
%addpath 'D:\learnapp\matlab\bin\code\defog\persontask\work2'

i=2;

clear_image=imread([path0,'image',num2str(i),'.jpg']);
fog_image=imread([path00,'image',num2str(i),'.jpg']);
recover_image3=imread([path3,'image',num2str(i),'.jpg']);
recover_image4=imread([path4,'image',num2str(i),'.jpg']);
recover_image5=imread([path5,'image',num2str(i),'.jpg']);
recover_image101=imread([path101,'image',num2str(i),'.jpg']);

figure;
subplot(2,3,1);imshow(clear_image);title('(a)清晰图像');
subplot(2,3,2);imshow(fog_image);title('(b)雾霾图像');
subplot(2,3,3);imshow(recover_image101);title('(c)本文算法');
subplot(2,3,4);imshow(recover_image5);title('(d)算法1');
subplot(2,3,5);imshow(recover_image3);title('(e)算法2');
subplot(2,3,6);imshow(recover_image4);title('(f)算法3');



[SNR,PSNR]=calculate_snr_psnr(clear_image,recover_image);
SSIM=calculate_ssim(clear_image,recover_image);
MSE=calculate_nmse(clear_image,recover_image);
shang=calculate_entropy(recover_image);
radient=Avg_Gradient(recover_image);

 

%图片写入
clc,clear

addpath 'D:\learnapp\matlab\bin\code\defog\persontask\work101'
%path1 = 'D:\learnapp\matlab\bin\code\defog\persontask\clear1\'; %%需要处理的图像文件夹
path2='D:\learnapp\matlab\bin\code\defog\persontask\work101\pic_4\';%%处理后图像所在文件夹
%path2='D:\learnapp\matlab\bin\code\defog\persontask\work1\pic_1\';
path1 = 'D:\learnapp\matlab\bin\code\defog\persontask\fog1\';


img_path_list = dir(strcat(path1, '*.jpg'));
%获取该文件夹中所有bmp格式的图像,strcat:串联字符串;dir:列出文件夹内容  
img_num = length(img_path_list);%获取图像总数量 

for j = 1 : img_num %逐一读取图像  
    image_name_old = img_path_list(j).name;% 图像名
    image = imread(strcat(path1, image_name_old));% 暂存原图像数据
    
    image_name_new = strcat('image', num2str(j), '.jpg');% 新图像名

   image1=main_file(image);
   %image1=image;
    imwrite(image1,strcat(path2, image_name_new));% 将新图像吗赋予原数据
end 


 %imwrite(K2,[Output_path,'wie',int2str(i),'.jpg']);

%该函数用于计算SNR信噪比和PSNR峰值信噪比
%信噪比与峰值信噪比的值越大代表图像的质量越好。
%I表示原始图像
%J表示恢复后的图像
function [snr,psnr] = calculate_snr_psnr(I,J)
    %如果是I灰度图像只有二维,如果I是彩色图像将会有三维
    dim = length(size(I));%保存的是I的维度
    M = size(I,1);
    N = size(I,2);
    dif = (I - J).^2;
    I_2 = I.^2;
    if dim == 2
        val1 = sum(sum(dif));
        val2 = sum(sum(I_2));
    else
        val1 = sum(sum(sum(dif)));
        val2 = sum(sum(sum(I_2)));
    end
    snr = 10*log10(val2/val1);
    psnr = 10*log10((255*255*M*N)/val1);
end

 

function EN=calculate_entropy(F)

%[name,path]=uigetfile({'*.*','请选择融合图像F'},'请打开融合图像F');file=strcat(path,name);F=imread(file);
if size(F,3)==3
    F=rgb2gray(F);
end
p=imhist(F);
p(p==0)=[];
p=p/numel(F);
EN=-sum(p.*log2(p));
return
function SSIM=calculate_ssim(img1,img2)

% ========================================================================
% SSIM Index with automatic downsampling, Version 1.0
% Copyright(c) 2009 Zhou Wang
% All Rights Reserved.
%
% ----------------------------------------------------------------------

% This is an implementation of the algorithm for calculating the
% Structural SIMilarity (SSIM) index between two images
%
% Please refer to the following paper and the website with suggested usage
%
% Z. Wang, A. C. Bovik, H. R. Sheikh, and E. P. Simoncelli, "Image
% quality assessment: From error visibility to structural similarity,"
% IEEE Transactios on Image Processing, vol. 13, no. 4, pp. 600-612,
% Apr. 2004.
%
% http://www.ece.uwaterloo.ca/~z70wang/research/ssim/
%
% Note: This program is different from ssim_index.m, where no automatic
% downsampling is performed. (downsampling was done in the above paper
% and was described as suggested usage in the above website.)
%
% Kindly report any suggestions or corrections to zhouwang@ieee.org
%
%----------------------------------------------------------------------
%
%Input : (1) img1: the first image being compared
%        (2) img2: the second image being compared
%        (3) K: constants in the SSIM index formula (see the above
%            reference). defualt value: K = [0.01 0.03]
%        (4) window: local window for statistics (see the above
%            reference). default widnow is Gaussian given by
%            window = fspecial('gaussian', 11, 1.5);
%        (5) L: dynamic range of the images. default: L = 255
%
%Output: (1) mssim: the mean SSIM index value between 2 images.
%            If one of the images being compared is regarded as 
%            perfect quality, then mssim can be considered as the
%            quality measure of the other image.
%            If img1 = img2, then mssim = 1.
%        (2) ssim_map: the SSIM index map of the test image. The map
%            has a smaller size than the input images. The actual size
%            depends on the window size and the downsampling factor.
%
%Basic Usage:
%   Given 2 test images img1 and img2, whose dynamic range is 0-255
%
%   [mssim, ssim_map] = ssim(img1, img2);
%
%Advanced Usage:
%   User defined parameters. For example
%
%   K = [0.05 0.05];
%   window = ones(8);
%   L = 100;
%   [mssim, ssim_map] = ssim(img1, img2, K, window, L);
%
%Visualize the results:
%
%   mssim                        %Gives the mssim value
%   imshow(max(0, ssim_map).^4)  %Shows the SSIM index map
%========================================================================

%[name,path]=uigetfile({'*.*','请选择原图像'},'请打开原图像');file1=strcat(path,name);img1=imread(file1);
%[name,path]=uigetfile({'*.*','请选择融合图像F'},'请打开融合图像F');file2=strcat(path,name);img2=imread(file2);

if size(img1,3)==3
    img1=rgb2gray(img1);
end
if size(img2,3)==3
    img2=rgb2gray(img2);
end
[M N] = size(img1);

img1 = double(img1);
img2 = double(img2);
K = [0.01 0.03]; window = fspecial('gaussian', 11, 1.5);L = 255;
% automatic downsampling
f = max(1,round(min(M,N)/256));
%downsampling by f
%use a simple low-pass filter 
if(f>1)
    lpf = ones(f,f);
    lpf = lpf/sum(lpf(:));
    img1 = imfilter(img1,lpf,'symmetric','same');
    img2 = imfilter(img2,lpf,'symmetric','same');

    img1 = img1(1:f:end,1:f:end);
    img2 = img2(1:f:end,1:f:end);
end

C1 = (K(1)*L)^2;
C2 = (K(2)*L)^2;
window = window/sum(sum(window));

mu1   = filter2(window, img1, 'valid');
mu2   = filter2(window, img2, 'valid');
mu1_sq = mu1.*mu1;
mu2_sq = mu2.*mu2;
mu1_mu2 = mu1.*mu2;
sigma1_sq = filter2(window, img1.*img1, 'valid') - mu1_sq;
sigma2_sq = filter2(window, img2.*img2, 'valid') - mu2_sq;
sigma12 = filter2(window, img1.*img2, 'valid') - mu1_mu2;

if (C1 > 0 & C2 > 0)
   ssim_map = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2));
else
   numerator1 = 2*mu1_mu2 + C1;
   numerator2 = 2*sigma12 + C2;
	denominator1 = mu1_sq + mu2_sq + C1;
   denominator2 = sigma1_sq + sigma2_sq + C2;
   ssim_map = ones(size(mu1));
   index = (denominator1.*denominator2 > 0);
   ssim_map(index) = (numerator1(index).*numerator2(index))./(denominator1(index).*denominator2(index));
   index = (denominator1 ~= 0) & (denominator2 == 0);
   ssim_map(index) = numerator1(index)./denominator1(index);
end
SSIM = mean2(ssim_map);
end

%该函数用于计算MSE计算归一化均方误差
%值越小图像质量越好
%I表示原始图像
%J表示恢复后的图像
function nmse = calculate_nmse(I,J)
    %如果是I灰度图像只有二维,如果I是彩色图像将会有三维
    dim = length(size(I));%保存的是I的维度
    dif = (I - J).^2;
    I_2 = I.^2;
    if dim == 2
        val1 = sum(sum(dif));
        val2 = sum(sum(I_2));
    else
        val1 = sum(sum(sum(dif)));
        val2 = sum(sum(sum(I_2)));
    end
    nmse = val1/val2;
end
function outval = Avg_Gradient(img) 
% 平均梯度,也称为清晰度,反映了图像中的微小细节反差与纹理变化特征,同时也反映了图像的清晰度,越大越清晰
 
if nargin == 1 
    img = double(img); 
    % Get the size of img 
    [r,c,b] = size(img); 
     
    dx = 1; 
    dy = 1; 
    for k = 1 : b 
        band = img(:,:,k); 
        [dzdx,dzdy] = gradient(band,dx,dy); 
        s = sqrt((dzdx .^ 2 + dzdy .^2) ./ 2); 
        g(k) = sum(sum(s)) / ((r - 1) * (c - 1)); 
    end 
    outval = mean(g); 
else 
    error('Wrong number of input!'); 
end

 

%评价并写入表格
clc;clear all;
path0= 'D:\learnapp\matlab\bin\code\defog\persontask\clear2\';  %清晰图
path1= 'D:\learnapp\matlab\bin\code\defog\persontask\fog2\';  %雾图
path2='D:\learnapp\matlab\bin\code\defog\persontask\work101\pic_4\';%复原图
%addpath 'D:\learnapp\matlab\bin\code\defog\persontask\work5'
%
img_path_list = dir(strcat(path1, '*.jpg'));
%获取该文件夹中所有bmp格式的图像,strcat:串联字符串;dir:列出文件夹内容  
img_num = length(img_path_list);%获取图像总数量 

PSNR=zeros(img_num,1);
SSIM=zeros(img_num,1);
MSE=zeros(img_num,1);
shang=zeros(img_num,1);
gradient=zeros(img_num,1); 


for i=1:img_num
 
  clear_image=imread([path0,'image',num2str(i),'.jpg']);%原图
  fog_image=imread([path1,'image',num2str(i),'.jpg']);%雾图
  recover_image=imread([path2,'image',num2str(i),'.jpg']);%去雾图
 
[SNR(i),PSNR(i)]=calculate_snr_psnr(clear_image,recover_image);
SSIM(i)=calculate_ssim(clear_image,recover_image);
MSE(i)=calculate_nmse(clear_image,recover_image);
shang(i)=calculate_entropy(recover_image);   
gradient(i)=Avg_Gradient(recover_image);
 
end

xlswrite('data2.xlsx',PSNR,1,'E1');%cdefg
xlswrite('data2.xlsx',SSIM,1,'P1');
xlswrite('data2.xlsx',MSE,1,'AA1');
xlswrite('data2.xlsx',shang,1,'AL1');
xlswrite('data2.xlsx',gradient,1,'AW1');



  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值