资源简介
本例采用opencv的随机森林对图像做分类,提取的是图像的颜色直方图,然后计算统计特征,采用csv文件存储图像特征。

代码片段和文件信息
// openCvRandomForest.cpp : 定义控制台应用程序的入口点。
//
#include “stdafx.h“
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
/******************************************************************************/
// global definitions (for speed and ease of use)
//随机森林实现
#define NUMBER_OF_TRAINING_SAMPLES 862 //样本数目
#define ATTRIBUTES_PER_SAMPLE 9 //每个样本的特征值,数据为csv格式
#define NUMBER_OF_TESTING_SAMPLES 14753 //测试数目
#define NUMBER_OF_CLASSES 2 //类别数目,用int类型的数字标记,从0开始
// N.B. classes are integer handwritten digits in range 0-9
/******************************************************************************/
// loads the sample database from file (which is a CSV text file)
int read_data_from_csv(const char* filename Mat data Mat classes int n_samples )
{
float tmp;
// if we can‘t read the input file then return 0
FILE* f = fopen( filename “r“ );
if( !f )
{
printf(“ERROR: cannot read file %s\n“ filename);
return 0; // all not OK
}
// for each sample in the file
for(int line = 0; line < n_samples; line++)
{
// for each attribute on the line in the file
for(int attribute = 0; attribute < (ATTRIBUTES_PER_SAMPLE + 1); attribute++)
{
if (attribute < 9)
{
// first 64 elements (0-63) in each line are the attributes
fscanf(f “%f“ &tmp);
data.at(line attribute) = tmp;
// printf(“%f“ data.at(line attribute));
}
else if (attribute == 9)
{
// attribute 65 is the class label {0 ... 9}
fscanf(f “%f“ &tmp);
classes.at(line 0) = tmp;
// printf(“%f\n“ classes.at(line 0));
}
}
}
fclose(f);
return 1; // all OK
}
/******************************************************************************/
int main( int argc char** argv)
{
// lets just check the version first
printf (“OpenCV version %s (%d.%d.%d)\n“ CV_VERSION CV_MAJOR_VERSION CV_MINOR_VERSION CV_SUBMINOR_VERSION);
const char* trainDataFile=“data7.csv“;
const char* testDataFile=“Sample11.csv“;
//定义训练数据与标签矩阵
Mat training_data = Mat(NUMBER_OF_TRAINING_SAMPLES ATTRIBUTES_PER_SAMPLE CV_32FC1);
Mat training_classifications = Mat(NUMBER_OF_TRAINING_SAMPLES 1 CV_32FC1);
//定义测试数据矩阵与标签
Mat testing_data = Mat(NUMBER_OF_TESTING_SAMPLES ATTRIBUTES_PER_SAMPLE CV_32FC1);
Mat testing_classifications = Mat(NUMBER_OF_TESTING_SAMPLES 1 CV_32F
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2016-08-17 15:48 openCvRandomForest\
目录 0 2016-08-17 15:37 openCvRandomForest\Debug\
文件 87552 2016-08-17 15:46 openCvRandomForest\Debug\openCvRandomForest.exe
文件 580324 2016-08-17 15:46 openCvRandomForest\Debug\openCvRandomForest.ilk
文件 1412096 2016-08-17 15:46 openCvRandomForest\Debug\openCvRandomForest.pdb
目录 0 2016-08-17 15:22 openCvRandomForest\ipch\
目录 0 2016-08-17 15:37 openCvRandomForest\ipch\opencvrandomforest-a952a877\
文件 2818048 2016-08-17 15:37 openCvRandomForest\ipch\opencvrandomforest-a952a877\opencvrandomforest-a1348e8b.ipch
目录 0 2016-08-17 15:26 openCvRandomForest\openCvRandomForest\
目录 0 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\
文件 16476 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\CL.read.1.tlog
文件 1114 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\CL.write.1.tlog
文件 1706 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\cl.command.1.tlog
文件 2 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\li
文件 2 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\li
文件 2 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\li
文件 2 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\li
文件 2372 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\li
文件 5454 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\li
文件 744 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\li
文件 72 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\openCvRandomForest.lastbuildstate
文件 2079 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\openCvRandomForest.log
文件 266379 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\openCvRandomForest.obj
文件 1245184 2016-08-17 15:34 openCvRandomForest\openCvRandomForest\Debug\openCvRandomForest.pch
文件 11844 2016-08-17 15:34 openCvRandomForest\openCvRandomForest\Debug\stdafx.obj
文件 429056 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\vc110.idb
文件 839680 2016-08-17 15:46 openCvRandomForest\openCvRandomForest\Debug\vc110.pdb
文件 1587 2016-08-17 15:17 openCvRandomForest\openCvRandomForest\ReadMe.txt
文件 1258985 2016-07-29 12:57 openCvRandomForest\openCvRandomForest\Sample11.csv
文件 45139 2016-07-28 21:51 openCvRandomForest\openCvRandomForest\data7.csv
文件 8084 2016-08-17 15:44 openCvRandomForest\openCvRandomForest\openCvRandomForest.cpp
............此处省略9个文件信息
- 上一篇:神舟精盾k480n热键驱动
- 下一篇:情感分析数据集
相关资源
- 随机森林R语言代码
- 基于OpenCV的数字识别468815
- 使用opencv去掉二值化图像中黑色面积
- opencv环境配置
- win10 64位下编译的opencv4.5.5库,opencv
- 随机森林算法 R 语言
- NVIDIAOpticalFlowSDK-79c6cee80a2df9a196f20afd6
- opencv_contrib-3.4.0.zip
- BoW|Pyramid BoW+SVM进行图像分类
- opencv2.4.9源码分析——SIFT
- 用两个摄像头实现,双目标定,双目
- opencv_traincascade训练分类器,手势识别
- opencv3.0交叉编译用parallel.cpp
- 基于opencv的图像识别识别图像中的色
- 基于openCV的识别特定颜色区域
- 基于OpenCV的分水岭算法实现
- QT+opencv+OCR 身份证号码,银行卡号识别
- opencv视频特定颜色区域识别
- 把RGB转换为HSV和HSI然后根据黄色和蓝
- opencv视觉测距
- 基于Qt和opencv的身份证号码识别系统
- opencv_ffmpeg249.dll
- SfM稀疏三维点云重建--完整工程文件
- 基于opencv的数人头程序源代码
- 水果识别代码
- 利用OpenCV中的Stitcher类实现全景图像拼
- 用各种机器学习方法knn,随机森林,
- opencv实现的sift算法源码,包含了图像
- openCV 上的小波变换
- 基于OPENCV的车牌识别系统设计
评论
共有 条评论