Homework3
Introduction
Color image segmentation is a process of extracting from the image domain one or
more connected regions satisfying uniformity criterion which is based on features
derived from spectral components. These components are defined in a chosen color
space model. The segmentation process could be augmented by some additional
knowledge about the objects in the scene such as geometric and optical properties.
Features
There are three types of features vector:(r,g,b) or (L,s,t) or (H,S,V). Attention that when
getting (H,S,V) vectors, firstly to convert an RGB colormap to an HSV colormap. And
then the other features vector can be easily got. Each time, one type of the feature
vectors are selected.
%get features
r = img(:,:,1);
g = img(:,:,2);
b = img(:,:,3);
%RGB vectors
features = [r(:), g(:), b(:)];
%Lst vectors
l = (r(:)+g(:)+b(:))/3;
s = (r(:)-b(:))/2;
t = (2*g(:)-r(:)-b(:))/4;
% features = [l,s,t];
%HSV vectors
hsv = rgb2hsv(img);
h = hsv(:,:,1);
s = hsv(:,:,2);
v = hsv(:,:,3);
% features = [h(:), s(:), v(:)];
K-means clustering
Firstly to set the number of the centroid, and then to decide the position of the
centroid. Here Use random initialization for the K centroids. So each time the result will
be di!erent for di!erent K centroids.
% random initialization of centroids
p = randperm(size(features,1));
for i=1:k
cen(i,:)=features(p(i),:);
end
And then is to scan the image to calculate the distance between the pixels and centroids. Here
the distance is Euclidean distance. Loop by classes and dimension to separate the original
matrix to groups, and then to get the smallest distance between pixels and centroids, also label
the pixels. In addition the result matrix will be different from the original matrix which can not be
show directly, so the matrix needs to be reshaped to be shown. What’s more the max iteration is
30, so the loop from 1 to 30. And the break of the loop is the movement of the centroids is less
than 0.01. So each time, the position of the centroid is stored, and during the next loop,
compare the position of the new centroid with the last centroid.
评论0