|
a |
|
b/libraries/lib_FastCMeans/DemoFCM.m |
|
|
1 |
function DemoFCM |
|
|
2 |
% Segment a sample 2D image into 3 classes using fuzzy c-means algorithm. |
|
|
3 |
% Note that similar syntax would be used for c-means based segmentation, |
|
|
4 |
% except there would be no fuzzy membership maps (denoted by variable U |
|
|
5 |
% below). |
|
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
im=imread('001_1.tif'); % sample image |
|
|
10 |
[C,U,LUT,H]=FastFCMeans(im,3); % perform segmentation |
|
|
11 |
|
|
|
12 |
% Visualize the fuzzy membership functions |
|
|
13 |
figure('color','w') |
|
|
14 |
subplot(2,1,1) |
|
|
15 |
I=double(min(im(:)):max(im(:))); |
|
|
16 |
c={'-r' '-g' '-b'}; |
|
|
17 |
for i=1:3 |
|
|
18 |
plot(I(:),U(:,i),c{i},'LineWidth',2) |
|
|
19 |
if i==1, hold on; end |
|
|
20 |
plot(C(i)*ones(1,2),[0 1],'--k') |
|
|
21 |
end |
|
|
22 |
xlabel('Intensity Value','FontSize',30) |
|
|
23 |
ylabel('Class Memberships','FontSize',30) |
|
|
24 |
set(gca,'XLim',[0 260],'FontSize',20) |
|
|
25 |
|
|
|
26 |
subplot(2,1,2) |
|
|
27 |
plot(I(:),LUT(:),'-k','LineWidth',2) |
|
|
28 |
xlabel('Intensity Value','FontSize',30) |
|
|
29 |
ylabel('Class Assignment','FontSize',30) |
|
|
30 |
set(gca,'XLim',[0 260],'Ylim',[0 3.1],'YTick',1:3,'FontSize',20) |
|
|
31 |
|
|
|
32 |
% Visualize the segmentation |
|
|
33 |
figure('color','w') |
|
|
34 |
subplot(1,2,1), imshow(im) |
|
|
35 |
set(get(gca,'Title'),'String','ORIGINAL') |
|
|
36 |
|
|
|
37 |
L=LUT2label(im,LUT); |
|
|
38 |
Lrgb=zeros([numel(L) 3],'uint8'); |
|
|
39 |
for i=1:3 |
|
|
40 |
Lrgb(L(:)==i,i)=255; |
|
|
41 |
end |
|
|
42 |
Lrgb=reshape(Lrgb,[size(im) 3]); |
|
|
43 |
|
|
|
44 |
subplot(1,2,2), imshow(Lrgb,[]) |
|
|
45 |
set(get(gca,'Title'),'String','FUZZY C-MEANS (C=3)') |
|
|
46 |
|
|
|
47 |
% If necessary, you can also unpack the membership functions to produce |
|
|
48 |
% membership maps |
|
|
49 |
Umap=FM2map(im,U,H); |
|
|
50 |
figure('color','w') |
|
|
51 |
for i=1:3 |
|
|
52 |
subplot(1,3,i), imshow(Umap(:,:,i)) |
|
|
53 |
ttl=sprintf('Class %d membership map',i); |
|
|
54 |
set(get(gca,'Title'),'String',ttl) |
|
|
55 |
end |
|
|
56 |
|