-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.m
executable file
·58 lines (51 loc) · 1.55 KB
/
test.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
%% MyTest
clc;
clear;
%
%%
%
%
%产生待分割图像,大小为64*64
S = [64 64];
Miu = [90,150,190];Sig = [20, 20, 20];
Im = zeros(S(1),S(2));
Im(1:21,:) = random('norm',Miu(1),Sig(1),[21,64]);
Im(22:42,:) = random('norm',Miu(2),Sig(2),[21,64]);
Im(43:64,:) = random('norm',Miu(3),Sig(3),[22,64]);
figure,imshow(Im,[]);title('待分割图像');
Image=reshape(Im,S(1)*S(2),1);
topicNum=3;
temp = eye(topicNum);
temp(temp==0) = 2;
temp(temp==1) = 0;
smooth = temp;
dataCost=zeros(3,S(1)*S(2));
for i=1:S(1)*S(2)
Pyx=normpdf(Image(i),Miu,Sig);
dataCost(:,i) = -log( Pyx./sum(Pyx) );
end
Neighbors=zeros(S(1)*S(2),S(1)*S(2));
for i=1:S(1)*S(2)
if(i+1<=S(1)*S(2))
Neighbors(i,i+1)=1;
end
if(i-1>=1)
Neighbors(i,i-1)=1;
end
if(i+64<=S(1)*S(2))
Neighbors(i,i+64)=1;
end
if(i-64>=1)
Neighbors(i,i-64)=1;
end
end
h = GCO_Create(S(1)*S(2),3); % Create new object with NumSites=4, NumLabels=3
GCO_SetDataCost(h,dataCost); % Site 3 prefers label 3
GCO_SetSmoothCost(h,temp); %
GCO_SetNeighbors(h,Neighbors);
GCO_Expansion(h); % Compute optimal labeling via alpha-expansion
Label=GCO_GetLabeling(h);
[E D Smo] = GCO_ComputeEnergy(h); % Energy = Data Energy + Smooth Energy
GCO_Delete(h); % Delete the GCoptimization object when finished
labels=reshape(Label,S(1),S(2));
figure;imshow(labels,[]);