forked from mathisonian/eecs442-finalproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphCut.m
62 lines (51 loc) · 1.61 KB
/
graphCut.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
function [ cutMask ] = graphCut( adjMatrix, contextMap )
labels = zeros(1, size(adjMatrix,1) - 2);
% Loop over the original and hole rows,
% and the non-zero's should be labeled appropriately.
%
% for
%
% end
% Remove the sink/source info
adjMatrix = adjMatrix(1:end-2,1:end-2);
normalized = adjMatrix / sum(adjMatrix(:));
w = size(adjMatrix,1);
h = size(adjMatrix,2);
contractPointers = cell(1,w);
while max(normalized(:)) < 1
normalized = normalized / sum(normalized(:));
r = rand(1);
curPosition = 0;
contracted = 0;
for i = 1:w
for j = 1:h
curPosition = curPosition + normalized(i,j);
if r < curPosition
normalized(i,:) = normalized(i,:) + normalized(j,:);
normalized(:,i) = normalized(:,i) + normalized(:,j);
normalized(:,j) = zeros(w,1);
normalized(j,:) = zeros(1,h);
normalized(i,i) = 0;
contracted = 1;
break;
end
end
if contracted == 1
break;
end
end
end
% return a mask of shit that is in the new / old image
[r,c] = find(contextMap);
cutMask = zeros(size(contextMap));
cut=0;
for i = 1:w
cur = contractPointers{i};
if(size(cur,1) > 0)
cut = cut + 1;
end
for pointer = cur
cutMask(r(pointer), c(pointer)) = cut;
end
end
end