Home > other >  Fuzzy c-means segmentation MATLAB code
Fuzzy c-means segmentation MATLAB code

Time:09-18


 % into gray image, a fuzzy c-means segmentation 
close all;
clear all;
clc;
data=https://bbs.csdn.net/topics/imread (' 4. JPG);
[m, n, r]=size (data);
If r> 1
Data1=rgb2gray (data);
The else
Data1=data;
End
% data=https://bbs.csdn.net/topics/imnoise (data, "gaussian", 0,0.02);
% data=https://bbs.csdn.net/topics/imnoise (data, 'salt & amp; Pepper ', 0.05);
Data1=imresize (data1, [256] 256);
[m, n]=size (data1);
figure(1);
% subplot (1, 2, 1); % figure in 1 row 2 column, the picture in the figure on the first picture
Imshow (data1);
The title (' original image);
Data1=double (data1);


% determine parameters% c=3;
C=2;
MC=2.0;
E=0.01; % accuracy
Ct=0; % iteration times ct

% initialization distanceD=zeros (m, n, c);
% initialization membership
U=zeros (m, n, c);
V1 (1)=75;
% v1 (2)=180;
V1 (2)=260;
While e> 0.0001 & amp; & Ct<1000% loop condition
V=v1;

% to calculate distance: sample data (I, j) the distance to the first class k
For k=1: c
For I=1: m
For j=1: n
D (I, j, k)=(data1 (I, j) - v1 (k)) ^ 2 + 0.0001;
End
End
End

% to calculate membership
For j=1: n
For I=1: m
Tp1=0.0;
For k=1: c
Tp1=tp1 + (1/d (I, j, k)) ^ (1/(MC - 1));
End
For k=1: c
U (I, j, k)=(1/d (I, j, k)) ^ (1/(MC - 1))/tp1.
End
End
End

% update clustering center
For k=1: c
Tp1=0.0;
Tp2=0.0;
For j=1: n
For I=1: m
Tp1=tp1 + u (I, j, k) ^ MC * data1 (I, j);
Tp2=tp2 + u (I, j, k) ^ MC;
End
End
V1 (k)=tp1/tp2. Clustering center %
End

% termination conditions
Temp=0.0;
For k=1: c
Temp=temp + (v (k) - v1 (k)) ^ 2;
End
If temp<0.0001
E=0.0001;
End
Ct=ct + 1;
End


% clusteringI=zeros (m, n);
% for I=1: m
% for j=1: n
% if u (I, j, 1) & gt; U (I, j, 2) & amp; & U (I, j, 1) & gt; U (I, j, 3)
% I (I, j)=0;
% the else
% if u (I, j, 2) & gt; U (I, j, 3) & amp; & U (I, j, 2) & gt; U (I, j, 1)
% I (I, j)=128;
% the else
% I (I, j)=255;
% end
% end
% end
% end
For I=1: m
For j=1: n
If u (I, j, 1) & gt; U (I, j, 2)
I (I, j)=0;
The else
I (I, j)=255;
End
End
End

Figure (2);
% % subplot (1,2,2) figure in row 2 column 2, the picture in the figure on the first picture
Imshow (uint8 (I));
Title (' segmentation image)
% f=getimage (figure (2));
% imwrite (f, 'D: \ \ picture picture \ 1. PNG');

% tp1=0.0;
% for I=1: m
% for j=1: n
% for k=1: c
Tp1=tp1 + % u (I, j, k) * log (u (I, j, k));
% end
% end
% end
% V=tp1/(m * n);

CodePudding user response:

Who can explain
  • Related