-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_unary.m
47 lines (43 loc) · 1.25 KB
/
compute_unary.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
function [graph] = compute_unary(bgrnd, fgrnd, graph, source, sink , lambda, im_data)
%case 1
p = pfb(im_data, fgrnd, bgrnd);
graph(source,:) = [p+lambda 0 0];
graph(source,source) = 0;
graph(source,sink) = 0;
%case 2
graph(source,fgrnd) = inf;
%case 3
graph(:, sink) = 0;
%case 4
graph(bgrnd,sink) = inf;
end
function [p] = pfb(im_data, fg, bg)
red = double(im_data(:,:,1));
blue = double(im_data(:,:,2));
green = double(im_data(:,:,3));
img = [ red(:) blue(:) green(:)];
fg_img = [red(fg(:)) blue(fg(:)) green(fg(:))];
bg_img = [red(bg(:)) blue(bg(:)) green(bg(:))];
% fg computation
fg_mean = mean(fg_img);
bg_mean = mean(bg_img);
fg_cov = cov(fg_img);
bg_cov = cov(bg_img);
if any(fg_cov)
fg_inv = inv(fg_cov);
else
fg_inv = fg_cov;
end
if any(bg_cov)
bg_inv = inv(bg_cov);
else
bg_inv = bg_cov;
end
p = zeros(1,size(img,1));
for i = 1:size(img,1)
lnf = -(log(det(fg_cov))/2) - ((1/2)*(img(i,:) - fg_mean)*fg_inv*(img(i,:)-fg_mean)');
lnb = -(log(det(bg_cov))/2) - ((1/2)*(img(i,:) - bg_mean)*bg_inv*(img(i,:)-bg_mean)');
prob = lnf - lnb;
p(i) = mean(prob(:));
end
end