一、OpenCV支持向量机(SVM)模块
1.1 openCV的机器学习库
OpenCV-ml库是OpenCV(开放源代码计算机视觉库)中的机器学习模块,常用于分类和回归问题,它是 OpenCV 众多modules下的一个模块。
该模块提供了一系列常见的统计模型和分类算法,用于进行各种机器学习任务。以下是关于OpenCV-ml库的一些主要功能和特点:
- 丰富的算法支持:OpenCV-ml库包含了多种机器学习算法,如支持向量机(SVM)、决策树、Boosting方法、K近邻(KNN)、随机森林等。这些算法可以用于分类、回归、聚类等多种任务。
- 易于使用:OpenCV-ml库提供了简洁的API接口,使得开发者能够方便地调用各种机器学习算法。同时,它也支持多种数据格式,方便用户导入和处理数据。
- 高效性:OpenCV-ml库经过优化,能够高效地处理大规模数据集,并且具有较快的运算速度。这使得它能够满足实时处理和分析的需求。
- 与OpenCV其他模块的集成:OpenCV-ml库与OpenCV的其他模块(如imgproc、features2d等)紧密集成,可以方便地进行图像处理和特征提取,然后将提取的特征用于机器学习任务。
1.2 SVM(支持向量机)模块
OpenCV 的 SVM(支持向量机)模块是 OpenCV 机器学习库中的一个重要组成部分,它实现了支持向量机算法,用于解决分类和回归问题。支持向量机是一种监督学习模型,广泛应用于各种领域,特别是在图像分类和识别任务中。
OpenCV 的 SVM 模块提供了灵活的参数设置和多种核函数选择,以适应不同的数据集和问题。以下是一些关于 OpenCV SVM 模块的主要特点:
-
多种核函数:支持线性核、多项式核、径向基函数(RBF)核和 Sigmoid 核等,可以根据问题的特性选择合适的核函数。
-
参数调整:可以通过调整 SVM 的参数,如 C 值(错误项的惩罚系数)和 gamma 值(对于 RBF、Poly 和 Sigmoid 核函数),来优化模型的性能。
-
多类分类支持:通过“一对一”或“一对多”的方式,可以处理多类分类问题。
-
概率估计:SVM 可以输出类别的概率估计,这对于某些应用(如置信度评估)非常有用。
-
易于使用:OpenCV 提供了简洁的 API,使得 SVM 的训练和测试过程相对简单。
1.3 支持向量机(SVM)应用步骤
在OpenCV中,使用支持向量机(SVM)进行预测涉及几个步骤。首先,获得训练数据,用于训练一个SVM模型,然后使用该模型对新的、未见过的数据进行预测。
使用svm模型,包含必要的头文件:
#include <opencv2/opencv.hpp>
#include <opencv2/ml/ml.hpp>
1) 准备训练和测试数据:
你需要为SVM准备训练和测试数据。这些数据通常是特征向量,存储在cv::Mat对象中。每个特征向量对应一个标签(分类的类别)。
2)创建和训练SVM模型:
使用OpenCV的cv::ml::SVM类来创建SVM模型。然后,使用train方法来训练模型。
3) 进行预测:
使用训练好的模型对新数据进行预测。这通常涉及将新数据作为输入传递给模型的predict方法。
二、支持向量机(SVM)应用示例
2.1 训练及验证数据获取
以下展示如何使用OpenCV的机器学习模块来实现一个基于SVM的手写数字识别器。首先前往网站:MNIST handwritten digit database, Yann LeCun, Corinna Cortes and Chris Burges,下载MNIST database,用于实现一个SVM的手写数字识别模型训练及验证。
下载完成后,进行解压操作:
解压后是idx1-ubyte
和 idx3-ubyte
是两种常见的标签编码格式,主要用于图像分割任务中。它们都是用来表示图像中每个像素所属类别的标签图像(也称为掩码或mask)。
-
idx1-ubyte:
- idx: 表示这是一个索引图像。
- 1: 表示每个像素用一个字节(8位)来表示,且这些值从0开始,通常是连续的整数。
- ubyte: 表示无符号字节类型,其值的范围是0到255。在
idx1-ubyte
格式中,通常会将0用作背景或未标记的类别,而其他值则用于表示不同的分割区域或类别。
-
idx3-ubyte:
- idx: 同样表示这是一个索引图像。
- 3: 这里并不是指每个像素用3个字节来表示,而是指每个像素用一个字节来表示,但值的范围是从0到255,通常用来表示256个不同的类别(包括0作为背景或未标记的类别)。注意,虽然名为
idx3
,但实际上它并不是用3个字节来存储每个像素的值。 - ubyte: 同样表示无符号字节类型。
在图像分割任务中,这些标签图像通常与原始RGB图像一起使用。RGB图像用于显示给人类观察者或作为模型的输入,而标签图像则用于训练模型或评估模型的性能。
2.2 训练及验证数据加载
idx3-ubyte
文件通常与 MNIST 数据集相关联,这是一个大型的手写数字数据库,经常用于机器学习和深度学习中的图像识别任务。MNIST 数据集包含两个文件:train-images-idx3-ubyte
和 train-labels-idx1-ubyte
(用于训练),以及 t10k-images-idx3-ubyte
和 t10k-labels-idx1-ubyte
(用于测试)。这些文件使用特定的二进制格式存储图像和标签。
通过两个函数来读取手写图像数据集和手写图像数据对应的标签(每个标签都是一个 0 到 9 之间的整数,表示对应图像中的手写数字)。
//大小端转换
int intReverse(int num)
{
return (num>>24|((num&0xFF0000)>>8)|((num&0xFF00)<<8)|((num&0xFF)<<24));
}
//读取手写图像数据集
cv::Mat read_mnist_image(const std::string fileName) {
int magic_number = 0;
int number_of_images = 0;
int img_rows = 0;
int img_cols = 0;
cv::Mat DataMat;
std::ifstream file(fileName, std::ios::binary);
if (file.is_open())
{
std::cout << "open images file: "<< fileName << std::endl;
file.read((char*)&magic_number, sizeof(magic_number));//format
file.read((char*)&number_of_images, sizeof(number_of_images));//images number
file.read((char*)&img_rows, sizeof(img_rows));//img rows
file.read((char*)&img_cols, sizeof(img_cols));//img cols
magic_number = intReverse(magic_number);
number_of_images = intReverse(number_of_images);
img_rows = intReverse(img_rows);
img_cols = intReverse(img_cols);
std::cout << "format:" << magic_number
<< " img num:" << number_of_images
<< " img row:" << img_rows
<< " img col:" << img_cols << std::endl;
std::cout << "read img data" << std::endl;
DataMat = cv::Mat::zeros(number_of_images, img_rows * img_cols, CV_32FC1);
unsigned char temp = 0;
for (int i = 0; i < number_of_images; i++) {
for (int j = 0; j < img_rows * img_cols; j++) {
file.read((char*)&temp, sizeof(temp));
//svm data is CV_32FC1
float pixel_value = float(temp);
DataMat.at<float>(i, j) = pixel_value;
}
}
std::cout << "read img data finish!" << std::endl;
}
file.close();
return DataMat;
}
//读取手写标签
cv::Mat read_mnist_label(const std::string fileName) {
int magic_number;
int number_of_items;
cv::Mat LabelMat;
std::ifstream file(fileName, std::ios::binary);
if (file.is_open())
{
std::cout << "open label file: "<< fileName << std::endl;
file.read((char*)&magic_number, sizeof(magic_number));
file.read((char*)&number_of_items, sizeof(number_of_items));
magic_number = intReverse(magic_number);
number_of_items = intReverse(number_of_items);
std::cout << "format:" << magic_number << " ;label_num:" << number_of_items << std::endl;
std::cout << "read Label data" << std::endl;
//data type:CV_32SC1,channel:1
LabelMat = cv::Mat::zeros(number_of_items, 1, CV_32SC1);
for (int i = 0; i < number_of_items; i++) {
unsigned char temp = 0;
file.read((char*)&temp, sizeof(temp));
LabelMat.at<unsigned int>(i, 0) = (unsigned int)temp;
}
std::cout << "read label data finish!" << std::endl;
}
file.close();
return LabelMat;
}
2.3 SVM(支持向量机)训练及验证,输出svm模型
1)加载训练图像数据和标签数据,采用cv::Mat存储,图像数据虚归一化;
2)创建svm模型,设置svm模型的各关联参数,不同参数设置,对应模型精度有较大影响;
3)加载测试图像数据和标签数据,采用cv::Mat存储,图像数据虚归一化;
4)采用测试图像数据验证已经训练好的svm模型,获得测试推演结果;
5)通过测试结果和已有的标签数据进行校对,验证该模型精度。
6)将训练好的模型保持输出。便于后续用于实时识别应用。
//change path for real paths
std::string trainImgFile = "D:\\workForMy\\OpenCVLib\\opencv_demo\\opencv_ml01\\train-images.idx3-ubyte";
std::string trainLabeFile = "D:\\workForMy\\OpenCVLib\\opencv_demo\\opencv_ml01\\train-labels.idx1-ubyte";
std::string testImgFile = "D:\\workForMy\\OpenCVLib\\opencv_demo\\opencv_ml01\\t10k-images.idx3-ubyte";
std::string testLabeFile = "D:\\workForMy\\OpenCVLib\\opencv_demo\\opencv_ml01\\t10k-labels.idx1-ubyte";
void train_SVM()
{
//read train images, data type CV_32FC1
cv::Mat trainingData = read_mnist_image(trainImgFile);
//images data normalization
trainingData = trainingData/255.0;
std::cout << "trainingData.size() = " << trainingData.size() << std::endl;
//read train label, data type CV_32SC1
cv::Mat labelsMat = read_mnist_label(trainLabeFile);
std::cout << "labelsMat.size() = " << labelsMat.size() << std::endl;
std::cout << "trainingData & labelsMat finish!" << std::endl;
//create SVM model
cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
//set svm args,type and KernelTypes
svm->setType(cv::ml::SVM::C_SVC);
svm->setKernel(cv::ml::SVM::POLY);
//KernelTypes POLY is need set gamma and degree
svm->setGamma(3.0);
svm->setDegree(2.0);
//Set iteration termination conditions, maxCount is importance
svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::EPS | cv::TermCriteria::COUNT, 1000, 1e-8));
std::cout << "create SVM object finish!" << std::endl;
std::cout << "trainingData.rows = " << trainingData.rows << std::endl;
std::cout << "trainingData.cols = " << trainingData.cols << std::endl;
std::cout << "trainingData.type() = " << trainingData.type() << std::endl;
// svm model train
svm->train(trainingData, cv::ml::ROW_SAMPLE, labelsMat);
std::cout << "SVM training finish!" << std::endl;
// svm model test
cv::Mat testData = read_mnist_image(testImgFile);
//images data normalization
testData = testData/255.0;
std::cout << "testData.rows = " << testData.rows << std::endl;
std::cout << "testData.cols = " << testData.cols << std::endl;
std::cout << "testData.type() = " << testData.type() << std::endl;
//read test label, data type CV_32SC1
cv::Mat testlabel = read_mnist_label(testLabeFile);
cv::Mat testResp;
float response = svm->predict(testData,testResp);
// std::cout << "response = " << response << std::endl;
testResp.convertTo(testResp,CV_32SC1);
int map_num = 0;
for (int i = 0; i <testResp.rows&&testResp.rows==testlabel.rows; i++)
{
if (testResp.at<int>(i, 0) == testlabel.at<int>(i, 0))
{
map_num++;
}
// else{
// std::cout << "testResp.at<int>(i, 0) " << testResp.at<int>(i, 0) << std::endl;
// std::cout << "testlabel.at<int>(i, 0) " << testlabel.at<int>(i, 0) << std::endl;
// }
}
float proportion = float(map_num) / float(testResp.rows);
std::cout << "map rate: " << proportion * 100 << "%" << std::endl;
std::cout << "SVM testing finish!" << std::endl;
//save svm model
svm->save("mnist_svm.xml");
}
2.4 SVM(支持向量机)实时识别应用
将t10k-images.idx3-ubyte处理成图片数据,用于svm模型调用示例,本文主要是通过一段python代码,将t10k-images.idx3-ubyte另存为一张张手写图片。
import numpy as np
import os
from PIL import Image
from struct import unpack
def read_idx3_ubyte(filename):
with open(filename, 'rb') as f:
magic, num_images, rows, cols = unpack('>IIII', f.read(16))
buf = f.read()
data = np.frombuffer(buf, dtype=np.uint8).reshape((num_images, rows, cols))
return data
def save_images_as_png(idx3_file, output_dir, prefix='image'):
images = read_idx3_ubyte(idx3_file)
for i, image in enumerate(images):
image_pil = Image.fromarray(image, 'L') # 'L' 表示灰度模式
filename = f"{output_dir}/{prefix}_{i}.png"
image_pil.save(filename)
# 使用示例
# idx3_file = 'train-images.idx3-ubyte'
# output_dir = 'train-images'
# if not os.path.exists(output_dir):#检查目录是否存在
# os.makedirs(output_dir)#如果不存在则创建目录
# save_images_as_png(idx3_file, output_dir)
idx3_file = 't10k-images.idx3-ubyte'
output_dir = 't10k-images'
if not os.path.exists(output_dir):#检查目录是否存在
os.makedirs(output_dir)#如果不存在则创建目录
save_images_as_png(idx3_file, output_dir)
在获得图片数据后,将加载这些图片,和上述已保存的svm模型(mnist_svm.xml),实现模型调用验证。
void prediction(const std::string fileName,cv::Ptr<cv::ml::SVM> svm)
{
//read img 28*28 size
cv::Mat image = cv::imread(fileName, cv::IMREAD_GRAYSCALE);
//uchar->float32
image.convertTo(image, CV_32F);
//image data normalization
image = image / 255.0;
//28*28 -> 1*784
image = image.reshape(1, 1);
//预测图片
float ret = svm->predict(image);
std::cout << "predict val = "<< ret << std::endl;
}
std::string imgDir = "D:\\workForMy\\OpenCVLib\\opencv_demo\\opencv_ml01\\t10k-images\\";
std::string ImgFiles[5] = {"image_0.png","image_10.png","image_20.png","image_30.png","image_40.png",};
void predictimgs()
{
//load svm model
cv::Ptr<cv::ml::SVM> svm = cv::ml::StatModel::load<cv::ml::SVM>("mnist_svm.xml");
for (size_t i = 0; i < 5; i++)
{
prediction(imgDir+ImgFiles[i],svm);
}
}
三、完整代码编译
3.1 OpenCV+MinGW的MakeFile编译
本文是采用win系统下,opencv采用MinGW编译的静态库(C/C++开发,win下OpenCV+MinGW编译环境搭建_opencv mingw-CSDN博客),建立makefile:
#/bin/sh
#win32
CX= g++ -DWIN32
#linux
#CX= g++ -Dlinux
BIN := ./
TARGET := opencv_ml01.exe
FLAGS := -std=c++11 -static
SRCDIR := ./
#INCLUDES
INCLUDEDIR := -I"../../opencv_MinGW/include" -I"./"
#-I"$(SRCDIR)"
staticDir := ../../opencv_MinGW/x64/mingw/staticlib/
#LIBDIR := $(staticDir)/libopencv_world460.a\
# $(staticDir)/libade.a \
# $(staticDir)/libIlmImf.a \
# $(staticDir)/libquirc.a \
# $(staticDir)/libzlib.a \
# $(wildcard $(staticDir)/liblib*.a) \
# -lgdi32 -lComDlg32 -lOleAut32 -lOle32 -luuid
#opencv_world放弃前,然后是opencv依赖的第三方库,后面的库是MinGW编译工具的库
LIBDIR := -L $(staticDir) -lopencv_world460 -lade -lIlmImf -lquirc -lzlib \
-llibjpeg-turbo -llibopenjp2 -llibpng -llibprotobuf -llibtiff -llibwebp \
-lgdi32 -lComDlg32 -lOleAut32 -lOle32 -luuid
source := $(wildcard $(SRCDIR)/*.cpp)
$(TARGET) :
$(CX) $(FLAGS) $(INCLUDEDIR) $(source) -o $(BIN)/$(TARGET) $(LIBDIR)
clean:
rm $(BIN)/$(TARGET)
编译如下:
3.2 OpenCV+vc2015+cmake编译
第二种编译,本文采用了vs2015 x64编译了opencv库(C/C++开发,opencv在win下安装及应用_windows安装opencv c++-CSDN博客)。
建立cmake文件:
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (opencv_test)
#
message(STATUS "windows compiling...")
add_definitions(-D_PLATFORM_IS_WINDOWS_)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
set(WIN_OS true)
#
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# 指定源文件的目录,并将名称保存到变量
SET(source_h
#
)
SET(source_cpp
#
${PROJECT_SOURCE_DIR}/main.cpp
)
#头文件目录
include_directories(${PROJECT_SOURCE_DIR}/../../opencv_VC/include)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4819")
add_definitions(
"-D_CRT_SECURE_NO_WARNINGS"
"-D_WINSOCK_DEPRECATED_NO_WARNINGS"
"-DNO_WARN_MBCS_MFC_DEPRECATION"
"-DWIN32_LEAN_AND_MEAN"
)
link_directories(
${PROJECT_SOURCE_DIR}/../../opencv_VC/x64/vc14/bin
${PROJECT_SOURCE_DIR}/../../opencv_VC/x64/vc14/lib
)
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR})
# 指定生成目标
add_executable(opencv_testd ${source_h} ${source_cpp})
else(CMAKE_BUILD_TYPE)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR})
# 指定生成目标
add_executable(opencv_test ${source_h} ${source_cpp})
target_link_libraries(opencv_test opencv_world460.lib opencv_img_hash460.lib)
endif (CMAKE_BUILD_TYPE)
# mkdir build_win
# cd build_win
# cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release ..
# msbuild opencv_test.sln /p:Configuration="Release" /p:Platform="x64"
启动vs2015 x64的命令工具(使前面配置的环境变量生效),进入main.cpp文件目录,编译如下:
mkdir build_win
cd build_win
cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release ..
msbuild opencv_test.sln /p:Configuration="Release" /p:Platform="x64"
编译输出大致如下:
3.3 执行效果
【1】OpenCV+MinGW+makefile编译程序执行输出,准确率达到98%以上(PS,大家可尝试去调设SVM模型的参数设置,看怎样设置可以获得更高的准确率)
通过模型调用识别图片全OK(呵呵,毕竟是测试集内的图片数据)
【2】opencv+vc2015+cmake编译程序执行输出,同样能到达效果。
3.4 附件,main.cpp全文
#include <opencv2/opencv.hpp>
#include <opencv2/ml/ml.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
#include <vector>
#include <iostream>
#include <fstream>
int intReverse(int num)
{
return (num>>24|((num&0xFF0000)>>8)|((num&0xFF00)<<8)|((num&0xFF)<<24));
}
std::string intToString(int num)
{
char buf[32]={0};
itoa(num,buf,10);
return std::string(buf);
}
cv::Mat read_mnist_image(const std::string fileName) {
int magic_number = 0;
int number_of_images = 0;
int img_rows = 0;
int img_cols = 0;
cv::Mat DataMat;
std::ifstream file(fileName, std::ios::binary);
if (file.is_open())
{
std::cout << "open images file: "<< fileName << std::endl;
file.read((char*)&magic_number, sizeof(magic_number));//format
file.read((char*)&number_of_images, sizeof(number_of_images));//images number
file.read((char*)&img_rows, sizeof(img_rows));//img rows
file.read((char*)&img_cols, sizeof(img_cols));//img cols
magic_number = intReverse(magic_number);
number_of_images = intReverse(number_of_images);
img_rows = intReverse(img_rows);
img_cols = intReverse(img_cols);
std::cout << "format:" << magic_number
<< " img num:" << number_of_images
<< " img row:" << img_rows
<< " img col:" << img_cols << std::endl;
std::cout << "read img data" << std::endl;
DataMat = cv::Mat::zeros(number_of_images, img_rows * img_cols, CV_32FC1);
unsigned char temp = 0;
for (int i = 0; i < number_of_images; i++) {
for (int j = 0; j < img_rows * img_cols; j++) {
file.read((char*)&temp, sizeof(temp));
//svm data is CV_32FC1
float pixel_value = float(temp);
DataMat.at<float>(i, j) = pixel_value;
}
}
std::cout << "read img data finish!" << std::endl;
}
file.close();
return DataMat;
}
cv::Mat read_mnist_label(const std::string fileName) {
int magic_number;
int number_of_items;
cv::Mat LabelMat;
std::ifstream file(fileName, std::ios::binary);
if (file.is_open())
{
std::cout << "open label file: "<< fileName << std::endl;
file.read((char*)&magic_number, sizeof(magic_number));
file.read((char*)&number_of_items, sizeof(number_of_items));
magic_number = intReverse(magic_number);
number_of_items = intReverse(number_of_items);
std::cout << "format:" << magic_number << " ;label_num:" << number_of_items << std::endl;
std::cout << "read Label data" << std::endl;
//data type:CV_32SC1,channel:1
LabelMat = cv::Mat::zeros(number_of_items, 1, CV_32SC1);
for (int i = 0; i < number_of_items; i++) {
unsigned char temp = 0;
file.read((char*)&temp, sizeof(temp));
LabelMat.at<unsigned int>(i, 0) = (unsigned int)temp;
}
std::cout << "read label data finish!" << std::endl;
}
file.close();
return LabelMat;
}
//change path for real paths
std::string trainImgFile = "D:\\workForMy\\OpenCVLib\\opencv_demo\\opencv_ml01\\train-images.idx3-ubyte";
std::string trainLabeFile = "D:\\workForMy\\OpenCVLib\\opencv_demo\\opencv_ml01\\train-labels.idx1-ubyte";
std::string testImgFile = "D:\\workForMy\\OpenCVLib\\opencv_demo\\opencv_ml01\\t10k-images.idx3-ubyte";
std::string testLabeFile = "D:\\workForMy\\OpenCVLib\\opencv_demo\\opencv_ml01\\t10k-labels.idx1-ubyte";
void train_SVM()
{
//read train images, data type CV_32FC1
cv::Mat trainingData = read_mnist_image(trainImgFile);
//images data normalization
trainingData = trainingData/255.0;
std::cout << "trainingData.size() = " << trainingData.size() << std::endl;
//read train label, data type CV_32SC1
cv::Mat labelsMat = read_mnist_label(trainLabeFile);
std::cout << "labelsMat.size() = " << labelsMat.size() << std::endl;
std::cout << "trainingData & labelsMat finish!" << std::endl;
//create SVM model
cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();
//set svm args,type and KernelTypes
svm->setType(cv::ml::SVM::C_SVC);
svm->setKernel(cv::ml::SVM::POLY);
//KernelTypes POLY is need set gamma and degree
svm->setGamma(3.0);
svm->setDegree(2.0);
//Set iteration termination conditions, maxCount is importance
svm->setTermCriteria(cv::TermCriteria(cv::TermCriteria::EPS | cv::TermCriteria::COUNT, 1000, 1e-8));
std::cout << "create SVM object finish!" << std::endl;
std::cout << "trainingData.rows = " << trainingData.rows << std::endl;
std::cout << "trainingData.cols = " << trainingData.cols << std::endl;
std::cout << "trainingData.type() = " << trainingData.type() << std::endl;
// svm model train
svm->train(trainingData, cv::ml::ROW_SAMPLE, labelsMat);
std::cout << "SVM training finish!" << std::endl;
// svm model test
cv::Mat testData = read_mnist_image(testImgFile);
//images data normalization
testData = testData/255.0;
std::cout << "testData.rows = " << testData.rows << std::endl;
std::cout << "testData.cols = " << testData.cols << std::endl;
std::cout << "testData.type() = " << testData.type() << std::endl;
//read test label, data type CV_32SC1
cv::Mat testlabel = read_mnist_label(testLabeFile);
cv::Mat testResp;
float response = svm->predict(testData,testResp);
// std::cout << "response = " << response << std::endl;
testResp.convertTo(testResp,CV_32SC1);
int map_num = 0;
for (int i = 0; i <testResp.rows&&testResp.rows==testlabel.rows; i++)
{
if (testResp.at<int>(i, 0) == testlabel.at<int>(i, 0))
{
map_num++;
}
// else{
// std::cout << "testResp.at<int>(i, 0) " << testResp.at<int>(i, 0) << std::endl;
// std::cout << "testlabel.at<int>(i, 0) " << testlabel.at<int>(i, 0) << std::endl;
// }
}
float proportion = float(map_num) / float(testResp.rows);
std::cout << "map rate: " << proportion * 100 << "%" << std::endl;
std::cout << "SVM testing finish!" << std::endl;
//save svm model
svm->save("mnist_svm.xml");
}
void prediction(const std::string fileName,cv::Ptr<cv::ml::SVM> svm)
{
//read img 28*28 size
cv::Mat image = cv::imread(fileName, cv::IMREAD_GRAYSCALE);
//uchar->float32
image.convertTo(image, CV_32F);
//image data normalization
image = image / 255.0;
//28*28 -> 1*784
image = image.reshape(1, 1);
//预测图片
float ret = svm->predict(image);
std::cout << "predict val = "<< ret << std::endl;
}
std::string imgDir = "D:\\workForMy\\OpenCVLib\\opencv_demo\\opencv_ml01\\t10k-images\\";
std::string ImgFiles[5] = {"image_0.png","image_10.png","image_20.png","image_30.png","image_40.png",};
void predictimgs()
{
//load svm model
cv::Ptr<cv::ml::SVM> svm = cv::ml::StatModel::load<cv::ml::SVM>("mnist_svm.xml");
for (size_t i = 0; i < 5; i++)
{
prediction(imgDir+ImgFiles[i],svm);
}
}
int main()
{
train_SVM();
predictimgs();
return 0;
}