Opencv 用SVM训练检测器

原创 2013年12月02日 16:52:47
用svm+HOG 训练检测器,可以在图像上进行扫描检测。
#include "stdafx.h"
// mypro.cpp : Defines the entry point for the console application.
//
#include "cv.h"
#include "highgui.h"
#include <ml.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#define DST_IMG_WIDTH 32       
#define DST_IMG_HEIGH 32
using namespace cv;
using namespace std;

class Mysvm: public CvSVM
{
public:
	int get_alpha_count()
	{
		return this->sv_total;
	}

	int get_sv_dim()
	{
		return this->var_all;
	}

	int get_sv_count()
	{
		return this->decision_func->sv_count;
	}

	double* get_alpha()
	{
		return this->decision_func->alpha;
	}

	float** get_sv()
	{
		return this->sv;
	}

	float get_rho()
	{
		return this->decision_func->rho;
	}
};

void Train()
{
	char classifierSavePath[256] = "pedestrianDetect-peopleFlow.txt";

	//string positivePath = "pos_KC_32X32\\";
	//string negativePath = "neg_KC_32X32\\";

	int positiveSampleCount = 33;
	int negativeSampleCount = 1458;
	int totalSampleCount = positiveSampleCount + negativeSampleCount;

	cout<<"//////////////////////////////////////////////////////////////////"<<endl;
	cout<<"totalSampleCount:    "<<totalSampleCount<<endl;
	cout<<"positiveSampleCount: "<<positiveSampleCount<<endl;
	cout<<"negativeSampleCount: "<<negativeSampleCount<<endl;

	CvMat *sampleFeaturesMat = cvCreateMat(totalSampleCount , 1764, CV_32FC1);
	//64*128的训练样本,该矩阵将是totalSample*3780,64*64的训练样本,该矩阵将是totalSample*1764
	cvSetZero(sampleFeaturesMat);  
	CvMat *sampleLabelMat = cvCreateMat(totalSampleCount, 1, CV_32FC1);//样本标识  
	cvSetZero(sampleLabelMat);  

	cout<<"************************************************************"<<endl;
	cout<<"start to training positive samples..."<<endl;

	//char positiveImgName[256];
	string pospath;
	ifstream posin("sample\\A.txt");
	int i=0;
	while(getline(posin,pospath))
	{
		
		cv::Mat src = cv::imread(pospath);
		Mat img(DST_IMG_HEIGH,DST_IMG_WIDTH,src.type());
		resize(src,img,img.size(),0,0,INTER_LINEAR);
		if( img.data == NULL )
		{
			cout<<"positive image sample load error: "<<i<<" "<<pospath<<endl;
			system("pause");
			continue;
		}

		cv::HOGDescriptor hog(cv::Size(32,32), cv::Size(8,8), cv::Size(4,4), cv::Size(4,4), 9);
		vector<float> featureVec; 

		hog.compute(img, featureVec, cv::Size(4,4));  
		int featureVecSize = featureVec.size();

		for (int j=0; j<featureVecSize; j++)  
		{  		
			CV_MAT_ELEM( *sampleFeaturesMat, float, i, j ) = featureVec[j]; 
		}  
		sampleLabelMat->data.fl[i] = 1;
		i++;
	}

	cout<<"end of training for positive samples..."<<endl;
	cout<<"*********************************************************"<<endl;
	cout<<"start to train negative samples..."<<endl;


	int k =0;
	string negpath;
	ifstream negin("sample\\Aneg.txt");
	while(getline(negin,negpath))
	{
		cv::Mat src = cv::imread(negpath);
		Mat img(DST_IMG_HEIGH,DST_IMG_WIDTH,src.type());
		resize(src,img,img.size(),0,0,INTER_LINEAR);

		if(img.data == NULL)
		{
			cout<<"negative image sample load error: "<<negpath<<endl;
			continue;
		}

		cv::HOGDescriptor hog(cv::Size(32,32), cv::Size(8,8), cv::Size(4,4), cv::Size(4,4), 9);  
		vector<float> featureVec; 

		hog.compute(img,featureVec,cv::Size(4,4));//计算HOG特征
		int featureVecSize = featureVec.size();  

		for ( int j=0; j<featureVecSize;j ++)  
		{  
			CV_MAT_ELEM( *sampleFeaturesMat, float, k+ positiveSampleCount,j ) = featureVec[j];
		}  

		 sampleLabelMat->data.fl[ k + positiveSampleCount] = -1;
		k++;
	}
	
	
	cout<<"end of training for negative samples..."<<endl;
	cout<<"********************************************************"<<endl;
	cout<<"start to train for SVM classifier..."<<endl;


	CvSVMParams params;  
	params.svm_type = CvSVM::C_SVC;  
	params.kernel_type = CvSVM::LINEAR;  
	params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 1000, FLT_EPSILON);
	params.C = 0.01;

	Mysvm svm;
	svm.train( sampleFeaturesMat, sampleLabelMat, NULL, NULL, params ); //用SVM线性分类器训练
	svm.save(classifierSavePath);

	cvReleaseMat(&sampleFeaturesMat);
	cvReleaseMat(&sampleLabelMat);

	int supportVectorSize = svm.get_support_vector_count();
	cout<<"support vector size of SVM:"<<supportVectorSize<<endl;
	cout<<"************************ end of training for SVM ******************"<<endl;

	CvMat *sv,*alp,*re;//所有样本特征向量 
	sv  = cvCreateMat(supportVectorSize , 1764, CV_32FC1);
	alp = cvCreateMat(1 , supportVectorSize, CV_32FC1);
	re  = cvCreateMat(1 , 1764, CV_32FC1);
	CvMat *res  = cvCreateMat(1 , 1, CV_32FC1);

	cvSetZero(sv);
	cvSetZero(re);
  
	for(int i=0; i<supportVectorSize; i++)
	{
		memcpy( (float*)(sv->data.fl+i*1764), svm.get_support_vector(i), 1764*sizeof(float));	
	}

	double* alphaArr = svm.get_alpha();
	int alphaCount = svm.get_alpha_count();

	for(int i=0; i<supportVectorSize; i++)
	{
        alp->data.fl[i] = alphaArr[i];
	}
	cvMatMul(alp, sv, re);

	int posCount = 0;
	for (int i=0; i<1764; i++)
	{
		re->data.fl[i] *= -1;
	}

	FILE* fp = fopen("dector0_20X32.txt","wb");

	if( NULL == fp )
	{
		return ;
	}

	for(int i=0; i<1764; i++)
	{
		fprintf(fp,"%f \n",re->data.fl[i]);
	}
	float rho = svm.get_rho();
	fprintf(fp, "%f", rho);
	cout<<"hogSVMDetector.txt 保存完毕"<<endl;//保存HOG能识别的分类器
	fclose(fp);

	return;
}

void Detect()
{
	vector<float> x;
	ifstream fileIn("dector_circle.txt", ios::in);
	float val = 0.0f;
	while(!fileIn.eof())
	{
		fileIn>>val;
		x.push_back(val);
	}
	fileIn.close();

	vector<cv::Rect>  found;
	cv::HOGDescriptor hog(cv::Size(32,32), cv::Size(8,8), cv::Size(4,4), cv::Size(4,4), 9);
	hog.setSVMDetector(x);
	vector<double>foundweight;
	IplImage* img = NULL;
	img=cvLoadImage("D:\\test\\testori\\00020.ppm");//D:\\test\\testori\\00093.ppm D:\\HOG+SVM-TEST\\pos\\11.jpg

	hog.detectMultiScale(img, found,foundweight,0, cv::Size(4,4), cv::Size(8,8), 1.1, 0);

	if (found.size() > 0)
		{
			for (int i=0; i<found.size(); i++)
			{
				CvRect tempRect = cvRect(found[i].x, found[i].y, found[i].width, found[i].height);
				cvRectangle(img, cvPoint(tempRect.x,tempRect.y),
				cvPoint(tempRect.x+tempRect.width,tempRect.y+tempRect.height),CV_RGB(255,0,0), 1);
			}
		}
	cvNamedWindow("img",1);
	cvShowImage("img",img);
	cvWaitKey(0);
}

int main()
{
	//Train();
	Detect();
}




相关文章推荐

自己训练SVM分类器进行HOG行人检测

正样本来源是INRIA数据集中的96*160大小的人体图片,使用时上下左右都去掉16个像素,截取中间的64*128大小的人体。 负样本是从不包含人体的图片中随机裁取的,大小同样是64*128。 SVM...
  • masikkk
  • masikkk
  • 2013年11月13日 22:51
  • 31735

OpenCV训练自己的人脸检测级连分类器并测试

0. 概述分为如下几步:step1. 制作训练数据集step2. 训练分类器step3. 使用分类器进行分类 1. 准备工作建立一个项目目录objection_detection/$ mkdir ob...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

使用opencv训练自己的检测器(1)

环境:oepncv2.4.9+vs2010(无论何种环境都大同小异) 目的:利用opencv自带的opencv_haartraining.exe训练自己的检测器 步骤:1.首先自建一个文件夹存...

Opencv目标检测之级联分类器训练与测试

OpenCV提供了两个程序可以训练自己的级器opencv_haartraining 与opencv_traincascade。opencv_traincascade是一个新程序,使用OpenCV 2...

我的第一个MFC小程序——文件夹下图片批量灰度化和归一化

程序界面: 在输入路径下输入待处理图片的所在文件夹路径,输出路径下输入保存图片的文件夹路径,选择灰度化或归一化,也可同时选择,输入归一化的宽和高的大小,点击开始处理 待处理图片如下...

手势识别(一)--手势基本概念和ChaLearn Gesture Challenge

以下转自: http://blog.csdn.net/qq1175421841/article/details/50312565 像点击(clicks)是GUI平台的核心,轻点(taps)是触摸平台的...

opencv SVM 训练行人检测器注意事项

事项1:支持向量一个的原因? 如下的解答: 用opencv2.49的我是这么理解的:对于线性SVM,在opencv\sources\modules\ml\src\svm.cpp这个源码的optimiz...

用opencv的traincascade训练检测器

#1,准备正负样本 正样本:可以一张图片上多个sample,也可以一张图片单独成一个sample,准备多个sample。生成描述文件如下所示: 负样本:只要不含正样本,任意图片都可以作为负样本...

使用opencv训练自己的检测器(2)

通过上一篇文章所说的一系列操作,准备好了训练数据,在继续操作。5.用cmd命令行,进入到项目文件夹内。 (例如:cd G:\traing_new_cascade) 然后使用如下命令生成vec文件:...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Opencv 用SVM训练检测器
举报原因:
原因补充:

(最多只允许输入30个字)