-
Notifications
You must be signed in to change notification settings - Fork 5
/
DemoFCM.m
54 lines (46 loc) · 1.49 KB
/
DemoFCM.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
function DemoFCM
% Segment a sample 2D image into 3 classes using fuzzy c-means algorithm.
% Note that similar syntax would be used for c-means based segmentation,
% except there would be no fuzzy membership maps (denoted by variable U
% below).
im=imread('cameraman.tif'); % sample image
[C,U,LUT,H]=FastFCMeans(im,3); % perform segmentation
% Visualize the fuzzy membership functions
figure('color','w')
subplot(2,1,1)
I=double(min(im(:)):max(im(:)));
c={'-r' '-g' '-b'};
for i=1:3
plot(I(:),U(:,i),c{i},'LineWidth',2)
if i==1, hold on; end
plot(C(i)*ones(1,2),[0 1],'--k')
end
xlabel('Intensity Value','FontSize',30)
ylabel('Class Memberships','FontSize',30)
set(gca,'XLim',[0 260],'FontSize',20)
subplot(2,1,2)
plot(I(:),LUT(:),'-k','LineWidth',2)
xlabel('Intensity Value','FontSize',30)
ylabel('Class Assignment','FontSize',30)
set(gca,'XLim',[0 260],'Ylim',[0 3.1],'YTick',1:3,'FontSize',20)
% Visualize the segmentation
figure('color','w')
subplot(1,2,1), imshow(im)
set(get(gca,'Title'),'String','ORIGINAL')
L=LUT2label(im,LUT);
Lrgb=zeros([numel(L) 3],'uint8');
for i=1:3
Lrgb(L(:)==i,i)=255;
end
Lrgb=reshape(Lrgb,[size(im) 3]);
subplot(1,2,2), imshow(Lrgb,[])
set(get(gca,'Title'),'String','FUZZY C-MEANS (C=3)')
% If necessary, you can also unpack the membership functions to produce
% membership maps
Umap=FM2map(im,U,H);
figure('color','w')
for i=1:3
subplot(1,3,i), imshow(Umap(:,:,i))
ttl=sprintf('Class %d membership map',i);
set(get(gca,'Title'),'String',ttl)
end