MATLAB Answers

segmentation using k-means clustering

12 views (last 30 days)
Keerthi  D
Keerthi D on 2 Oct 2020
Commented: Keerthi D on 12 Oct 2020
sir ,i'm doing my project on leaf disease detection and classification using svm.so i have a set of 1500 images.The first step was the background elimination using freehand and L*a*b color conversion.I completed that two process.Next step was segmentation and rule formation.The background eliminated resized RGB image is converted into L*a*b first,then the segmentation module is executed on 'a*b' channel.A well known k-means clustering algorithm is utilised to separate infected and healthy leaf regions. The iterative k-means reassign each pixel to nearest cluster so as to decrease the sum distances and recalculate cluster centroids.here i am using euclidean distance measurement.This process results in an input image partitioned into three regions,each containing different portions of a leaf image sample .Index values corresponding to the three clusters are used to label image pixels in the original image.Grey level values of the resultant three colour clusters are then utilised for further processing.
I 'm just tried to code the segmentation part.but i am not sure is it correct or not. And also i want the entire background of cluster1 is white and background of cluster2 and cluster3 are black.but i didn't get it.can you please help me to solve these problems.
code:
fontSize=10;
location='C:\Users\Keerthi Dev\Desktop\50datasample\original';%folder in which your images exists
ds=imageDatastore(location); %create datastore for all images in your folder
new_folder='C:\Users\Keerthi Dev\Desktop\50datasample\after_resized'; %new folder
k=1;
while hasdata(ds)
img=read(ds); %read image from datastore
scale=imresize(img,[256 256]);
fileName=sprintf('image_%d.jpg',k);
fullFileName=fullfile(new_folder,fileName);
imwrite(scale,fullFileName);
%size(scale)
k=k+1;
%lab conversion
labImage = rgb2lab(scale);
ab = labImage(:,:,2:3);
ab = im2single(ab);
%segmentation
nColors = 3; %repeat the clustering 3 times to avoid local minima
pixel_labels = imsegkmeans(ab,nColors,'NumAttempts',3);
%imshow(pixel_labels,[])
%title('Image labeled by cluster Index');
figure;
subplot(1,4,1)
imshow(scale);
title('leaf image');
mask1 = pixel_labels==1;
cluster1 = scale .* uint8(mask1);
subplot(1,4,2);
imshow(cluster1);
title('objects in cluster1');
mask2 = pixel_labels==2;
cluster2 = scale .* uint8(mask2);
subplot(1,4,3);
imshow(cluster2);
title('objects in cluster2');
mask3 = pixel_labels==3;
cluster3 = scale .* uint8(mask3);
subplot(1,4,4);
imshow(cluster3);
title('objects in cluster3');
end
corresponding outputs:
wanted output:
please help me to correct the code .

  0 Comments

Sign in to comment.

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 2 Oct 2020
Part 1
"I 'm just tried to code the segmentation part.but i am not sure is it correct or not.
In segmentation work, answer would be OK (Acceptable) or not OK (Not satisfactory/Not Acceptable)? You may check the accuracy (TP,FP,TN,FN) of the same while comparing with ground truth images.
There is no right or wrong answer (Yes/No). The accuracy expressed based on percentage or other quality metrices.
Part 2
And also i want the entire background of cluster1 is white and background of cluster2 and cluster3 are black.but i didn't get it.can you please help me to solve these problems."
For White background
cluster1=imgage.*uint8(mask);
cluster1(cluster1==0)=255;
imshow(cluster1);
For Black background
cluster1=imgage.*uint8(mask);
imshow(cluster1);

  3 Comments

Keerthi  D
Keerthi D on 12 Oct 2020

Sir l got only the background of cluster1 is white. But the background of other two clusters are not changed.And in cluster1, leaf image also changed when the background become white. Please help me to correct these mistakes.

Keerthi  D
Keerthi D on 12 Oct 2020

These are the current outputs

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!