技术专栏
使用Opencv+SVM+Hog进行行人识别的代码
//��ʾͼ���ļ�
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include<opencv2/ml.hpp>
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<string> img_path;
vector<int> 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
vector<float>descriptors;//�������
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<float>::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)
{
data_mat.at<float>(i, n) = *iter;
n++;
}
res_mat.at<float>(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<string> img_path;
vector<int> 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
vector<float>descriptors;//�������
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<float>::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)
{
data_mat.at<float>(i, n) = *iter;
n++;
}
res_mat.at<float>(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<string> 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
vector<float>descriptors;//�������
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<float>::iterator iter = descriptors.begin(); iter != descriptors.end(); iter++)
{
svm_mat.at<float>(i, n) = *iter;
n++;
}
int ret = svm.predict(svm_mat);
printf("predict:%d | path:%s\n", ret, img_test_path[i].c_str());
}
}
声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
红包
1
收藏
评论
打赏
- 分享
- 举报
评论
0个
手气红包
暂无数据
相关专栏
-
浏览量:932次2023-09-25 10:59:14
-
浏览量:602次2024-03-05 16:55:32
-
浏览量:747次2023-02-16 11:18:38
-
浏览量:1420次2022-10-18 10:21:42
-
浏览量:671次2023-11-09 13:58:15
-
浏览量:2305次2022-05-13 10:46:47
-
浏览量:1969次2023-02-14 20:27:20
-
浏览量:608次2023-09-11 18:04:33
-
浏览量:470次2024-01-30 09:57:47
-
浏览量:4478次2021-04-19 14:55:34
-
浏览量:868次2024-03-14 18:20:47
-
浏览量:171次2023-08-15 23:15:48
-
浏览量:1992次2018-01-17 12:06:58
-
浏览量:270次2023-07-30 18:35:03
-
浏览量:1364次2023-03-15 09:05:04
-
浏览量:7596次2021-05-19 16:25:40
-
浏览量:2060次2023-02-17 11:37:20
-
浏览量:6000次2021-07-28 14:21:28
-
浏览量:1894次2022-05-24 17:40:33
置顶时间设置
结束时间
删除原因
-
广告/SPAM
-
恶意灌水
-
违规内容
-
文不对题
-
重复发帖
打赏作者
正年华🍀
您的支持将鼓励我继续创作!
打赏金额:
¥1
¥5
¥10
¥50
¥100
支付方式:
微信支付
打赏成功!
感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
审核成功
发布时间设置
发布时间:
请选择发布时间设置
是否关联周任务-专栏模块
审核失败
失败原因
请选择失败原因
备注
请输入备注