please in details can anyone explain this code of K means Segmentation?

조회 수: 2 (최근 30일)
Touhidul islam
Touhidul islam 2017년 12월 28일
편집: Touhidul islam 2018년 1월 9일
clear all
close all
[filename,pathname] = uigetfile({'*.*';'*.bmp';'*.tif';'*.gif';'*.png'},'Pick an Image File');
I = im2double(imread([pathname,filename]));
[rows, columns, numberOfColorChannels] = size(I);
F = reshape(I, rows*columns, numberOfColorChannels);
******* From here please explain
K = 3; % Cluster Numbers
CENTS = F( ceil(rand(K,1)*size(F,1)) ,:); % Cluster Centers
DAL = zeros(size(F,1),K+2); % Distances and Labels
KMI = 50; % K-means Iteration
for n = 1:KMI
for i = 1:size(F,1)
for j = 1:K
DAL(i,j) = norm(F(i,:) - CENTS(j,:));
end
[Distance, CN] = min(DAL(i,1:K)); % 1:K are Distance from Cluster Centers 1:K
DAL(i,K+1) = CN; % K+1 is Cluster Label
DAL(i,K+2) = Distance; % K+2 is Minimum Distance
end
for i = 1:K
A = (DAL(:,K+1) == i); % Cluster K Points
CENTS(i,:) = mean(F(A,:)); % New Cluster Centers
if sum(isnan(CENTS(:))) ~= 0 % If CENTS(i,:) Is Nan Then Replace It With Random Point
NC = find(isnan(CENTS(:,1)) == 1); % Find Nan Centers
for Ind = 1:size(NC,1)
CENTS(NC(Ind),:) = F(randi(size(F,1)),:);
end
end
end
end
X = zeros(size(F));
for i = 1:K
end idx = find(DAL(:,K+1) == i);
X(idx,:) = repmat(CENTS(i,:),size(idx,1),1);
end
  댓글 수: 2
Image Analyst
Image Analyst 2017년 12월 28일
편집: Image Analyst 2017년 12월 28일
Please read this and fix your formatting.
Next, read this link and then attach your image file.\
In the meantime, is the author answering your questions?
Touhidul islam
Touhidul islam 2017년 12월 28일
sir , i have done all the necessary things you had asked me to do.

댓글을 달려면 로그인하십시오.

답변 (1개)

Bernhard Suhm
Bernhard Suhm 2018년 1월 5일
You need to provide some more context, what are you trying to accomplish? Right now, this feels like a coding puzzle. By my interpretation, this code clusters all the pixes of the input image into 3 clusters (running k-means KMI times), and then replaces each pixel by its cluster.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by