Clear Filters
Clear Filters

I want to segment my leaf image with 4 clusters. but shows the error: Attempt to grow array along ambiguous dimension. Error in Detect (line 46) colors(rgb_label ~= k) = 0;

2 views (last 30 days)
here's my code:
clc
close all
clear all
[filename, pathname] = uigetfile({'*.*';'*.bmp';'*.jpg';'*.gif'}, 'Pick a Leaf Image File');
I = imread([pathname,filename]);
I = imresize(I,[256,256]);
figure, imshow(I);
title('Query Leaf Image');
I = imadjust(I,stretchlim(I));
I_Otsu = im2bw(I,graythresh(I));
I_HIS = rgb2hsi(I);
cform = makecform('srgb2lab');
lab_he = applycform(I,cform);
ab = double(lab_he(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
num=4;
nColors = num;
[cluster_idx cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',4);
pixel_labels = reshape(cluster_idx,nrows,ncols);
segmented_images = cell(1,nColors);
% Create RGB label using pixel_labels
rgb_label = repmat(pixel_labels,[1,1,nColors]);
for k = 1:nColors
colors = I;
colors(rgb_label ~= k) = 0;
segmented_images{k} = colors;
end
figure, subplot(4,1,1);
imshow(segmented_images{1});
title('Cluster 1');
subplot(4,1,2);
imshow(segmented_images{2});
title('Cluster 2');
subplot(4,1,3);
imshow(segmented_images{3});
title('Cluster 3');
subplot(4,1,4);
imshow(segmented_images{4});
title('Cluster 4');

Answers (0)

Categories

Find more on Agriculture in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!