segmentation using k-means clustering

56 visualizaciones (últimos 30 días)
Keerthi  D
Keerthi D el 2 de Oct. de 2020
Respondida: Image Analyst el 21 de Ag. de 2023
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 .

Respuestas (2)

KALYAN ACHARJYA
KALYAN ACHARJYA el 2 de Oct. de 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 comentarios
Keerthi  D
Keerthi D el 12 de Oct. de 2020
Editada: Keerthi D el 12 de Oct. de 2020
Keerthi  D
Keerthi D el 12 de Oct. de 2020

These are the current outputs

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 21 de Ag. de 2023
Sorry I didn't see this when it was posted 3 years ago. As you can see the whole approach is wrong. K-means is not what you want to do unless you have a good supply of each class in each image. Otherwise what is it going to do when it has only good leaf and background, and no disease pixels? It's going to be force to find 3 classes, and it will, when you're hoping it would tell you that there are just two classes (healthy leaf and background).
What you should (or can) do is to use discriminant analysis to find the classes. You build a training set by showing/telling it which pixels belong to each class. You do this over many images to get a good representation of what each class looks like (its centroid and shape or extent). Then you use that trained classifier to grade your unknown test images. NOW it will find each class fairly accurately, and if there is no disease, youi'll see that, because there will be no disease pixels found.
See my attached demo.

Categorías

Más información sobre Agriculture en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by