forked from tingfengainiaini/sparseCodingSuperResolution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_patches.m
64 lines (46 loc) · 1.47 KB
/
sample_patches.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
function [HP, LP] = sample_patches(im, patch_size, patch_num, upscale)
if size(im, 3) == 3,
hIm = rgb2gray(im);
else
hIm = im;
end
% generate low resolution counter parts
lIm = imresize(hIm, 1/upscale, 'bicubic');
lIm = imresize(lIm, size(hIm), 'bicubic');
[nrow, ncol] = size(hIm);
x = randperm(nrow-2*patch_size-1) + patch_size;
y = randperm(ncol-2*patch_size-1) + patch_size;
[X,Y] = meshgrid(x,y);
xrow = X(:);
ycol = Y(:);
if patch_num < length(xrow),
xrow = xrow(1:patch_num);
ycol = ycol(1:patch_num);
end
patch_num = length(xrow);
hIm = double(hIm);
lIm = double(lIm);
H = zeros(patch_size^2, length(xrow));
L = zeros(4*patch_size^2, length(xrow));
% compute the first and second order gradients
hf1 = [-1,0,1];
vf1 = [-1,0,1]';
lImG11 = conv2(lIm, hf1,'same');
lImG12 = conv2(lIm, vf1,'same');
hf2 = [1,0,-2,0,1];
vf2 = [1,0,-2,0,1]';
lImG21 = conv2(lIm,hf2,'same');
lImG22 = conv2(lIm,vf2,'same');
for ii = 1:patch_num,
row = xrow(ii);
col = ycol(ii);
Hpatch = hIm(row:row+patch_size-1,col:col+patch_size-1);
Lpatch1 = lImG11(row:row+patch_size-1,col:col+patch_size-1);
Lpatch2 = lImG12(row:row+patch_size-1,col:col+patch_size-1);
Lpatch3 = lImG21(row:row+patch_size-1,col:col+patch_size-1);
Lpatch4 = lImG22(row:row+patch_size-1,col:col+patch_size-1);
Lpatch = [Lpatch1(:),Lpatch2(:),Lpatch3(:),Lpatch4(:)];
Lpatch = Lpatch(:);
HP(:,ii) = Hpatch(:)-mean(Hpatch(:));
LP(:,ii) = Lpatch;
end