当前位置:网站首页>Matlab的KNN分类使用(附源码),实现像素分类(自己设置训练集比例),打印测试精度
Matlab的KNN分类使用(附源码),实现像素分类(自己设置训练集比例),打印测试精度
2022-06-22 11:15:00 【求求你来BUG行不行】
Matlab的KNN使用
在这里,我们对Matlab里面的KNN使用进行讲解。
fitcknn函数使用
我们主要是实现单个图片的像素分类。
有一个原图,还有一个label.txt,这个txt里面是每一个像素所属的类别。

原图

txt大概展示的图像分类
思路:
注意:
我们是将RGB图像的同一个位置的三个像素值(也就是特征值)给一个标签。
先读取图片和标签。
img=imread('train.jpg');
label=textread('label.txt');
然后获得标签大小。
我们将一个图分为训练集和测试集,行数不变,然后列数取对应的比例。
我们将图片和标签分别取0.5的比例。
展示的图片是三通道的,然后为了使每一个像素点的特征值能多一点,所以对图片进行了腐蚀与重建,使图像更加具有分类性和将每一个像素点的特征值变为6个。也就是将原图和变换后的图像就像拼接,对测试集的处理也是这样。
然后创建分类器
现在高版本用的是fitknn。传入的是特征值,标签,说明,K值。
mdl = fitcknn(imgtrain,labeltrain,'NumNeighbors',3);
之后对测试集进行预处理。
再通过KNN模型对测试集进行预测,然后将预测的标签和真实标签进行比较,得到准确率。
predictlab = predict(mdl,img_stack);
结果:
准确率:
不同的K值会得到不同的结果。
我们这里的结果比较差很有可能是因为标签的分类比较粗糙,然后KNN分类器的结果比较细致,所以导致结果比较差。
源码:
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为对应的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),[]);
路漫漫其修远兮,吾将上下而求索!!!
边栏推荐
- Understanding of thread deadlock
- Haas506 2.0 development tutorial - Advanced Component Library -modem Info (only supports versions above 2.2)
- Web Configuration of Visual Studio Code
- 微信小程序项目实例——图片处理小工具(自制低配版美图秀秀)
- 6-9 inter application communication - sub application communication
- 【软工】 设计模块
- Eureka的InstanceInfoReplicator类(服务注册辅助工具)
- 奋斗吧,程序员——第三十九章 人生不失意,焉能慕知己
- Idr Display function obtains the summary statistical information of Poisson regression Poisson model (initial event density ratio IDR value, adjusted event density ratio IDR value and its confidence i
- electron添加SQLite數據庫
猜你喜欢
![两两交换链表中的节点[单向链表不断链原则]](/img/67/8e9f3c396a8f529a616964b69cc47f.png)
两两交换链表中的节点[单向链表不断链原则]

The software used is PHP MySQL database

TCP connection establishment process (in-depth understanding of the source code and three handshakes)

Cloud minimalist deployment svelte3 chat room

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

Electron adding SQLite database

"Dare not doubt the code, but have to doubt the code" a network request timeout analysis

【软工】 设计模块

安装pygame

Wechat applet project example - image processing gadget (self-made low configuration version of Meitu XiuXiu)
随机推荐
haas506 2.0开发教程-高级组件库-modem.info(仅支持2.2以上版本)
IO之Reader案例
MATLAB中cellstr函数的使用
“不敢去怀疑代码,又不得不怀疑代码”记一次网络请求超时分析
【云图说】 第244期 三分钟了解容器镜像服务
PHP database mysql question
Rtklib postpos carding (taking single point positioning as an example)
How much memory does a TCP connection occupy?
Reader case of IO
APM set pitch four rotor control mode
At 19:00 this Thursday evening, the 7th live broadcast of battle code Pioneer - how third-party application developers contribute to open source
Attack and defense drill | threat hunting practice case based on att & CK
R语言基于with函数对指定的协变量执行双样本t检验、t.test函数对不同分组对应的协变量的均值进行Welch双样本t检验分析、双独立样本t检验
Save: software analysis, verification and test platform
奋斗吧,程序员——第四十四章 八百里分麾下炙,五十弦翻塞外声
R语言使用read.table加载条件logistic回归分析的数据集(csv数据)、使用unique函数查看配对数据有多少组
分治思想在海量数据处理中的应用
promise升级版async,await来袭,搭配try+catch更香哦
Exchange the nodes in the linked list in pairs [the principle of one-way linked list without chain]
The R language uses the matchit package for propensity matching analysis and match The data function constructs the matched sample set, and uses visual analysis to test the balance of all covariates i