% =========================================================================
% Test code for Super-Resolution Convolutional Neural Networks (SRCNN)
%
% Reference
% Chao Dong, Chen Change Loy, Kaiming He, Xiaoou Tang. Learning a Deep Convolutional Network for Image Super-Resolution,
% in Proceedings of European Conference on Computer Vision (ECCV),
%
% Chao Dong, Chen Change Loy, Kaiming He, Xiaoou Tang. Image Super-Resolution Using Deep Convolutional Networks,
% arXiv:1501.00092
%
% Chao Dong
% IE Department, The Chinese University of *
% For any question, send email to ndc.forward@gmail.com
% ========================================================================= close all;
clear all; %% read ground truth image
im = imread('Set5\butterfly_GT.bmp');
%im = imread('Set14\zebra.bmp'); %% set parameters
up_scale = ;
model = 'model\9-5-5(ImageNet)\x3.mat';
% up_scale = ;
% model = 'model\9-3-5(ImageNet)\x3.mat';
% up_scale = ;
% model = 'model\9-1-5(91 images)\x3.mat';
% up_scale = ;
% model = 'model\9-5-5(ImageNet)\x2.mat';
% up_scale = ;
% model = 'model\9-5-5(ImageNet)\x4.mat'; %% work on illuminance only
if size(im,)>
im = rgb2ycbcr(im);
im = im(:, :, );
end
im_gnd = modcrop(im, up_scale); %保证图像被up_scale整除
im_gnd = single(im_gnd)/; %Single(单精度浮点型)变量存储为 IEEE 位( 个字节)浮点数值的形式,它的范围在负数的时候是从 -3.402823E38 到 -1.401298E-45,而在正数的时候是从 1.401298E-45 到 3.402823E38。 %% bicubic interpolation
im_l = imresize(im_gnd, /up_scale, 'bicubic'); %缩小3倍
im_b = imresize(im_l, up_scale, 'bicubic'); %又放大三倍 %% SRCNN
im_h = SRCNN(model, im_b); %用网络处理一下 %% remove border %去除没有用的边界
im_h = shave(uint8(im_h * ), [up_scale, up_scale]); %表示变量是无符号整数,范围是0到255.
im_gnd = shave(uint8(im_gnd * ), [up_scale, up_scale]);
im_b = shave(uint8(im_b * ), [up_scale, up_scale]); %% compute PSNR
psnr_bic = compute_psnr(im_gnd,im_b);
psnr_srcnn = compute_psnr(im_gnd,im_h); %% show results
fprintf('PSNR for Bicubic Interpolation: %f dB\n', psnr_bic);
fprintf('PSNR for SRCNN Reconstruction: %f dB\n', psnr_srcnn); %保存 图片
imwrite(im_h,'img_h.png');
imwrite(im_b,'img_b.png');
imwrite(im_gnd,'img_gnd.png'); figure, imshow(im_b); title('Bicubic Interpolation');
figure, imshow(im_h); title('SRCNN Reconstruction'); %imwrite(im_b, ['Bicubic Interpolation' '.bmp']);
%imwrite(im_h, ['SRCNN Reconstruction' '.bmp']);
SRCNN的核心算法:
function im_h = SRCNN(model, im_b) %% load CNN model parameters
load(model);
[conv1_patchsize2,conv1_filters] = size(weights_conv1);
conv1_patchsize = sqrt(conv1_patchsize2);
[conv2_channels,conv2_patchsize2,conv2_filters] = size(weights_conv2);
conv2_patchsize = sqrt(conv2_patchsize2);
[conv3_channels,conv3_patchsize2] = size(weights_conv3);
conv3_patchsize = sqrt(conv3_patchsize2);
[hei, wid] = size(im_b); %% conv1
weights_conv1 = reshape(weights_conv1, conv1_patchsize, conv1_patchsize, conv1_filters);
conv1_data = zeros(hei, wid, conv1_filters);
for i = : conv1_filters
conv1_data(:,:,i) = imfilter(im_b, weights_conv1(:,:,i), 'same', 'replicate');
conv1_data(:,:,i) = max(conv1_data(:,:,i) + biases_conv1(i), );
end %% conv2
conv2_data = zeros(hei, wid, conv2_filters);
for i = : conv2_filters
for j = : conv2_channels
conv2_subfilter = reshape(weights_conv2(j,:,i), conv2_patchsize, conv2_patchsize);
conv2_data(:,:,i) = conv2_data(:,:,i) + imfilter(conv1_data(:,:,j), conv2_subfilter, 'same', 'replicate');
end
conv2_data(:,:,i) = max(conv2_data(:,:,i) + biases_conv2(i), );
end %% conv3
conv3_data = zeros(hei, wid);
for i = : conv3_channels
conv3_subfilter = reshape(weights_conv3(i,:), conv3_patchsize, conv3_patchsize);
conv3_data(:,:) = conv3_data(:,:) + imfilter(conv2_data(:,:,i), conv3_subfilter, 'same', 'replicate');
end %% SRCNN reconstruction
im_h = conv3_data(:,:) + biases_conv3;
图解里面变量和卷积