机器学习 C++ 的opencv实现SVM图像二分类的测试 (三)【附源码】

机器学习 C++ 的opencv实现SVM图像二分类的测试 (三)

数据集合下载地址:https://download.csdn.net/download/hgaohr1021/89506900

根据上节得到的svm.xml,测试结果为:

cpp 复制代码
#include <stdio.h>  
#include <time.h>  
#include <opencv2/opencv.hpp>  

#include <iostream> 
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/core/utils/logger.hpp>
#include <opencv2/ml/ml.hpp>  
#include <io.h>

using namespace std;
using namespace cv;
using namespace cv::ml;

void getFiles(string path, vector<string>& files);
int main()
{
	int result = 0;
	string filePath = "data\\test_image\\0";
	vector<string> files;
	getFiles(filePath, files);
	int number = files.size();
	cout << number << endl;
	string modelpath = "svm.xml";
	cv::Ptr<cv::ml::SVM> svm;
	svm = cv::Algorithm::load<cv::ml::SVM>(modelpath);


	/*CvSVM svm;
	svm.clear();
	string modelpath = "svm.xml";
	FileStorage svm_fs(modelpath, FileStorage::READ);
	if (svm_fs.isOpened())
	{
		svm.load(modelpath.c_str());
	}*/

	FileStorage svm_fs(modelpath, FileStorage::READ);
	if (svm_fs.isOpened())
	{
		svm->load(modelpath.c_str());
	}

	for (int i = 0; i < number; i++)
	{
		Mat inMat = imread(files[i].c_str());
		resize(inMat, inMat, cv::Size(60, 256), (0, 0), (0, 0), cv::INTER_LINEAR);  //将图片调整为相同的大小
		Mat p = inMat.reshape(1, 1);
		p.convertTo(p, CV_32FC1);
		int response = (int)svm->predict(p);
		if (response == 1)
		{
			result++;
		}
	}


	cout << "识别个数:" << result << endl;
	cout << "识别率:" << setprecision(2) << fixed << double(result) / (double)(number) << endl;
	getchar();
	return  0;
}
void getFiles(string path, vector<string>& files)
{
	long long   hFile = 0;
	struct _finddata_t fileinfo;
	string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
	{
		do
		{
			if ((fileinfo.attrib & _A_SUBDIR))
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
					getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
			}
			else
			{
				files.push_back(p.assign(path).append("\\").append(fileinfo.name));
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}

数据集合下载地址:https://download.csdn.net/download/hgaohr1021/89506900

相关推荐
智驱力人工智能2 分钟前
无人机目标检测 低空安全治理的工程实践与价值闭环 无人机缺陷识别 农业无人机作物长势分析系统 森林防火无人机火点实时识别
人工智能·opencv·安全·yolo·目标检测·无人机·边缘计算
爱装代码的小瓶子6 分钟前
【c++与Linux基础】文件篇(5)- 文件管理系统:
linux·开发语言·c++
格林威11 分钟前
Baumer相机车牌字符分割与识别:优化车牌识别准确率的 7 种方法,附 OpenCV+Halcon 实战代码!
人工智能·opencv·计算机视觉·视觉检测·halcon·工业相机·智能相机
汉克老师12 分钟前
小学生0基础学大语言模型应用(第 19 课《字符串提示词训练(Prompt Thinking)》)
人工智能·深度学习·机器学习·语言模型·prompt·提示词
xu_yule19 分钟前
网络和Linux网络-15(IO多路转接)reactor编程-服务器
linux·运维·服务器·c++
Howrun77719 分钟前
C++_错误处理
开发语言·c++
近津薪荼25 分钟前
优选算法——滑动窗口3(子数组)
c++·学习·算法
方便面不加香菜26 分钟前
c++入门基础
c++
雍凉明月夜34 分钟前
瑞芯微RV1106G3板端部署
c++·人工智能·深度学习
Smart-Space40 分钟前
cpphtmlbuilder-c++灵活构造html
c++·html