当前位置:网站首页>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 .

Original picture

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 .
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 :
Accuracy rate :
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 !!!
边栏推荐
- 奋斗吧,程序员——第四十三章 十步杀一人,千里不留行
- Vector data of Zunyi city's benchmark land price in 2022 (WGS84)
- 奋斗吧,程序员——第四十章 一面风情深有韵,半笺娇恨寄幽怀
- SQLMap-hh
- 两两交换链表中的节点[单向链表不断链原则]
- CF736 D2
- Kruskal reconstruction tree
- Dirichlet prefix and study notes
- “不敢去怀疑代码,又不得不怀疑代码”记一次网络请求超时分析
- Should the theme of the IDE be bright or dark? Here comes the ultimate answer!
猜你喜欢

Electron adding SQLite database

Typical life cycle model of information system project

Add custom fields to the time synchronization message based on uavcan protocol in Px4 code learning

安装pygame

美团基于 Flink 的实时数仓平台建设新进展

xlrd. biffh. XLRDError: Excel xlsx file; Not supported solution

How many of the eight classic MySQL errors did you encounter?

庖丁解牛,这八个MySQL经典错误,你遇到几个?

1.11 haas506 2.0开发教程-driver-RTC(仅支持2.2以上版本)

From prototype chain to inheritance, illustrate the context and recommend collection
随机推荐
Matlab的KNN分类使用(附源码),实现像素分类(自己设置训练集比例),打印测试精度
2022年度敏捷教练行业现状报告(2022 State of Agile Coaching Report)
IO之Reader案例
如果你是个半路出家的程序员,请一字一句的看完
R语言使用read.table加载条件logistic回归分析的数据集(csv数据)、使用unique函数查看配对数据有多少组
牛客挑战赛53C
The role of connect in the network
Microsoft edge browser dev 104 is released, and the deep / shallow theme switching is smoother
Save: software analysis, verification and test platform
PHP database mysql question
克鲁斯卡尔重构树
从原型链到继承,图解来龙去脉,推荐收藏
奋斗吧,程序员——第三十九章 人生不失意,焉能慕知己
鉴权之cookie、session、JWT
7-1 framework Publishing - publishing framework through NPM
How many of the eight classic MySQL errors did you encounter?
Vector data of Zunyi city's benchmark land price in 2022 (WGS84)
R language performs two sample t-test on the specified covariates based on the with function, and the t.test function performs Welch two sample t-test analysis and two independent sample t-test on the
R语言使用自定义函数编写深度学习阶跃step激活函数、并可视化阶跃step激活函数
IO之Reader案例