欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

matalb计算灰度图像PSNR

程序员文章站 2023-12-31 16:27:16
...

原始图像和去噪图像分别在两个文件夹里

clc, clear all;
denoise_dir=fullfile('D:\视频数据集\4\denoise_10\');
label_dir=fullfile('D:\视频数据集\4\test_label\');
subdir_denoise =  dir(fullfile(denoise_dir,'*.jpg'));
subdir_label =  dir(fullfile(label_dir,'*.jpg'));
name_denoise={subdir_denoise.name}';
name_label={subdir_label.name}';
psnr_array=zeros(1,length(name_denoise));
for i =1:length(name_denoise)   %计算灰度图像psnr
    dir_denoise=fullfile(denoise_dir,name_denoise(i));
    dir_label=fullfile(label_dir,name_label(i));
    img1 =imread(char(dir_denoise));
    img2 =imread(char(dir_label));
    img2=double(img2);
    img1=double(img1);
    mse_value = mean( (img2(:) - img1(:)).^2 );
    psnr_i = 10*log10( 255*255 / mse_value );
    psnr_array(1,i) = psnr_i;
end
psnr_ave = sum(psnr_array)./length(name_denoise);

补充一些将图像数据存放为mat文件的东西。
在tensorflow中,通常会遇到把一些图像数据转成mat文件后再放入网络中,在这里我记录一下我遇到的问题。
背景:我是做一个去噪网络的实验,我是将连续8帧放进网络,我有一个视频是300帧,我最初是把480x832x300的图像矩阵用matlab中的reshape函数转成480x832x8x30,经过测试后,输出的任然是mat文件,我有用同样的方法转成480x832x300,发现图像之间已经完全不对应了,所以重新写了for循环,最后是对应的。总体代码如下:

读取视频

function y = read_video(file_name, frames)
    fprintf('Loading test sequence "%s" \n', file_name);
    mov       = VideoReader(file_name);
    if ~exist('frames','var') || isempty(frames)
        frames = mov.NumberOfFrames;
    end
    nFrames   = min(frames,mov.NumberOfFrames);
    vidHeight = mov.Height;
    vidWidth  = mov.Width;
    isRGB = strcmpi(mov.VideoFormat,'RGB24');
    if isRGB
        y = zeros(vidHeight,vidWidth,3,nFrames);
    else
        y = zeros(vidHeight,vidWidth,nFrames);
    end
    for n = 1:nFrames,
        frame = read(mov, n);
        if isRGB
            y(:,:,:,n)  = frame;
        else
            y(:,:,n)  = frame;
        end
    end
end

给视频加噪

y = read_video('D:\视频数据集\Chipmunks_832x480.avi');
S = 255;
sigma =10;
I_MAX = 255/S;
% Create synthetic Gaussian noise
randn('seed',0);
y = cast(y, 'single')/S;
sigma = sigma/S;
z = y + sigma*randn(size(y));
z(:,:,1,1);
size(z)
f = round(size(y,3)/2);
a=size(y);
size(y,3);
figure,imshow([y(:,:,:,1),z(:,:,:,1)]);
test_input1 = zeros(480,832,240);
test_label1 = zeros(480,832,240);

获取加噪图像的Y分量,并保存为图片

for f=1:240
    one = z(:,:,:,f);
    R = one(:,:,1);
    G = one(:,:,2);
    B = one(:,:,3);
%     imshow([R,G,B]);
    Y = 0.299 * R + 0.587 * G + 0.114 * B;
    U = -0.1678 * R - 0.3313 * G + 0.5 * B;
    V = 0.5 * R - 0.4187 * G - 0.0813 * B;
    test_input1(:,:,f) = Y;
    imwrite(Y,strcat('D:/视频数据集/4/test_input_Y/',num2str(f),'.png'),'png');
    imwrite(U,strcat('D:/视频数据集/4/test_input_U/',num2str(f),'.png'),'png');
    imwrite(V,strcat('D:/视频数据集/4/test_input_V/',num2str(f),'.png'),'png');
end

获取干净图像的Y分量,并保存为图片

for f=1:240
    one = y(:,:,:,f);
    R = one(:,:,1);
    G = one(:,:,2);
    B = one(:,:,3);
    Y = 0.299 * R + 0.587 * G + 0.114 * B;
    U = -0.1678 * R - 0.3313 * G + 0.5 * B;
    V = 0.5 * R - 0.4187 * G - 0.0813 * B;
    test_label1(:,:,f) = Y;
    imwrite(Y,strcat('D:/视频数据集/4/test_label/',num2str(f),'.jpg'),'jpg');
end

将尺寸大小为480x832x240的图像矩阵转换为480x832x8x30的矩阵并保存为mat文件

for i=1:30
    test_input(:,:,:,i) = test_input1(:,:,((8*(i-1)+1):(8*(i-1)+8)));
end
size(test_input)

test_label = zeros(480,832,8,30);
for i=1:30
    test_label(:,:,:,i) = test_label1(:,:,((8*(i-1)+1):(8*(i-1)+8)));
end

save test_label test_label -v7.3    % 注意要加上 -v7.3,不然tensorflow读不进去,我就采坑了
save test_input test_input -v7.3

将去噪的mat文件转化为图像并保存

load 'D:\DnCNN\VBM4D_v1 (1)\sfcnn_result.mat'
denoise =sfcnn_result;
size(denoise)
denoise = squeeze(denoise);
size(denoise)
for i=1:30
    for j=1:8
        H=denoise(i,j,:,:);
        size(H)
        H =(squeeze(H))';
        size(H);
        imwrite(double(H),strcat('D:/视频数据集/4/denoise_10/',num2str(8*(i-1)+j),'.jpg'),'jpg');
    end
end

最后,想计算psnr的话,就用本文开头的代码吧。

相关标签: PSNR

上一篇:

下一篇: