can someone help me to fix this error(Error using ==> kmeans at 382 An empty cluster error occurred in every replicate)

1 view (last 30 days)
??? Error using ==> kmeans at 382 An empty cluster error occurred in every replicate.
Error in ==> segmentizeImage1 at 25 [cluster_idx cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
Error in ==> bui_simple>start_snap_Callback at 108 segmentizeImage1;
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> bui_simple at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)bui_simple('start_snap_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
REFER TO THIS CODE BELOw:
x = wa; % this is where the image get from snapshot on video
rawImage = x;
I = imresize(rawImage,0.50);
cform = makecform('srgb2lab');
J = applycform(I,cform);
ab = double(J(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 2;
[cluster_idx cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',2);
pixel_labels = reshape(cluster_idx,nrows,ncols);
segmented_images = cell(1,2);
rgb_label = repmat(pixel_labels,[1 1 3]);
for k = 1:nColors
color = I;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
mean_cluster_value = mean(cluster_center,2);
[tmp, idx] = sort(mean_cluster_value);
hand_cluster_num = idx(2);
K = segmented_images{hand_cluster_num};
L = im2bw(segmented_images{hand_cluster_num},0.001);
s = regionprops(rgb_label, I, {'Centroid','BoundingBox'});
% s(hand_cluster_num).Centroid
% s(hand_cluster_num).BoundingBox
centroid_x = uint8(s(hand_cluster_num).Centroid(1));
centroid_y = uint8(s(hand_cluster_num).Centroid(2));
cropped_x = uint8(s(hand_cluster_num).BoundingBox(1))+1;
cropped_y = uint8(s(hand_cluster_num).BoundingBox(2))+1;
cropped_w = uint8(s(hand_cluster_num).BoundingBox(4))-2;
cropped_h = uint8(s(hand_cluster_num).BoundingBox(5))-2;
% apply gaussian blur to remove noise
PSF = fspecial('gaussian',7,10);
L = imfilter(L,PSF,'symmetric','conv');
% cropped region of interest from main image
M = L( cropped_y : (cropped_y+cropped_h), cropped_x: (cropped_x+cropped_w) );
% trim leading and leading zeroes
M(:, logical(sum(abs(M)) == 0)) = [];
cropped_h = size(M,1);
cropped_w = size(M,2);
non_zeroes = find(sum(abs(L))~=0);
cropped_x = non_zeroes(1);
% fill holes to remove flares
M = imfill(M, 'holes');
% get a shrunk sample from cropped image
N = imresize(M, 0.25);
[sample_height sample_width] = size(N);
% clip excess pixels
if(sample_height>50)
N = N(1:50,:);
end
if(sample_width>50)
N = N(:,1:50);
end
[sample_height sample_width] = size(N);
%pad both sides
htopad = double(25 - ceil(sample_height/2));
wtopad = double(25 - ceil(sample_width/2));
if((htopad>=0)&&(wtopad>=0))
N = padarray(N,[htopad wtopad]);
%for uneven padding, add padding to end
[sample_height sample_width] = size(N);
htopad = 50 - sample_height;
wtopad = 50 - sample_width;
N = padarray(N,[htopad wtopad],0,'post');
else
disp('Padding not performed...');
end
imshow(M);
%hold on;
% plot(centroid_x, centroid_y, 'bo');
% rectangle('Position', [ cropped_x cropped_y cropped_w cropped_h ], 'EdgeColor','y');
% hold off;
y = N;
z = imresize(y, [40 30]);
z = z(:);
  2 Comments
john john
john john on 15 Feb 2012
the code is working, it can captures and recognize data, the problem later on it will goes into that error, can some1 explain it to me why it give's error? is it bug on kmeans.m on toolbox?
R
R on 6 Jan 2014
did get solution to this problem of...An empty cluster error occurred in every replicate. in kmeans algorithm.

Sign in to comment.

Accepted Answer

Tom Lane
Tom Lane on 15 Feb 2012
Type "help kmeans" and look at the description of the 'EmptyAction' parameter. Maybe that will help.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!