如何对图像下采样翻转等(SRfeat对图像的处理方式)

  1. zero的用法

zeros(3,4,5,6)

ans(:,:,1,1) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,2,1) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,3,1) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,4,1) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,5,1) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,1,2) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,2,2) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,3,2) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,4,2) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,5,2) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,1,3) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,2,3) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,3,3) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,4,3) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,5,3) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,1,4) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,2,4) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,3,4) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,4,4) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,5,4) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,1,5) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,2,5) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,3,5) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,4,5) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,5,5) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,1,6) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,2,6) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,3,6) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,4,6) =

 0     0     0     0
 0     0     0     0
 0     0     0     0

ans(:,:,5,6) =

 0     0     0     0
 0     0     0     0
 0     0     0     0
  1. filepaths = dir(fullfile(folder,’*.bmp’));%列出该文件夹下所有.bmp格式的文件(其中包括文件的名字、日期、像素等)
    aug_data_div2k.m
clear;close all;
% settings
folder = '/home/zhj/SPSR/code/data/GT/DIV2K_train_HR/';
savepath = '/home/zhj/SPSR/code/data/GT/DIV_train_cropped/';
size_input = 74;
%scale 
scale = 4; 
size_label = size_input * scale;
stride = size_input * scale;



% initialization
data = zeros(size_input, size_input, 3, 1);
label = zeros(size_label, size_label, 3, 1);
padding = 0;
count = 0;
do_write = 0;

totalct = 0;
created_flag = false;
change =0;
old =1;
% generate data
filepaths = dir(fullfile(folder,'*.png'));  %列出该文件夹下所有.png格式的文件

rand_index = randperm(length(filepaths));  %randperm()产生随机的序列
c=0

for index = 1 : length(filepaths)
        i = rand_index(index);
        filepaths(i).name %文件名

        image = imread(fullfile(folder,filepaths(i).name));%读出文件夹中名字为后面的图片
        [H, W, C] = size(image);
        
        [add, im_name, type] = fileparts(filepaths(i).name);%该函数用于将一个文件的完整路径中各部分提取出来。 返回指定文件的路径名称、文件名和扩展名
        left = floor(( W - size_label)/2)+1 ;
        top = floor((H - size_label)/2) +1;
        
		
        image1 = image;
        for kk = 1:35
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']
        y_s = randsample(1:size(image1,1)-size_label+1,1);%产生随机的序列
        x_s = randsample(1:size(image1,2)-size_label+1,1);%产生随机的序列
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);%随机的剪裁,前面是图像的名字
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');%重新定义大小
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));%保存起来
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));   
        end
        

        image1 = flip(image,2);%翻转
        for kk = 1:35
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']

        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));   
        end
        
        image1 = imrotate(image,90);%旋转90度
        for kk = 1:10
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']
        
        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));   
        end
        
        image1 = imrotate(image,180);%旋转180度
        for kk = 1:10
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']

        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));   
        end

        
        
        image1 = imrotate(image,270);%旋转270度
        for kk = 1:10
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']
        
        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));   
        end
        
        
        image1 = flip(image,2);
        image1 = imrotate(image1,90);%翻转完了,再旋转90度
        for kk = 1:10
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']
        
        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));   
        end        
        
        
        image1 = flip(image,2);
        image1 = imrotate(image1,180);
        for kk = 1:10
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']

        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));   
        end                
        
        image1 = flip(image,2);
        image1 = imrotate(image1,270);
        for kk = 1:10
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']
        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '.png']));   
        end                
end


第二个代码

clear;close all;
% settings
folder = './DIV_train/';
savepath = './DIV_train_cropped/';
size_input = 74;
%scale 
scale = 4; 
size_label = size_input * scale;
stride = size_input * scale;



% initialization
data = zeros(size_input, size_input, 3, 1);
label = zeros(size_label, size_label, 3, 1);
padding = 0;
count = 0;
do_write = 0;

totalct = 0;
created_flag = false;
change =0;
old =1;
% generate data
filepaths = dir(fullfile(folder,'*.png'));

rand_index = randperm(length(filepaths));
c=0
for index = 1 : length(filepaths)

        i = rand_index(index);
        filepaths(i).name
        image = imread(fullfile(folder,filepaths(i).name));
        image = imresize(image,1/2,'bicubic');

        [H, W, C] = size(image);
        [add, im_name, type] = fileparts(filepaths(i).name);
        left = floor(( W - size_label)/2)+1 ;
        top = floor((H - size_label)/2) +1;
        image1 = image;
        for kk = 1:5
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']

        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));   
        end
        

        image1 = flip(image,2);
        for kk = 1:5
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']

        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));   
        end
        
        image1 = imrotate(image,90);
        for kk = 1:5
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']
        

        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));   
        end
        
        image1 = imrotate(image,180);
        for kk = 1:5
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']
        
        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));   
        end
        
        image1 = imrotate(image,270);
        for kk = 1:5
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']

        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));   
        end
        
        
        image1 = flip(image,2);
        image1 = imrotate(image1,90);
        for kk = 1:5
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']

        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));   
        end        
        
        
        image1 = flip(image,2);
        image1 = imrotate(image1,180);
        for kk = 1:5
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']

        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));   
        end                
        
        image1 = flip(image,2);
        image1 = imrotate(image1,270);
        for kk = 1:5
        do_write = do_write +1;
        fprintf('do_write: %d\n', do_write);
        outname = [filepaths(i).name(1:end-5) '.png']

        y_s = randsample(1:size(image1,1)-size_label+1,1);
        x_s = randsample(1:size(image1,2)-size_label+1,1);
        subim_label2 = imcrop(image1, [x_s,y_s,size_label-1,size_label-1]);
        subim_input2 = imresize(subim_label2,1/scale,'bicubic');
        imwrite(subim_label2, fullfile(savepath,'GT',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));
        imwrite(subim_input2, fullfile(savepath,'LR_bicubic',[filepaths(i).name(1:end-5) '_' num2str(do_write) '_2.png']));   
        end                
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值