-
Notifications
You must be signed in to change notification settings - Fork 3
/
cut.m
131 lines (104 loc) · 4.51 KB
/
cut.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
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
function imout = cut(sample, patchsize, overlap, imout, samplepos, imoutpos)
samplesz = size(sample);
sampledms = length(samplesz);
imout(imoutpos(1)+overlap:imoutpos(1)+patchsize-1, imoutpos(2)+overlap:imoutpos(2)+patchsize-1, :) = sample(samplepos(1)+overlap:samplepos(1)+patchsize-1, samplepos(2)+overlap:samplepos(2)+patchsize-1, :);
imoutsz = size(imout);
imoutdms = length(imoutsz);
samplet = zeros(patchsize, overlap);
imoutt = zeros(patchsize, overlap);
delta = zeros(patchsize, overlap);
if sampledms == 2
samplet(:, :) = sample(samplepos(1):samplepos(1)+patchsize-1, samplepos(2):samplepos(2)+overlap-1);
else
for k=1: 1: samplesz(3);
samplet(:, :) = samplet(:, :) + sample(samplepos(1):samplepos(1)+patchsize-1, samplepos(2):samplepos(2)+overlap-1, k);
end
end
if imoutdms == 2
imoutt(:, :) = imout(imoutpos(1):imoutpos(1)+patchsize-1, imoutpos(2):imoutpos(2)+overlap-1);
else
for p=1: 1: imoutsz(3);
imoutt(:, :) = imoutt(:, :) + imout(imoutpos(1):imoutpos(1)+patchsize-1, imoutpos(2):imoutpos(2)+overlap-1, p);
end
end
delta(:, :) = samplet(:, :) - imoutt(:, :);
delta(:, :) = abs(delta(:, :));
ddelta = zeros(patchsize, overlap);
ddelta(1, :) = delta(1, :);
for i=2: 1: patchsize;
for j=1: 1: overlap;
if j == 1
ddelta(i, 1) = delta(i, 1) + min([delta(i-1, 1) delta(i-1, 2)]);
elseif j == overlap
ddelta(i, overlap) = delta(i, overlap) + min([delta(i-1, overlap-1) delta(i-1, overlap)]);
else
ddelta(i, j) = delta(i, j) + min([delta(i-1, j-1) delta(i-1, j) delta(i-1, j+1)]);
end
end
end
cutarr = zeros(patchsize, 1);
cutarr(patchsize) = find(ddelta(patchsize, :)==min(ddelta(patchsize, :)), 1, 'first');
for i=patchsize: -1: 2;
if cutarr(i, 1) == 1
temp = [ddelta(i-1, 1) ddelta(i-1, 2)];
cutarr(i-1) = find(temp==min(temp), 1, 'first');
elseif cutarr(i) == overlap
temp = [ddelta(i-1, overlap) ddelta(i-1, overlap-1)];
cutarr(i-1) = overlap + 1 - find(temp==min(temp), 1, 'first');
else
temp = [ddelta(i-1, cutarr(i)-1) ddelta(i-1, cutarr(i)) ddelta(i-1, cutarr(i)+1)];
cutarr(i-1) = cutarr(i) - 2 + find(temp==min(temp), 1, 'first');
end
end
for i=0: 1: patchsize-1;
imout(imoutpos(1)+i, imoutpos(2)+cutarr(i+1)-1:imoutpos(2)+overlap-1, :) = sample(samplepos(1)+i, samplepos(2)+cutarr(i+1)-1:samplepos(2)+overlap-1, :);
end
samplet = zeros(overlap, patchsize);
imoutt = zeros(overlap, patchsize);
delta = zeros(overlap, patchsize);
if sampledms == 2
samplet(:, :) = sample(samplepos(1):samplepos(1)+overlap-1, samplepos(2):samplepos(2)+patchsize-1);
else
for k=1: 1: samplesz(3);
samplet(:, :) = samplet(:, :) + sample(samplepos(1):samplepos(1)+overlap-1, samplepos(2):samplepos(2)+patchsize-1, k);
end
end
if imoutdms == 2
imoutt(:, :) = imout(imoutpos(1):imoutpos(1)+overlap-1, imoutpos(2):imoutpos(2)+patchsize-1);
else
for p=1: 1: imoutsz(3);
imoutt(:, :) = imoutt(:, :) + imout(imoutpos(1):imoutpos(1)+overlap-1, imoutpos(2):imoutpos(2)+patchsize-1, p);
end
end
delta(:, :) = samplet(:, :) - imoutt(:, :);
delta(:, :) = abs(delta(:, :));
ddelta = zeros(overlap, patchsize);
ddelta(:, 1) = delta(:, 1);
for i=2: 1: patchsize;
for j=1: 1: overlap;
if j == 1
ddelta(1, i) = delta(1, i) + min([delta(1, i-1) delta(2, i-1)]);
elseif j == overlap
ddelta(overlap, i) = delta(overlap, i) + min([delta(overlap-1, i-1) delta(overlap, i-1)]);
else
ddelta(j, i) = delta(j, i) + min([delta(j-1, i-1) delta(j, i-1) delta(j+1, i-1)]);
end
end
end
cutarr = zeros(patchsize, 1);
cutarr(patchsize) = find(ddelta(:, patchsize)==min(ddelta(:, patchsize)), 1, 'first');
for i=patchsize: -1: 2;
if cutarr(i) == 1
temp = [ddelta(1, i-1) ddelta(2, i-1)];
cutarr(i-1) = find(temp==min(temp), 1, 'first');
elseif cutarr(i) == overlap
temp = [ddelta(overlap, i-1) ddelta(overlap-1, i-1)];
cutarr(i-1) = overlap + 1 - find(temp==min(temp), 1, 'first');
else
temp = [ddelta(cutarr(i)-1, i-1) ddelta(cutarr(i), i-1) ddelta(cutarr(i)+1, i-1)];
cutarr(i-1) = cutarr(i) - 2 + find(temp==min(temp), 1, 'first');
end
end
for i=0: 1: patchsize-1;
imout(imoutpos(1)+cutarr(i+1)-1:imoutpos(1)+overlap-1, imoutpos(2)+i, :) = sample(samplepos(1)+cutarr(i+1)-1:samplepos(1)+overlap-1, samplepos(2)+i, :);
end