仅供参考,主要是为自己学习使用
一、bicubic双三次插值算法()
clear all
clc
im = {}; %%创建字典im以保存读取的图片
dis = dir('D:\EDSR\dataset\Set14\*.bmp');
b = length(dis)
for i = 1:length(dis)
path = strcat('D:\EDSR\dataset\Set14\',dis(i).name);
im{i} =imread(path); %%将读取到的图像存储在字典im中
up_scale = 2; %%将降采样系数设为2,即图像缩小2倍
%if size(im{i},3)>1 %%如果图像不是单通道,则转化为灰度图返回m第三个维度的长度,并赋值给num如果m的维度小于3则返回1.
% im{i} = rgb2ycbcr(im{i});
% im{i} = im{i}(:, :, 1);
%end
% im_gnd{i} = modcrop(im{i}, up_scale);%保证图像被scale整除
% im_gnd{i} = single(im_gnd{i})/255;
%% bicubic interpolation
im_l{i} = imresize(im{i}, 1/up_scale, 'bicubic');
%im_b{i} = imresize(im_l{i}, up_scale, 'bicubic');
%figure,imshow(im_l{i}) %%为了同时显示多幅图片,不加figure的话只会显示一副图片,因为读入的第i副图片会被第i+1副图片覆盖
mkdir set14_new;
%imwrite(picture,'/Set5_new/dis('num2str(i,'%05d')').name.png');
imwrite(im_l{i},['D:\EDSR\dataset\set14_new\' num2str(i,'%05d') '.png'])
end
a='wancheng'
二、单通道变三通道
clear all
clc
im = {}; %%创建字典im以保存读取的图片
dis = dir('D:\EDSR\dataset\GTdata\Set14\*.bmp');
b = length(dis)
for i = 1:length(dis)
path = strcat('D:\EDSR\dataset\GTdata\Set14\',dis(i).name);
im{i} =imread(path); %%将读取到的图像存储在字典im中
up_scale = 2; %%将降采样系数设为2,即图像缩小2倍
%if size(im{i},3)>1 %%如果图像不是单通道,则转化为灰度图返回m第三个维度的长度,并赋值给num如果m的维度小于3则返回1.
% im{i} = rgb2ycbcr(im{i});
% im{i} = im{i}(:, :, 1);
%end
if size(im{i},3)<3
I3(:,:,1)=im{i};
I3(:,:,2)=im{i};
I3(:,:,3)=im{i};
im{i}= I3;
end
imwrite(im{i},['./result/', num2str(i,'%05d') '.png'])
end
a = 'wancheng'
三、裁剪图片使得GT图像和inference后的图像大小一致
clear all
clc
im_result = {}; %%创建字典im以保存读取的图片
im_gt = {};
im_a = {};
dis_result = dir('D:\EDSR\dataset\data_inference\Set14_result\*.png');
dis_gt = dir('D:\EDSR\dataset\GTdata\Set14\*.png');
b = length(dis_result)
for i = 1:length(dis_result)
path_result = strcat('D:\EDSR\dataset\data_inference\Set14_result\',dis_result(i).name);
path_gt = strcat('D:\EDSR\dataset\GTdata\Set14\',dis_gt(i).name);
im_result{i} = imread(path_result); %%将读取到的图像存储在字典im中
im_gt{i} = imread(path_gt);
w_result = size(im_result{i},2);
w_gt = size(im_gt{i},2); %获取图像宽
h_result = size(im_result{i},1);
h_gt = size(im_gt{i},1);%获取图像高
% if w_result>w_gt && h_result==h_gt
% im_result{i} = imcrop(im_result{i},[0,0,w_result-1,h_result]);
% elseif w_result==w_gt && h_result>h_gt
% im_result{i} = imcrop(im_result{i},[0,0,w_result,h_result-1]);
% elseif w_result>w_gt && h_result>h_gt
% im_result{i} = imcrop(im_result{i},[0,0,w_result-1,h_result-1]);
% end
im_a{i} = imcrop(im_result{i},[0,0,w_gt,h_gt]);
%mkdir inference
imwrite(im_a{i},['inference/', num2str(i,'%05d') '.png'])
end
a = 'finish'