-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUpdateOmega.m
136 lines (114 loc) · 3.34 KB
/
UpdateOmega.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
132
133
134
function [ret] = UpdateOmega(Omega, E, bet, gam)
% Omega should be either 1 or -1
% E is in the range between 0~255
% bet is beta
% gam is gammma
% -----------------------------------
% Clarify the parameters :
% This program || In Paper
% 1 || -1
% 2 || 1
%
% SetUnary will treat nth row as label n
% SetPairwise have a different notation which uses 0,1 system, in
% which 0 means 1 (cut btw. source) and 1 means 2 (cut btw. sink).
addpath Bk_Lib
[m, n] = size(Omega); % Get the size of the image
% Create graph
Graph = BK_Create(m*n); % Create a undirected graph with n*m vertices
% Set Unary weight
[U1, U_1] = computeU(Omega, E, bet);
BK_SetUnary(Graph, [U_1' ; U1']);
% Set P
BK_SetPairwise(Graph, computeP(Omega, gam));
% Calculate the mincut
BK_Minimize(Graph) % ret should be a 1-D vector
label = BK_GetLabeling(Graph);
ret = (label == 1) .* -1 + (label == 2);
ret = reshape(ret, [n m])';
BK_Delete(Graph);
%% ComputeU will return the U matrix
% U1 indicates that the pixel is marked 1
% U_1 means the pixel is marked -1
function [U1, U_1] = computeU(Omega, E, bet)
% Vectorize matrices
Omega = Omega';
E = E';
Omega = Omega(:);
E = E(:);
bet = bet * ones(length(E), 1);
U1 = (abs(E) <= bet) .* (log(bet)); % Omega == 1
U_1 = (abs(E) <= bet) .* (-log(bet)) + (abs(E) > bet) .* (log(bet));
%% ComputeP. This function should return a edge# x 6 matrix
function [P] = computeP(Omega, gam)
[m, n] = size(Omega);
edgenum = (m - 1) * n + (n - 1) * m; % number of edges
P = zeros(edgenum, 6);
Omega = Omega(:);
ct2 = 1;
for count = 1 : m*n
col = floor(count/m) + 1;
row = mod(count, m);
bottom = (mod(count, m) == 0);
right = ((n*m - count) < m);
if(right && ~bottom)
P(ct2, :) = [count count+1 gam -gam -gam gam];
ct2 = ct2 + 1;
else
if(~right && bottom)
P(ct2, :) = [count, count+m gam -gam -gam gam];
ct2 = ct2 + 1;
else
if(~right && ~bottom)
P(ct2, :) = [count count+1 gam -gam -gam gam];
P(ct2+1, :) = [count count+m gam -gam -gam gam];
ct2 = ct2 + 2;
end
=======
addpath Bk_matlab;
[m, n] = size(Omega); % Get the size of the image
% Create graph
Graph = Bk_Create(m*n); % Create a undirected graph with n*m vertices
% Set Unary weight
[U1, U_1] = computeU(Omega, E, bet);
Bk_SetUary(Graph, [U_1' ; U1']);
% Set P
Bk_setNeighbors(Graph, computeP(Omega, gam));
% Calculate the mincut
ret = Bk_Minimize(Graph); % ret should be a 1-D vector
ret = reshape(ret, [n, m])';
function [U1, U_1] = computeU(Omega, E, bet)
% Vectorize matrices
Omega = Omega(:);
E = E(:);
bet = bet * ones(length(E), 1);
U1 = (abs(E) <= bet & Omega == 1) * (log10(bet));
U_1 = (abs(E) <= bet & Omega == -1) * (-log10(bet)) + ...
(abs(E) > bet & Omega == -1) * (log10(bet));
function [P] = computeP(Omega, gam)
[m, n] = size(Omega);
P = zeros(m*n, m*n);
Omega = Omega(:);
for count = 1 : m*n
row = floor(count/n) + 1;
col = mod(count, n);
right = (mod(count, n) == 0);
bottom = (count-(n*(m-1)) <= n);
if(right && ~bottom)
P(row, col+n) = gam * Omega(count) * Omega(count+n);
else
if(~right && bottom)
P(row, col+1) = gam * Omega(count) * Omega(count+1);
else
P(row, col+1) = gam * Omega(count) * Omega(count+1);
P(row, col+n) = gam * Omega(count) * Omega(count+n);
>>>>>>> origin/master
end
end
end
<<<<<<< HEAD
if(ct2 ~= edgenum+1)
error('ct2 and edgenum mistach');
end
=======
>>>>>>> origin/master