필터 지우기
필터 지우기

I have a Fuzzy-C means function, is there a pro to explain it to me?

조회 수: 1 (최근 30일)
Diboun Mohsen Djawad
Diboun Mohsen Djawad 2021년 9월 21일
function [ Unow, center, now_obj_fcn ] = FCMforImage( img, clusterNum)
% demo
% img = double(imread('brain.tif'));
% clusterNum = 3;
% [ Unow, center, now_obj_fcn ] = FCMforImage( img, clusterNum );
% figure;
% subplot(2,2,1); imshow(img,[]);
% for i=1:clusterNum
% subplot(2,2,i+1);
% imshow(Unow(:,:,i),[]);
% end
if nargin < 2
clusterNum = 8; % number of cluster
end
[row, col] = size(img);
expoNum = 2; % fuzzification parameter
epsilon = 0.001; % stopping condition
mat_iter = 100; % number of maximun iteration
rng('default');
Upre = rand(row, col, clusterNum);
dep_sum = sum(Upre, 3);
dep_sum = repmat(dep_sum, [1,1, clusterNum]);
Upre = Upre./dep_sum;
center = zeros(clusterNum,1);
for i=1:clusterNum
center(i,1) = sum(sum(Upre(:,:,i).*img))/sum(sum(Upre(:,:,i)));
end
pre_obj_fcn = 0;
for i=1:clusterNum
pre_obj_fcn = pre_obj_fcn + sum(sum((Upre(:,:,i) .*img - center(i)).^2));
end
fprintf('Initial objective fcn = %f\n', pre_obj_fcn);
for iter = 1:mat_iter
Unow = zeros(size(Upre));
for i=1:row
for j=1:col
for uII = 1:clusterNum
tmp = 0;
for uJJ = 1:clusterNum
disUp = abs(img(i,j) - center(uII));
disDn = abs(img(i,j) - center(uJJ));
tmp = tmp + (disUp/disDn).^(2/(expoNum-1));
end
Unow(i,j, uII) = 1/(tmp);
end
end
end
now_obj_fcn = 0;
for i=1:clusterNum
now_obj_fcn = now_obj_fcn + sum(sum((Unow(:,:,i) .*img - center(i)).^2));
end
fprintf('Iter = %d, Objective = %f\n', iter, now_obj_fcn);
if max(max(max(abs(Unow-Upre))))<epsilon || abs(now_obj_fcn - pre_obj_fcn)<epsilon
break;
else
Upre = Unow.^expoNum;
for i=1:clusterNum
center(i,1) = sum(sum(Upre(:,:,i).*img))/sum(sum(Upre(:,:,i)));
end
pre_obj_fcn = now_obj_fcn;
end
end

답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by