77 views (last 30 days)

Hi professionals,

I am grateful for you acknowledging my requests firstly!

I am trying to understand the steps to conduct KNN classification on **One Image**! not a whole data set as it is still challenging for me to comprehend the technical aspects of it in MATLAB!

I manage to get something going and I am not sure if this is what ***The KNN** should be and the Plotted area specified is not showing!

Can a professional take a look at this and see where I took a wrong turn please? I am not certain of the stages of doing the KNN process but I gave it my best shot!!

If you have an example of how to conduct this process step by step somewhere, It would mean the world to me to grasp the understand, just point me to the location or direction and I will try for myself..

Hope I had something at least right in my example!!

I would ever be grateful!

Thanks in advance for your support I appreciate this loads!

My CODE:

%Step 1

im = imread('car.jpg');

figure; imshow(im);

[r,c,s]=size(im);

classes={'Navy Blue','Cocoa Brown','Grey','Silver','White','Brown','Light Green'};

numberofClasses=length(classes); %The Number of The Classes Would be 7 Colours

sample_regions = false([r c numberofClasses]); %initialise a matrics for sample regions not that it is all equal to zeros denoted by the syntax

%Step 2 Selecting the sample regions

figim = figure;

for count=1:numberofClasses % Selecting the sample regions for every 7 class that was created

set(figim,'name',['Please Select The Sample Region For' classes{count}]);

sample_regions(:,:,count)=roipoly(im);

end

close(figim);

%Step 3 After selecting the sample regions it is displayed via the loop

for count=1:numberofClasses

figure

imshow(sample_regions(:,:,count))

title(['The Sample Region For' classes{count}]);

end

%Step 4 Convert RGB image to L*a*B image

cform = makecform('srgb2lab');

lab_im=applycform(im,cform);

%Step 5 Once the L*a*B Values were calculate The (MEAN) for the (a*) and (*b)

a=lab_im(:,:,2);

b=lab_im(:,:,3);

color_markers= repmat(0, [numberofClasses, 2]); %This matrix will highlight the values

%of the (MEAN of the a & b varibles for all of the 7 classes that was selected)

for count=1:numberofClasses

color_markers(count,1)=mean2(a(sample_regions(:,:,count)));

color_markers(count,2)=mean2(b(sample_regions(:,:,count)));

end

%Step 6 Classify Each Pixel Using The Nearest Neighbour Rule

color_labels=0:numberofClasses-1;

distance=repmat(0, [size(a), numberofClasses]);

%%Step 7 Performing the Classifiation

for count=1:numberofClasses %iterating from 1 through the 7 classes

distance(:,:,count)=((a-color_markers(count,1)).^2+(b-color_markers(count,2)).^2).^0.5;

end

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

label=color_labels(label);

colors=[0.1686 0.2235 1.0000; 0.2784 0.1608 0.0824; 0.5 0.5 0.5; 0.7294 0.7216 0.7216; 1 1 1; 0.5216 0.4196 0.3843;...

0.4667 0.6745 0.1882];

y=zeros(size(im));

l=double(label)+1;

for m=1:r

for n=1:c

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

end

end

figure; imshow(y)

colorbar

%Step 8 Scatter plot for the nearest neighbour classification

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

plot_labels = {'k', 'r', 'g', purple, 'm', 'y'};

figrue;

%Step 9 plotting the values utilising the standard plot parameters

for count=1:numberofClasses

plot(a(label==count-1),b(label==count-1), '.','MarkerEdgeColor', plot_labels{count}, 'MarkerFaceColor',...

plot_labels{count});

hold on

end

%Step 10 Display The Plotted Areas

title('Scatterplot of the segmented pixels in ''a*b*'' space');

llabel('''a*'' values');

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

%%%%%% My Plotted Area is not showing and It's Baffling me! please help!! %%%%%%%

%% Thank You So Very Much!!

Image Analyst
on 30 Sep 2019

Attached is a demo for a discriminant classifier. It should be easy for you to adapt it to KNN.

Image Analyst
on 5 Oct 2019

Image Analyst
on 30 Sep 2019

Opportunities for recent engineering grads.

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/482599-using-knn-to-classify-a-single-image-example-in-matlab#comment_750864

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/482599-using-knn-to-classify-a-single-image-example-in-matlab#comment_750864

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/482599-using-knn-to-classify-a-single-image-example-in-matlab#comment_750867

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/482599-using-knn-to-classify-a-single-image-example-in-matlab#comment_750867

Sign in to comment.