필터 지우기
필터 지우기

Image segmentation by k-means algorithm

조회 수: 4 (최근 30일)
mohammed abdul wadood
mohammed abdul wadood 2018년 2월 11일
댓글: Federico Alvarez 2024년 1월 21일
I have an code for k-means segmentation but I have some problems when I applying it on my multispectral satellite image I facing error in dimensions (Subscripted assignment dimension mismatch), how can I solve this problem?
% Grayscale Image Segmentation Using K-Means Algorithm Function Kmeans segmentation
clc
close all
clear all
[im,map]=imread('pp1.bmp');
im=im2double(im);
[row,col]=size(im);
% number of clusters
nc=4;
% initial random cluster centroids
cs=rand(nc,1);
pcs=cs;
% number of iteration
T=50;
t=0;
D=zeros(row,col,nc);
tsmld=[];
eps=1.e-5;
cmx=1;
while (t<T && cmx>eps)
%Distance between centroids and image's pixel
for c=1:nc
D(:,:,c)=(im-cs(c)).^2;
end
%assign members (image pixels)to minimum distance clusters
[mv,ML]=min(D,[],3);
%updat cluster centroid
for c=1:nc
I=(ML==c);
cs(c)=mean(mean(im(I)));
end
%find maximum absolute difference between crrent and previous iteration
%cluster centroids
cmx=max(abs(cs-pcs));
pcs=cs;
t=t+1;
%sum difference between centroid and their members and store it for
%plotting energy minimization functions
tsmld=[tsmld; sum(mv(:))];
end
% assign a colour to each cluster
colors=hvs(nc);
sim=colors(ML,:);
sim=reshape(sim,row,col,3);
figure,subplot(1,2,1),imshow(im,map);
title('Input Image: pp1');
subplot(1,2,2);imshow(sim,map);
title('segmented Image:pp1')
figure;plot(tsmld,'*-b')
xlabel('Iteration');ylabel('Energy');
title('K-means energy minimization-pp1');

채택된 답변

Image Analyst
Image Analyst 2018년 2월 11일
I don't even see a call to kmeans() in your code. Adapt my attached example to however many spectral bands you want to use (it should be obvious how to do it).
  댓글 수: 10
Federico Alvarez
Federico Alvarez 2024년 1월 21일
Unrelated / related - thank you all (specially Image Analyst). I fell into a similar issue, and was suspecting that the way I was arranging my data was the problem. Looking at your code samples helped me confirm the problem.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jaswinder Singh
Jaswinder Singh 2019년 4월 16일
can anybody provide me the code to find coefficient of a hysteresis loop whose coordinates are given to me. data is attached

Community Treasure Hunt

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

Start Hunting!

Translated by