当前位置:网站首页>KNN classification of MATLAB (with source code) is used to realize pixel classification (set the proportion of training set by yourself) and print test accuracy

KNN classification of MATLAB (with source code) is used to realize pixel classification (set the proportion of training set by yourself) and print test accuracy

2022-06-22 11:43:00 Can you bug, please

Matlab Of KNN Use

ad locum , We are right. Matlab Inside KNN Use to explain .
fitcknn Function USES
We mainly realize the pixel classification of a single image .
There is an original picture , One more label.txt, This txt Inside is the category to which each pixel belongs .

 Please add a picture description
Original picture

 Insert picture description here
txt Roughly show the image classification

Ideas :
Be careful :
We are will RGB Three pixel values at the same position of the image ( That is, eigenvalues ) Give a label .
Read the pictures and labels first .

img=imread('train.jpg');
label=textread('label.txt');

Then get the label size .
We divide a graph into training set and test set , The number of rows remains the same , Then take the corresponding proportion of the column number .
We take the picture and the label separately 0.5 The proportion of .
 Insert picture description here

The pictures shown are three channel , Then, in order to make the characteristic value of each pixel a little more , So the image was corroded and reconstructed , Make the image more classified and change the characteristic value of each pixel into 6 individual . That is, the original image and the transformed image are like stitching , The same is true for test sets .

Then create a classifier
Now the higher version uses fitknn. What is passed in is the characteristic value , label , explain ,K value .

mdl = fitcknn(imgtrain,labeltrain,'NumNeighbors',3);

Then preprocess the test set .
Re pass KNN The model predicts the test set , Then compare the predicted tags with the real tags , Get accuracy .

predictlab = predict(mdl,img_stack);

result : Insert picture description here
Accuracy rate :
 Insert picture description here
Different K Values give different results .
The poor results here may be due to the rough classification of labels , then KNN The result of the classifier is more detailed , So the result is poor .

Source code :

close all; 
clear; 
clc;
img=imread('train.jpg');
label=textread('label.txt');
[m,n]=size(label);
imgtrain = img(:,1:0.5*n,:);
labeltrain = label(:,1:0.5*n);
subplot(1,4,1),imshow(imgtrain,[]);
subplot(1,4,2),imshow(labeltrain,[]);

se1=strel('disk',3);
img_erode=imerode(imgtrain, se1);
img_reop=imreconstruct(img_erode,imgtrain);
img_stack=cat(3,imgtrain,img_reop);
imgtrain=reshape(img_stack,320*120,6);
labeltrain=reshape(labeltrain,320*120,1);
mdl = fitcknn(imgtrain,labeltrain,'NumNeighbors',3);%k For the corresponding 1,2,3,4.....

imgtest = img(:,0.5*n+1:n,:);
img_erode=imerode(imgtest, se1);
img_reop=imreconstruct(img_erode,imgtest);
img_stack=cat(3,imgtest,img_reop);
labeltest = label(:,0.5*n+1:n);
labeltest = reshape(labeltest,320*120,1);
img_stack=reshape(img_stack,320*120,6);

predictlab = predict(mdl,img_stack);
accuracy=length(find(predictlab==labeltest))/length(labeltest)*100;
subplot(1,4,3),imshow(imgtest,[]);
subplot(1,4,4),imshow(reshape(predictlab, 320,120),[]);

What a long long road! , I will go up and down !!!

原网站

版权声明
本文为[Can you bug, please]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221115301339.html