// ConsoleApplication48.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
using namespace cv::ml;
void svm_hog_detect();
int main()
{
//train_svm_hog();
svm_hog_detect();
return 0;
}
void svm_hog_detect()
{
//HOG检测器,用来计算HOG描述子的
//检测窗口(48,48),块尺寸(16,16),块步长(8,8),cell尺寸(8,8),直方图bin个数9
//HOGDescriptor hog(Size(48, 48), Size(16, 16), Size(8, 8), Size(8, 8), 9);
HOGDescriptor hog(Size(64, 128), Size(16, 16), Size(8, 8), Size(8, 8), 9);
//HOG描述子的维数,由图片大小、检测窗口大小、块大小、细胞单元中直方图bin个数决定
int DescriptorDim;
//从XML文件读取训练好的SVM模型
Ptr
if (svm->empty())
{
cout << "load svm detector failed!!!" << endl;
return;
}
//特征向量的维数,即HOG描述子的维数
DescriptorDim = svm->getVarCount();
//获取svecsmat,元素类型为float
Mat svecsmat = svm->getSupportVectors();
//特征向量维数
int svdim = svm->getVarCount();
int numofsv = svecsmat.rows;
//alphamat和svindex必须初始化,否则getDecisionFunction()函数会报错
Mat alphamat = Mat::zeros(numofsv, svdim, CV_32F);
Mat svindex = Mat::zeros(1, numofsv, CV_64F);
Mat Result;
double rho = svm->getDecisionFunction(0, alphamat, svindex);
//将alphamat元素的数据类型重新转成CV_32F
alphamat.convertTo(alphamat, CV_32F);
Result = -1 * alphamat * svecsmat;
vector
for (int i = 0; i < svdim; ++i)
{
vec.push_back(Result.at
}
vec.push_back(rho);
//saving HOGDetectorForOpenCV.txt
ofstream fout("HOGDetectorForOpenCV.txt");
for (int i = 0; i < vec.size(); ++i)
{
fout << vec[i] << endl;
}
hog.setSVMDetector(vec);
/**************读入视频进行HOG检测******************/
//VideoCapture capture("demo.avi");
//if (!capture.isOpened())
//{
// cout << "Read video Failed !" << endl;
// return;
// }
//Mat frame;
//int frame_num = capture.get(CAP_PROP_FRAME_COUNT);
//cout << "total frame number is: " << frame_num << endl;
//int width = capture.get(CAP_PROP_FRAME_WIDTH);
//int height = capture.get(CAP_PROP_FRAME_HEIGHT);
//VideoWriter out;
//用于保存检测结果
//out.open("road result.mp4", CV_FOURCC('D', 'I', 'V', 'X'), 25.0, Size(width / 2, height / 2), true);
//for (int i = 0; i < frame_num; ++i)
//{
// capture >> frame;
//resize(frame, frame, Size(width / 2, height / 2));
Mat frame = imread("1.jpg");
resize(frame, frame, Size(frame.cols / 2, frame.rows / 2));
//目标矩形框数组
vector
//对图片进行多尺度检测
//hog.detectMultiScale(frame, found, 0, Size(8, 8), Size(16, 16), 1.1, 2);
hog.detectMultiScale(frame, found, 0, Size(8, 8), Size(32, 32), 1.05, 2);
for (int i = 0; i
if (found[i].x > 0 && found[i].y > 0 && (found[i].x + found[i].width)< frame.cols
&& (found[i].y + found[i].height)< frame.rows)
found_1.push_back(found[i]);
}
//找出所有没有嵌套的矩形框r,并放入found_filtered中,如果有嵌套的话,
//则取外面最大的那个矩形框放入found_filtered中
for (int i = 0; i < found_1.size(); i++)
{
Rect r = found_1[i];
int j = 0;
for (; j < found_1.size(); j++)
if (j != i && (r & found_1[j]) == found_1[j])
break;
if (j == found_1.size())
found_filtered.push_back(r);
}
//画矩形框,因为hog检测出的矩形框比实际目标框要稍微大些,所以这里需要做一些调整,可根据实际情况调整
for (int i = 0; i
Rect r = found_filtered[i];
//将检测矩形框缩小后绘制,根据实际情况调整
r.x += cvRound(r.width*0.1);
r.width = cvRound(r.width*0.8);
r.y += cvRound(r.height*0.07);
r.height = cvRound(r.height*0.8);
}
for (int i = 0; i
Rect r = found_filtered[i];
rectangle(frame, r.tl(), r.br(), Scalar(0, 0, 255), 2);
}
imshow("detect result", frame);
cvWaitKey();
//保存检测结果
// out << frame;
/*if (waitKey(30) == 'q')
{
break;
}*/
//}
}