关闭

Opencv 用SVM训练检测器

标签: 学习Visual Studio 2010C++opencvsvm
1004人阅读 评论(0) 收藏 举报
分类:
用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();
}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:48184次
    • 积分:991
    • 等级:
    • 排名:千里之外
    • 原创:51篇
    • 转载:2篇
    • 译文:0篇
    • 评论:41条
    最新评论