K-Means Clustering for Image Segregation
Dataset: ------------ https://github.com/SatadruMukherjee/D... Code:
clc
clear all
close all
warning off
unzip("Mixed_Dataset.zip",'myfolder');
imds=imageDatastore('Mixed_Dataset');
ms=[];
figure(1);
for i=1:numel(imds.Files)
img=readimage(imds,i);
subplot(7,4,i);
imshow(img);
set(gcf,'color','c');
ms=[ms;size(img)];
end
features=[];
figure(2);
for i=1:numel(imds.Files)
img=readimage(imds,i);
img=~imbinarize(img);
area_value=bwarea(img);
perimeter_value=bwperim(img,8);
perimeter=nnz(perimeter_value);
conv_hull=bwconvhull(img);
convex_hull=sum(sum(conv_hull));
features=[features;area_value
perimeter convex_hull];
subplot(7,4,i);
imshow(conv_hull);
set(gcf,'color','c');
end
features(:,1)=(features(:,1)-min(features(:,1)))/(max(features(:,1))-min(features(:,1)));
features(:,2)=(features(:,2)-min(features(:,2)))/(max(features(:,2))-min(features(:,2)));
features(:,3)=(features(:,3)-min(features(:,3)))/(max(features(:,3))-min(features(:,3)));
indexes=kmeans(features,4,'Replicates',10);
c=1;
s=1;
t=1;
st=1;
for i=1:numel(imds.Files)
img=readimage(imds,i);
if(indexes(i)==1)
figure(3);
subplot(3,3,c);
imshow(img);
set(gcf,'color','c');
c=c+1;
elseif(indexes(i)==2)
figure(4);
subplot(3,3,s);
imshow(img);
set(gcf,'color','c');
s=s+1;
elseif(indexes(i)==3)
figure(5);
subplot(3,3,t);
imshow(img);
set(gcf,'color','c');
t=t+1;
else
figure(6);
subplot(3,3,st);
imshow(img);
set(gcf,'color','c');
st=st+1;
end
end
No comments