-
Notifications
You must be signed in to change notification settings - Fork 2
/
HMRF_MCEM.m
45 lines (41 loc) · 2.1 KB
/
HMRF_MCEM.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
%% Àëãîðèòì ïîñëåäîâàòåëüíîé îöåíêè ïàðàìåòðîâ ïîñðåäñòâîì ãåíåðàöèè âûáîðêè èç ñõåìû Ãèááñà
%---input---------------------------------------------------------
% data: èñõîäíûå äàííûå, ìàòðèöà NxP, N - êîëè÷åñòâî ïèêñåëåé
% dim: ðàçìåð ìàòðèöû ôèíàëüíîé ñåãìåíòàöèè
% k: êîëè÷åñòâî ìåòîê
% beta: íà÷àëüíîå çíà÷åíèå ïàðàìåòðà ìîäåëè Ïîòòñà, ñêàëÿð
% mus: íà÷àëüíîå çíà÷åíèå ïàðàìåòðà vMF, ìàòðèöà LxP
% kappas: íà÷àëüíîå çíà÷åíèå ïàðàìåòðà vMF, âåêòîð Lx1
% burn_in: êîëè÷åñòâî îòáðàñûâàåìûõ çâåíüåâ èç ñõåìû Ãèááñà
% sample_num: êîëè÷åñòâî çâåíüåâ èñïîëüçóåìûõ äëÿ îöåíêè ïàðàìåòðîâ
% max_iter: ìàêñèìàëüíîå êîëè÷åñòâî èòåðàöèé
% neighbours_count: êîëè÷åñòâî ñîñåäåé, äîñòóïíûå çíà÷åíèÿ
% 2-D: 4, 8, 16
% 3-D: 6, 26
% mask: ìàñêà
%---output--------------------------------------------------------
% sample: ôèíàëüíàÿ ñåãìåíòàöèÿ íà îñíîâå îöåíåííûõ ïàðàìåòðîâ
% sample2: ôèíàëüíàÿ ñåãìåíòàöèÿ ïîñëå ïðèìåíåíèÿ ê íåé GraphCut
% beta: îöåíåííîå çíà÷åíèå beta
% mus: îöåíåííîå çíà÷åíèå mus
% kappas: îöåíåííîå çíà÷åíèå kappas
function [sample, beta, mus, kappas, sample2] = HMRF_MCEM(data, segment_init, k, beta, mus, kappas, burn_in, sample_num, max_iter, neighbours_count, mask)
p = size(data, 2);
if(nargin < 11)
mask = ones(size(segment_init));
end
segment_init(mask==0) = 0;
for i=1:max_iter
fprintf('\tHRMF MCEM Iteration: %d of %d\n',i,max_iter);
% ãåíåðèðóåì âûáîðêó èç ñõåìû Ãèááñà
[~, logprobs] = CalculateLikelihoodProbabilities(data, k, kappas, mus, mask);
[samples] = GibbsSamplerLabelCost(segment_init, burn_in, sample_num, k, beta, logprobs, neighbours_count);
% ïîäñòðàèâàåì ïàðàìåòðû
[beta, mus, kappas] = EstimateParametersHMRFMCEM(data, samples, k, p, beta, mus, kappas);
% ãåíåðèðóåì íà÷àëüíóþ êîíôèãóðàöèþ
segment_init = reshape(samples(end, :), size(segment_init));
end
% Find MAP
sample = reshape(samples(end, :), size(segment_init));
[~, logprobs] = CalculateLikelihoodProbabilities(data, k, kappas, mus, sample);
sample2 = MRF_MAP_GraphCutAExpansion(sample, logprobs, beta, k, max_iter, neighbours_count);