-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateK.m
46 lines (43 loc) · 1.42 KB
/
updateK.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
function [k] = updateK(im_data_vectorized, alpha, alphaVal, bbox_vectorized, numK, pi, mu, sigma, init, rng_seed )
% disp('step 1: finding k');
if init
k = initK(im_data_vectorized, alpha, alphaVal, bbox_vectorized, numK, rng_seed);
else
D = computeD(im_data_vectorized, numK, pi, mu, sigma);
[~, kUpdated] = min(D);
k = kUpdated;
end
% disp('step 1 done!');
end
function k = initK(im_data_vectorized, alpha, alphaVal, bbox_vectorized, numK, rng_seed) % D is [k x numpixels]
initGlobalVariables;
rng(rng_seed);
if (strcmp(initType, 'random'))
k = randi(numK, [1 size(im_data_vectorized, 2)]);
elseif (strcmp(initType, 'kmeans'))
%if (allowCache)
% try
% load([kCacheFile]);
% return
% catch
% end
%end
% disp('initializing k using kmeans');
numPixels = size(im_data_vectorized, 2);
[kFg, ~] = kMeans(im_data_vectorized(:,bbox_vectorized==1)', numK); % k is [1 x numpixel]
[kBg, ~] = kMeans(im_data_vectorized(:,bbox_vectorized==0)', numK); % k is [1 x numpixel]
k = zeros(1, numPixels);
fgIdx = 1;
bgIdx = 1;
for i=1:numPixels
if (alpha(i) == fg_val)
k(i) = kFg(fgIdx);
fgIdx = fgIdx+1;
elseif (alpha(i) == bg_val)
k(i) = kBg(bgIdx);
bgIdx = bgIdx+1;
end
end
% save(kCacheFile, 'k');
end
end