You are on page 1of 4

Project Report on Image segmentation

Using nearest neighbor


Pattern Recognition(CSM-023)
Submitted by : Teena Dubey M.Sc (CS)
Abstract We present a class of simple
algorithms for color image segmentation based
on the Nearest Neighbor (1-NN) decision rule.
The feature vector for each pixel in the image is
constructed from color components in HSI
space. Since processing all pixels with 1-NN rule
is time-consuming, we decided that only some
crate pixels will be classified straight with 1NN, while the others will then be labeled
according to their spatial neighborhood
containing pixels already classified, and only in
relatively rare cases sent to a global 1-NN
classifier.We test the accuracy and
computational efficiency of thealgorithms
applied to medical image segmentation.
Keywords image segmentation, clustering,
nearest neighbor.

Introduction
Segmentation is an important step in the process
of image analysis. During this transform the
image is divided into parts that correspond with
objects or areas of the real world contained in
the image. A complete segmentation defines a set
of disjoint regions uniquely corresponding with
objects in the input image, while a partial
segmentation divides an image area to regions
not directly corresponding with objects but, for
example, with the classes of objects .An example
of such approach is clustering also known as
pixel classification. In this group of algorithms,
the image is processed globally by assigning
each pixel to one of the defined classes, for
example objects A, objects B, or background,
without respect to object consistence. The
technique is often based on thresholding the
intensity level of each pixel. Finding an optimal
range of intensity allows separating the
background from the objects. More
sophisticated clustering algorithms are based on
image intensity histograms. Such a concept

allows finding the optimal number of classes and


automatically setting proper threshold values
for pixel classification. Clustering techniques
were recently adopted for color images. In fact,
color image segmentation requires analyzing
the pixel distribution histogram in 3D space,
which implies some difficulties. Sometimes we
have a priori knowledge about the image
content: the number of object types and their
color properties, and our task is to perform a
quantitative analysis of the image to measure
the number of objects of specified classes and
accurately establish their contours and area. In
some cases, when we have some manually
segmented pictures of the same group, we can
use them as a training set for k-NN based image
segmentation.

Figure 1. image.jpg

Coding
close all
clear all
%Read the source RGB image
x = imread('images.jpg');

figure; imshow(x)
[r,c,s] = size(x);

%%step3: classify each pixel using nearest


neighbor rule

%Initalize storage for each sample region


classes = {'red','yellow','green','blue','background'};
nclasses = length(classes);

% each color maker now has an 'a*' and a 'b*'


value. you can classify each
% pixel in the lab_x image by calculating the
euclidean distance bewteen

sample_regions = false([r c nclasses]);

% that pixel and each color maker.The smallest


distance will tell you that

%select each sample region


f = figure;
for count = 1:nclasses

% the pixel most closely matches that color


marker.For example,if the

set(f,'name',['select sample region for'


classes{count}]);

% distance between a pixel and the red color maker


is the smallest,then
% the pixel would be labeled as a red pixel.

sample_region(:,:,count) = roipoly(x);end
close(f);

color_label = 0:nclasses-1;

% display sample region

a = double(a);

for count = 1:nclasses

b = double(b);

figure

d = repmat(0,[size(a),nclasses]);

imshow(sample_region(:,:,count));

%Perform classification

title(['sample region for' classes{count}]);

for count = 1:nclasses


d(:,:,count) = ( (a -color_maker(count,1)).^2+...

end

(b -color_maker(count,2)).^2 ).^0.5;

%convert the RGB imagean L*a*b* image


cform = makecform('srgb2lab');

end

lab_x = applycform(x,cform);

[value,label] = min(d,[],3);

%calculate the mean 'a*' and 'b*' value for each


ROI area

label = color_label(label);
%clear value distance;

a = lab_x(:,:,2);
b = lab_x(:,:,3);

color =[ 255 0 0; 0 0 255 ; 255 255 0 ; 255 0 255; 0


255 255 ];

color_maker = repmat (0,[nclasses,2]);


for count = 1:nclasses
color_maker(count,1) =
mean2(a(sample_region(:,:,count)));
color_maker(count,2)
mean2(b(sample_region(:,:,count)));
end

y = zeros(size(x));
l = double(label)+1 ;
for m = 1 : r
for n = 1: c

y(m,n,:) = color(l(m,n),:);

ylabel('''b*'' values');

end
end

Conclusion

figure ; imshow(y)
colorbar
%scatter plot
classification

for

the

nearest

neighbour

purple = [119/255 72/255 152/255];


plot_labels = {'k','r','g',purple,'m','y'};
figure
for count = 1:nclasses
plot (a(label == count -1),b(label == count
-1),'.','markeredgecolor',...

The image segmentation results obtained with the


modified NN classifier are promising. In respect of
accuracy and computational efficiency taken
together, they seem to be much better than pure 1NN pixel classification. Nevertheless, the
clustering methods using histogram analysis are
still faster. On the other hand, nearest neighbor
approach offers more accurate segmentation,
because the feature vector may be constructed
appropriately to the image specifics. In the future
we intend to combine those two approaches to
image segmentation in a hybrid algorithm.

plot_labels{count},'markerfacecolor',
plot_labels{count});
hold on ;
end
title('ScatterPlot of the segmented pixels in ''a*b*''
space');
xlabel('''a*'' values ');

Reference
[1] course slide
[2]https://www.mathworks.com/matlabcentral/filee
xchange/46117-k-nearest-neighbors-for-imagesegmentation
[3]https://www.youtube.com/watch?
v=3hEvcyCJNRc

You might also like