from following code segmented image is not displayed? where is the problem

1 view (last 30 days)
rgb = imread('peppers.png'); %'D:\OLD L\HSV_segment\42049.jpg'
cform = makecform('srgb2lab');
lab = applycform(rgb,cform);
ab = lab(:,:,2:3);
nrows = size(ab,1);
ncols = size(ab,2);
F = reshape(ab,size(ab,1)*size(ab,2),2);
%F = reshape(ab,size(ab,1)*size(ab,2),3); % Color Features
%%K-means
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 = 5; % K-means Iteration
for n = 1:KMI
for i = 1:size(F,1)
for j = 1:K
DAL(i,j) = norm(double(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
idx = find(DAL(:,K+1) == i);
X(idx,:) = repmat(CENTS(i,:),size(idx,1),1);
end
T = reshape(X,size(ab,1)*size(ab,2),2);
%Z=double(T);
%%Show
figure()
subplot(121); imshow(rgb); title('original')
subplot(122); imshow(T); title('segmented')
disp('number of segments ='); disp(K)
  3 Comments

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!