使用Opencv+SVM+Hog进行行人识别的代码

//��ʾͼ���ļ�

#include

#include

#include

#include

#include

#include

using namespace std;

using namespace cv;

#pragma comment(linker, “/subsystem:\”windows\” /entry:\”mainCRTStartup\””)

void train_data(const char* data_path,const char* save_path);

void svm_test(const char* svn_data_path, const char* test_data_path);

int main()

{

train_data(“Resource/train_data.txt”,”svm_data.xml”);

return 1;

vector img_path;

vector img_label;

const char* air_label = “airplanes”;

const char* train_dir_path = “Resource/train_images”;

char data_path[128] = {0};

sprintf(data_path, “%s/%s.txt”, train_dir_path, air_label);

ifstream svm_data(data_path);

if (svm_data.fail())return -1;

string fileName;

while (getline(svm_data, fileName))

{

char full_path[128] = { 0 };

sprintf(full_path, “%s/%s/%s”, train_dir_path, air_label, fileName.c_str());

printf(“%s\n”, full_path);

img_path.push_back(string(full_path));

}

svm_data.close();

Mat data_mat, res_mat;

int nImgNum = img_path.size();

res_mat = Mat::zeros(nImgNum, 1, CV_32FC1);

Mat src;

Mat trainImg = Mat::zeros(64, 64, CV_8UC3);//��Ҫ������ͼƬ

for (string::size_type i = 0; i != img_path.size(); i++)

{

src = imread(img_path[i].c_str(), 1);

resize(src, trainImg, Size(64, 64), 0, 0, INTER_CUBIC);

HOGDescriptor hog = HOGDescriptor(cvSize(64, 64), cvSize(16, 16), cvSize(8, 8), cvSize(8, 8), 9); //������˼���ο�����1,2

vectordescriptors;//�������

hog.compute(trainImg, descriptors, Size(1, 1), Size(0, 0)); //���ü��㺯����ʼ����

if (i == 0)

{

data_mat = Mat::zeros(nImgNum, descriptors.size(), CV_32FC1); //��������ͼƬ��С���з���ռ�

}

int n = 0;

for (vector::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)

{

data_mat.at(i, n) = *iter;

n++;

}

res_mat.at(i, 0) = i%2;

}

CvSVM svm;//�½�һ��SVM

CvSVMParams param;//�����Dz���

CvTermCriteria criteria;

criteria = cvTermCriteria(CV_TERMCRIT_EPS, 1000, FLT_EPSILON);

param = CvSVMParams(CvSVM::C_SVC, CvSVM::RBF, 10.0, 0.09, 1.0, 10.0, 0.5, 1.0, NULL, criteria);

/*

SVM���ࣺCvSVM::C_SVC

Kernel�����ࣺCvSVM::RBF

degree��10.0���˴β�ʹ�ã�

gamma��8.0

coef0��1.0���˴β�ʹ�ã�

C��10.0

nu��0.5���˴β�ʹ�ã�

p��0.1���˴β�ʹ�ã�

Ȼ���ѵ���������滯������������CvMat�͵������

*/

//����������(5)SVMѧϰ�������������

svm.train(data_mat, res_mat, Mat(), Mat(), param);//ѵ����

//�������ѵ�����ݺ�ȷ����ѧϰ����,����SVMѧϰ�����

svm.save(“SVM_DATA.xml”);

return 1;

//const char *pstrImageName = “Resource/train_images/airplanes/image_0001.jpg”;

//const char *pstrWindowsTitle = “OpenCV”;

////���ļ��ж�ȡͼ��

//IplImage *pImage = cvLoadImage(pstrImageName, CV_LOAD_IMAGE_UNCHANGED);

////��������

//cvNamedWindow(pstrWindowsTitle, CV_WINDOW_AUTOSIZE);

////��ָ����������ʾͼ��

//cvShowImage(pstrWindowsTitle, pImage);

////�ȴ������¼�

//cvWaitKey();

//cvDestroyWindow(pstrWindowsTitle);

//cvReleaseImage(&pImage);

return 0;

}

void train_data(const char* data_path, const char* save_path)

{

vector img_path;

vector img_label;

int index = 0;

ifstream svm_data(data_path);

if (svm_data.fail())return;

string line;

while (getline(svm_data, line))

{

if (index % 2 == 0)

{

img_label.push_back(atoi(line.c_str()));

}

else

{

img_path.push_back(line);

}

index++;

}

svm_data.close();

Mat data_mat, res_mat;

int nImgNum = img_label.size();

res_mat = Mat::zeros(nImgNum, 1, CV_32FC1);

Mat src;

Mat trainImg = Mat::zeros(64, 64, CV_8UC3);//��Ҫ������ͼƬ

for (string::size_type i = 0; i != nImgNum; i++)

{

src = imread(img_path[i].c_str(), 1);

resize(src, trainImg, Size(64, 64), 0, 0, INTER_CUBIC);

HOGDescriptor hog = HOGDescriptor(cvSize(64, 64), cvSize(16, 16), cvSize(8, 8), cvSize(8, 8), 9); //������˼���ο�����1,2

vectordescriptors;//�������

hog.compute(trainImg, descriptors, Size(1, 1), Size(0, 0)); //���ü��㺯����ʼ����

if (i == 0)

{

data_mat = Mat::zeros(nImgNum, descriptors.size(), CV_32FC1); //��������ͼƬ��С���з���ռ�

}

int n = 0;

for (vector::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)

{

data_mat.at(i, n) = *iter;

n++;

}

res_mat.at(i, 0) = img_label[i];

}

CvSVM svm;//�½�һ��SVM

CvSVMParams param;//�����Dz���

CvTermCriteria criteria;

criteria = cvTermCriteria(CV_TERMCRIT_EPS, 1000, FLT_EPSILON);

param = CvSVMParams(CvSVM::C_SVC, CvSVM::RBF, 10.0, 0.09, 1.0, 10.0, 0.5, 1.0, NULL, criteria);

/*

SVM���ࣺCvSVM::C_SVC

Kernel�����ࣺCvSVM::RBF

degree��10.0���˴β�ʹ�ã�

gamma��8.0

coef0��1.0���˴β�ʹ�ã�

C��10.0

nu��0.5���˴β�ʹ�ã�

p��0.1���˴β�ʹ�ã�

Ȼ���ѵ���������滯������������CvMat�͵������

*/

//����������(5)SVMѧϰ�������������

svm.train(data_mat, res_mat, Mat(), Mat(), param);//ѵ����

//�������ѵ�����ݺ�ȷ����ѧϰ����,����SVMѧϰ�����

svm.save(save_path);

}

void svm_test(const char* svm_data_path, const char* test_data_path)

{

CvSVM svm;

svm.load(svm_data_path);

vector img_test_path;

ifstream img_path_input(test_data_path);

if (img_path_input.fail())return;

string line;

while (getline(img_path_input,line))

{

img_test_path.push_back(line);

}

int nImgNum = img_test_path.size();

for (string::size_type i = 0; i != nImgNum; i++)

{

Mat src = imread(img_test_path[i].c_str(), 1);

Mat trainImg = Mat::zeros(64, 64, CV_8UC3);

resize(src, trainImg, Size(64, 64), 0, 0, INTER_CUBIC);

HOGDescriptor hog = HOGDescriptor(cvSize(64, 64), cvSize(16, 16), cvSize(8, 8), cvSize(8, 8), 9); //������˼���ο�����1,2

vectordescriptors;//�������

hog.compute(trainImg, descriptors, Size(1, 1), Size(0, 0)); //���ü��㺯����ʼ����

Mat svm_mat = Mat::zeros(nImgNum, descriptors.size(), CV_32FC1);

int n = 0;

for (vector::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)

{

svm_mat.at(i, n) = *iter;

n++;

}

int ret = svm.predict(svm_mat);

printf(“predict:%d | path:%s\n”, ret, img_test_path[i].c_str());

}

}
<

免责声明:文章内容来自互联网,本站不对其真实性负责,也不承担任何法律责任,如有侵权等情况,请与本站联系删除。
转载请注明出处:使用Opencv+SVM+Hog进行行人识别的代码 https://www.yhzz.com.cn/a/14851.html

上一篇 2023-05-12 11:49:17
下一篇 2023-05-12 11:54:11

相关推荐

联系云恒

在线留言: 我要留言
客服热线:400-600-0310
工作时间:周一至周六,08:30-17:30,节假日休息。