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!!

댓글 수: 2
Image Analyst
2019년 9월 28일
You forgot to attach car.jpg. I'll check back later in the day for it.
Matpar
2019년 9월 28일
채택된 답변
추가 답변 (1개)
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.


댓글 수: 14
Matpar
2019년 10월 1일
Image Analyst
2019년 10월 2일
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.
Image Analyst
2019년 10월 2일
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.
Matpar
2019년 10월 3일
Image Analyst
2019년 10월 3일
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?
Matpar
2019년 10월 4일
Matpar
2019년 10월 4일
Matpar
2019년 10월 5일
Image Analyst
2019년 10월 5일
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.
Matpar
2019년 10월 5일
Image Analyst
2019년 10월 6일
See the Mathworks page Machine Learning, classification, clustering
Matpar
2019년 10월 7일
카테고리
도움말 센터 및 File Exchange에서 Nearest Neighbors에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!