Question

In: Electrical Engineering

Try to use K means clustering to segment an image. You can use Matlab function: kmeans(...

Try to use K means clustering to segment an image. You can use Matlab function: kmeans( )

Solutions

Expert Solution

function [mu,mask]=kmeans(ima,k)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   kmeans image segmentation
%
%   Input:
%          ima: grey color image
%          k: Number of classes
%   Output:
%          mu: vector of class means 
%          mask: clasification image mask
%
%   Author: xxxxxx
%    Email: xxxxxx
%     Date: xxxxxxx
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% check image
ima=double(ima);
copy=ima;         % make a copy
ima=ima(:);       % vectorize ima
mi=min(ima);      % deal with negative 
ima=ima-mi+1;     % and zero values

s=length(ima);

% create image histogram

m=max(ima)+1;
h=zeros(1,m);
hc=zeros(1,m);

for i=1:s
  if(ima(i)>0) h(ima(i))=h(ima(i))+1;end;
end
ind=find(h);
hl=length(ind);

% initiate centroids

mu=(1:k)*m/(k+1);

% start process

while(true)
  
  oldmu=mu;
  % current classification  
 
  for i=1:hl
      c=abs(ind(i)-mu);
      cc=find(c==min(c));
      hc(ind(i))=cc(1);
  end
  
  %recalculation of means  
  
  for i=1:k, 
      a=find(hc==i);
      mu(i)=sum(a.*h(a))/sum(h(a));
  end
  
  if(mu==oldmu) break;end;
  
end

% calculate mask
s=size(copy);
mask=zeros(s);
for i=1:s(1),
for j=1:s(2),
  c=abs(copy(i,j)-mu);
  a=find(c==min(c));  
  mask(i,j)=a(1);
end
end

mu=mu+mi-1;   % recover real range

Related Solutions

In MATLAB, Implement a hybrid clustering algorithm which combines hierarchical clustering and k-means clustering.
In MATLAB, Implement a hybrid clustering algorithm which combines hierarchical clustering and k-means clustering.
K-means clustering: a. In the k-means lab, you examined different values for k using the "knee"...
K-means clustering: a. In the k-means lab, you examined different values for k using the "knee" heuristic to pick the best value of k. Explain what is so special about the k values on the “knee”? Hint: There are two properties that together make these values of k special. b. Give an example of a type of data (data type) that k-means should not be used for and explain why.
One way to cluster objects is called k-means clustering. The goal is to find k different...
One way to cluster objects is called k-means clustering. The goal is to find k different clusters, each represented by a "prototype", defined as the centroid of cluster. The centroid is computed as follows: the jth value in the centroid is the mean (average) of the jth values of all the members of the cluster. Our goal is for every member a cluster to be closer to that cluster's prototype than to any of the other prototypes. Thus a prototype...
Question 1. What is k-means clustering? How does it work? Give a few examples that you...
Question 1. What is k-means clustering? How does it work? Give a few examples that you would use this algorithm. ---------------- Question 2. What is k-nearest neighbor? How does it work? Give a few examples that you would use this algorithm.
Hi, can you please verify with the basic code of a beginner MATLAB user, and try...
Hi, can you please verify with the basic code of a beginner MATLAB user, and try explaining how to approach if possible. Thanks a lot! :) 1. Answer the following questions regarding complex numbers. You must provide all handwritten working and MATLAB code/outputs for the problems below. (a) Express the complex number w = √ 3+i in complex exponential form. (Provide a handwritten solution) (b) (1 mark) Use MATLAB to check your answer for part (a) by calculating the length...
In your own words, summarize the steps of K-means clustering. Make sure to give example(s). What...
In your own words, summarize the steps of K-means clustering. Make sure to give example(s). What are the advantages and disadvantages of the K-means clustering? Any limitations?
We've now had an introduction to several different models: Linear regression, logistic regression, k-means, hierarchical clustering,...
We've now had an introduction to several different models: Linear regression, logistic regression, k-means, hierarchical clustering, GMM, Naive Bayes, and decision trees. For this assignment, I would like you to choose three models from the above list and describe two problems that each of the models could potentially be used to solve. You can do one big post with all three models and six solvable problems or do three separate posts if you prefer.   Short Explanation of Decision Trees Decision...
Matlab question: 1. Implement image filtering in frequency domain on the "testtpattern512.tif" image. You will design...
Matlab question: 1. Implement image filtering in frequency domain on the "testtpattern512.tif" image. You will design the filter, and filter the image in the frequency domain, then use inverse DFT to display the filtered image in the spatial domain. Assume zero padding for filtering process. a- Design a rectangular ideal low pass filter with a cutoff frequency of (Uo, Vo), filtering the image so that the vertical lines are blurred together, horizontal lines are basically intact and the letters are...
matlab use matlab to calculate the velocity of the vertical falling ball (v) as a function...
matlab use matlab to calculate the velocity of the vertical falling ball (v) as a function of time by numerical derive the following measurement values.(x is height in meters and y is the landing time in milliseconds) Make sure that the speed in the starting point is 0. Then reset instantaneous velocity as a function of time in a diagram with matlab x=[0.00 0.20 0.40 0.60 0.80 1.00 1.20 1.50 2.00 2.50 3.00 3.40 3.80 4.20 4.60 5.0] y=[[0.00 1.620...
IN MATLAB, Create a 8x8 magic matrix. Then use MATLAB built in function, sum, to calculate...
IN MATLAB, Create a 8x8 magic matrix. Then use MATLAB built in function, sum, to calculate the sum of each column. Use subscript notation to find the 6th element and the last element of each magic square. Find the maximum of all the elements in each magic square.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT