-
Notifications
You must be signed in to change notification settings - Fork 1
/
object_removal.m
62 lines (47 loc) · 1.3 KB
/
object_removal.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
clear all ;
im = imread('./images/dog/dog.jpg') ;
[M, N, chn] = size(im) ;
FM = M ; FN = N - 75;
OM = M ; ON = N ;
mask = zeros(M, N) ;
% for broadway_tower
% mask(720:800,100:147) = 1;
% for shore
% mask(115:165, 350:370) = 1;
% for dog
% mask(120:300, 170:230) = 1;
mask(120:220, 170:230) = 1;
% traverse until we get desired width
while N > FN
cost = get_cost_gradient(im) ;
cost(mask == 1) = -1e5 ;
dp = zeros(M, N) ;
from = zeros(M, N) ;
dp(1, :) = cost(1, :) ;
from(1, :) = 1 : N ;
for i = 2 : M
for j = 1 : N
dp(i, j) = dp(i - 1, j) ;
from(i, j) = j ;
if j > 1 && dp(i - 1, j - 1) < dp(i, j)
dp(i, j) = dp(i - 1, j - 1) ;
from(i, j) = j - 1 ;
end
if j < N && dp(i - 1, j + 1) < dp(i, j)
dp(i, j) = dp(i - 1, j + 1) ;
from(i, j) = j + 1 ;
end
dp(i, j) = dp(i, j) + cost(i, j) ;
end
end
[~, idx] = min(dp(M, :)) ;
for i = M : -1 : 1
im(i, idx : N - 1, :) = im(i, idx + 1 : N, :) ;
mask(i, idx : N - 1, :) = mask(i, idx + 1 : N, :) ;
idx = from(i, idx) ;
end
im = im(:, 1 : N - 1, :) ;
mask = mask(:, 1 : N - 1, :) ;
N = N - 1 ;
end
imshow(uint8(im));