MATLAB Answers

Using KNN to Classify a Single Image Example in MATLAB

77 views (last 30 days)
Matpar on 28 Sep 2019
Commented: Matpar on 7 Oct 2019
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!
%Step 1
im = imread('car.jpg');
figure; imshow(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}]);
%Step 3 After selecting the sample regions it is displayed via the loop
for count=1:numberofClasses
title(['The Sample Region For' classes{count}]);
%Step 4 Convert RGB image to L*a*B image
cform = makecform('srgb2lab');
%Step 5 Once the L*a*B Values were calculate The (MEAN) for the (a*) and (*b)
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
%Step 6 Classify Each Pixel Using The Nearest Neighbour Rule
distance=repmat(0, [size(a), numberofClasses]);
%%Step 7 Performing the Classifiation
for count=1:numberofClasses %iterating from 1 through the 7 classes
[value, label]=min(distance,[],3);
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];
for m=1:r
for n=1:c
figure; imshow(y)
%Step 8 Scatter plot for the nearest neighbour classification
purple = [119/255 73/255 152/255];
plot_labels = {'k', 'r', 'g', purple, 'm', 'y'};
%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',...
hold on
%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
Image Analyst on 28 Sep 2019
You forgot to attach car.jpg. I'll check back later in the day for it.
Matpar on 28 Sep 2019
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

Sign in to comment.

Accepted Answer

Image Analyst
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.


Matpar on 30 Sep 2019
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!
Matpar on 4 Oct 2019
I think i am getting mixed up with classifier by Knn and clustering by knn
Image Analyst
Image Analyst on 5 Oct 2019
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.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 30 Sep 2019
See attached demo for using KNN classification to classify an RGB image into the number of classes you specify. Adapt as needed.
0001 Screenshot.png


Matpar on 5 Oct 2019
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
Matpar on 7 Oct 2019
ok kool thank you a mill. appreciate this loads

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by