-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.m
More file actions
176 lines (144 loc) · 4.66 KB
/
train.m
File metadata and controls
176 lines (144 loc) · 4.66 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
clc;
clear;
close all;
scale = 2 ;
min_num = 512;
v = 0.5;
startt = tic;
folder1 =['Images1\Train_' num2str(scale) 'L'];
folder2 =['Images1\Train_' num2str(scale) 'H'];
filepaths = dir(fullfile(folder1,'*.bmp'));
% Counting the number of block2
numfile = length(filepaths);
numdigit = numel(num2str(numfile));
numblock = 0;
for i=1:numfile
sname = num2str(i,['%0',num2str(numdigit),'.0f']);
imageL = imread(fullfile(folder1,['L',num2str(scale),sname,'.bmp']));
sz = size(imageL);
numblock = numblock + (sz(1)-3)*(sz(2)-3);
end
%Initialisation matrices, including rotating 90, 180, 270 degrees
LR = zeros(numblock, 16); % low resolution data
HR = zeros(numblock, scale*scale); % high resolution data
%Assign matrices
offset = floor(scale/2);
pointer = 1;
for i = 1:numfile
sname = num2str(i,['%0',num2str(numdigit),'.0f']);
imageL = imread(fullfile(folder1,['L',num2str(scale),sname,'.bmp']));
imageL = im2double(imageL);
imageH = imread(fullfile(folder2,['H',num2str(scale),sname,'.bmp']));
imageH = im2double(imageH);
sz = size(imageL);
imagepadding = zeros(sz(1)+2,sz(2)+2);
imagepadding(2:end-1,2:end-1) = imageL;
for ii = 2:sz(1)-2
for jj = 2:sz(2)-2
LRblock = imagepadding( ii : ii + 3, jj : jj + 3 );
HRblock = imageH( ( ii - 1 ) * scale + offset + 1 : ii* scale + offset,...
( jj - 1 )*scale+offset+1 : jj*scale+offset);
LR(pointer,:)=reshape(LRblock,[1,16]);
HR(pointer,:)=reshape(HRblock,[1,scale*scale]);
pointer = pointer+1;
end
end
display(['file ' num2str(i)]);
end
lr{1,1} = LR;
hr{1,1} = HR;
H_15 = [2 8 3 12 10 1 4 11 14 6 9 15 7 13 5];
i=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n0 = ceil(size(LR,1)/32);
dt = zeros(5*n0,6); %Initialize memory space
lr1 = cell(n0,1); %store low resolution data
hr1 = cell(n0,1); %store high resolution data
ptr0 = 1; % the current line number of the parameter matrix
ptr1 = 1; % the current line number of the decision tree
ptr2 = 1; % the front line number of the decision tree
sz = size(lr,1); % the size of the current level of the tree
while(i<=15)
j = 1;
while(j<=sz)
if(size(lr{j,1},1)>=3*min_num)
[lr{3*(j-1)+1+sz,1},lr{3*(j-1)+2+sz,1},lr{3*(j-1)+3+sz,1},...
hr{3*(j-1)+1+sz,1},hr{3*(j-1)+2+sz,1},hr{3*(j-1)+3+sz,1},v1,v2]...
= Ternary_Tree( lr{j,1},hr{j,1},H_15(i),v,min_num);
if(size(lr{3*(j-1)+2+sz,1},1)==0)
lr{3*(j-1)+1+sz,1} =[];
hr{3*(j-1)+1+sz,1} =[];
% Leaf node
dt(ptr1,1) = 0;
dt(ptr1,2) = ptr0;
lr1{ptr0} = lr{j,1};
hr1{ptr0} = hr{j,1};
ptr0 = ptr0 + 1;
ptr1 = ptr1 + 1;
%ptr2 = ptr2 + 1;
else
dt(ptr1,1) = i;
dt(ptr1,2) = ptr2+1;
dt(ptr1,3) = ptr2+2;
dt(ptr1,4) = ptr2+3;
dt(ptr1,5) = v1;
dt(ptr1,6) = v2;
ptr1 = ptr1 + 1;
ptr2 = ptr2 + 3;
end
else
% Leaf node
dt(ptr1,1) = 0;
dt(ptr1,2) = ptr0;
lr1{ptr0} = lr{j,1};
hr1{ptr0} = hr{j,1};
ptr0 = ptr0 + 1;
ptr1 = ptr1 + 1;
end
j = j+1;
end
lr = lr(sz+1:end,1);
hr = hr(sz+1:end,1);
lr(cellfun(@isempty,lr)) = [];
hr(cellfun(@isempty,hr)) = [];
sz = size(lr,1);
display(['The ' num2str(i) 'th segmentation !']);
i = i+1;
end
j=1;
while(j<=sz)
% Leaf node
dt(ptr1,1) = 0;
dt(ptr1,2) = ptr0;
lr1{ptr0} = lr{j,1};
hr1{ptr0} = hr{j,1};
ptr0 = ptr0 + 1;
ptr1 = ptr1 + 1;
j=j+1;
end
lr1(cellfun(@isempty,lr1)) = [];
hr1(cellfun(@isempty,hr1)) = [];
dt = dt(1:ptr2,:);
% initialise as bicubic parameters
ptr0 = ptr0-1;
parameters = repmat(zeros(scale*scale,16), [1 1 ptr0]);
pointer = 0;
lambda = 0;
er_our = 0;
er_bic = 0;
len = scale * scale;
for i = 1:ptr0
LRD = lr1{ i, 1 };
HRD = hr1{ i, 1 };
M = lambda * eye( 16 );
M = M + LRD' * LRD ;
M = [ M ones( 16, 1 ); ones( 1, 16 ), 0 ];
B = LRD' * HRD;
B = [ B; ones( 1, scale * scale ) ];
para1 = M \ B;
para1 = para1( 1 : 16, : );
parameters(:,:,i)=para1';
end
toc(startt);
save(['parameters1\parameter_' num2str(scale)],'parameters');
save(['parameters1\dt_' num2str(scale)],'dt');