Using KNN to Classify a Single Image Example in MATLAB

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!!
car.jpg

댓글 수: 2

You forgot to attach car.jpg. I'll check back later in the day for it.
My apologies IA,
I know you do this so fast one day i will get the gist of these processes!
thanks in advance appreciate this loads

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

 채택된 답변

Image Analyst
Image Analyst 2019년 9월 30일

0 개 추천

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

댓글 수: 3

Hi IA,
Wow that's very interesting but unfortunatly I am still trying to figure out the basics. The code I posted earlier on is wrong in reference to what the KNN should be.
The thing is it's a classifier I am still finding it challenging to understand what this should return! in my code it returned an image that was segmented into colours specified within the code (white and blue).
isn't that the role of the KNN to specifiy what is the back ground and foreground?
Or am I getting mixed up!
I think i am getting mixed up with classifier by Knn and clustering by knn
It does classification according to similar colors. Those colored regions may or may not represent foreground and background as far as distance from the camera.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 9월 30일

0 개 추천

See attached demo for using KNN classification to classify an RGB image into the number of classes you specify. Adapt as needed.
00_Screenshot.png
0001 Screenshot.png

댓글 수: 14

Thank you so much #Image Analyst I willrun through it now!
Matpar
Matpar 2019년 10월 1일
편집: Matpar 2019년 10월 1일
Wow that's exactly what i was trying to achieve!!! thanx but Question?
Is there an easier way to do the same thing or is this the only method to get this process completed?
I was looking for a shorter code to do the same thing, is there such a way?
this seems like the longer method which i really appreciate and some of it I got and the rest i will get the googling to get the understanding of the operations.
If you throw out all the fancy display stuff and comments, you'll find the meat of the code is only a dozen lines of code or so. Feel free to cut them out if you want, though I don't think you should cut out any crucial comments.
Matpar
Matpar 2019년 10월 2일
편집: Matpar 2019년 10월 2일
I figured, thank you loads for your acknowledging my request with accuracy and efficiency. I really appreciate this mate!
Can I ask how long will it take me to get the understanding like you do?
letting you know in advance I am going to be here for some time, I hope to get in on the understanding's aspects.
It's taken me 40 years to learn image processing, and I'm still not done yet.
I've been doing MATLAB for 13 years. It took me about 3 months to get proficient enough to do what I want without much thought about how to code it up. I'm still learning MATLAB though.
Wow man that's some time you've been applying! I hope to get proficient as well but I am not sure if i am researching the right process to get examples like what you've provided! How do I get to find solutions like what you've provided? Is there a location link to tap into for resources like this? Or are these codes processed from your experience? Everyone keeps pointing me towards a matlab example but it's not like what you've sent never mind the adjustments I made for it to suit my needs! Please let me know so I can process my issues for my self rather than be harassing you kind gentlemen! Thanx for assisting me in everyway I am grateful nevertheless.
I'm not sure what you're asking. You can go to a university to get a Ph.D. in imaging, like I did. Then work with MATLAB everyday full time for years, like I did/do. Are you still having problems with the KNN classification?
Wow ok so like i said i will get there one day it's my goal you guys are my inspirations!!
as for the KNN it's a bit I tried going through the code line by line and displaying the results to understand the output!
in most cases that work and well because some are for loop iterations It's a bit chanllenging to really understand what is happening behind the scenes to finally present an output
are you taking on apprentices? how can I get a one on one with ya?
Hi IA,
It is possible for you to outline the piple of the knn process please or rather point me in the direction where I can understand the stages of the framework?
Thank you in advance appreciate you loads for responding
It's really super super simple. It just assigns the class to a point that is the majority of classes of other points of the training set in the same neighborhood. Try Wikipedia or Google or MATLAB documentation.
That's the thing Image Analyst! I did the search and came up with all sorts of confilicting information. If a pro points me to it then i know for sure that that's what it does rather read here and then read there it so much ppl saying different things it's sometimes challenging. I youtubed for the whole day already and well i gathered that its a classification technique and small details
ok kool thank you a mill. appreciate this loads

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

질문:

2019년 9월 28일

댓글:

2019년 10월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by