-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPrepare_Data_HR_LR.m
76 lines (64 loc) · 2.28 KB
/
Prepare_Data_HR_LR.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function Prepare_TrainData_HR_LR()
clear; close all; clc
% SET scale factor here
scale = 2;
% scale = 4;
% whether use off-line data augmentation
USE_AUG = true;
% SET data dir
sourcedir = 'dataset/train_data/hr_over';
% sourcedir = 'dataset/train_data/hr_under';
savedir = 'dataset/train_data';
saveHRpath = fullfile(savedir, 'discarded_images');
saveLRpath = fullfile(savedir, 'lr_over');
% saveLRpath = fullfile(savedir, 'lr_under');
if ~exist(saveHRpath, 'dir')
mkdir(saveHRpath);
end
if ~exist(saveLRpath, 'dir')
mkdir(saveLRpath);
end
filepaths = [dir(fullfile(sourcedir, '*.png'));dir(fullfile(sourcedir, '*.bmp'));dir(fullfile(sourcedir, '*.JPG'))];
downsizes = [];
% prepare data with or without augmentation
parfor i = 1 : length(filepaths)
filename = filepaths(i).name;
fprintf('No.%d -- Processing %s\n', i, filename);
[add, im_name, type] = fileparts(filepaths(i).name);
image = imread(fullfile(sourcedir, filename));
if USE_AUG
for angle = 0 : 1 : 3
for downidx = 0 : 1 : length(downsizes)
image_HR = image;
if downidx > 0
image_HR = imresize(image_HR, downsizes(downidx), 'bicubic');
end
image_HR = rot90(image_HR, angle);
image_HR = modcrop(image_HR, scale);
image_LR = imresize(image_HR, 1/scale, 'bicubic');
saveHRfile = [im_name '_rot' num2str(angle*90) '_ds' num2str(downidx) '.png'];
saveLRfile = [im_name '_rot' num2str(angle*90) '_ds' num2str(downidx) '.png'];
imwrite(image_HR, fullfile(saveHRpath, saveHRfile));
imwrite(image_LR, fullfile(saveLRpath, saveLRfile));
end
end
else
image_HR = modcrop(image, scale);
image_LR = imresize(image_HR, 1/scale, 'bicubic');
imwrite(image_HR, fullfile(saveHRpath, [im_name '.png']));
imwrite(image_LR, fullfile(saveLRpath, [im_name '.png']));
end
end
end
function imgs = modcrop(imgs, modulo)
if size(imgs,3)==1
sz = size(imgs);
sz = sz - mod(sz, modulo);
imgs = imgs(1:sz(1), 1:sz(2));
else
tmpsz = size(imgs);
sz = tmpsz(1:2);
sz = sz - mod(sz, modulo);
imgs = imgs(1:sz(1), 1:sz(2),:);
end
end