#include "iostream"
#include "queue"
using namespace std;
#include "opencv2/opencv.hpp"
#include "Windows.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
using namespace cv; int main()
{
try{ IplImage *pFrame = NULL;
CvCapture *pCapture = NULL;
//pCapture = cvCreateCameraCapture(-1);
//pCapture = cvCaptureFromCAM(0);
pCapture = cvCaptureFromFile("C:\\C_C++ code\\Photo and video\\TextVideo2.flv");
//pCapture = cvCaptureFromFile("C:\\C_C++ code\\Photo and video\\TextVideo1.flv");
if (!pCapture)
{
cout << "File opened fail..." << endl;
return -;
} Mat img;
HOGDescriptor hog;
Rect r;
int nNum = ;
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
vector<Rect> found,found1;
int i, j;
char str[];
while (pFrame = cvQueryFrame(pCapture))
{
nNum++;
Mat img = cvarrToMat(pFrame, ); //IplImage turn into Mat if (nNum >= )
{
//进行检测
hog.detectMultiScale(img, found); found1.clear();
//-------------------去除嵌套的矩形框------------------------
for (i = ; i < found.size(); i++)
{
r = found[i];
for (j = ; j < found.size(); j++)
{
if ( i != j && ((r&found[j]) == r) )
{
break;
}
}
if (j == found.size())
{
found1.push_back(r);
}
}
//画长方形 框出行人 for (i = ; i < found1.size(); i++)
{
r = found1[i];
rectangle(img, r, Scalar(, , ), );
}
nNum = ;
} for (int i = ; i < found1.size(); i++)
{
r = found1[i];
rectangle(img, r, Scalar(, , ), );
}
sprintf(str, "The track count is: %d", found1.size()); putText(img, str, cvPoint(, ), CV_FONT_HERSHEY_PLAIN, 0.8,CV_RGB(, , ),,); imshow("Track People", img);
if (cvWaitKey() >= )
break;
}
}
catch (exception &e)
{
cout << e.what() << endl;
} return ;
}

效果:

OpenCV人形检测Hog-LMLPHP

图片人形测试:

#include "iostream"
#include "queue"
using namespace std;
#include "opencv2/opencv.hpp"
#include "Windows.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
int main(int argc, char** argv){
Mat img;
vector<Rect> found; img = imread("C:\\C_C++ code\\Photo and video\\text006.jpg"); HOGDescriptor defaultHog;
defaultHog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); //进行检测
defaultHog.detectMultiScale(img, found); //画长方形,框出行人
for (int i = ; i < found.size(); i++){
Rect r = found[i];
rectangle(img, r, Scalar(, , ), );
}
namedWindow("检测行人", CV_WINDOW_AUTOSIZE);
imshow("检测行人", img); waitKey(); return ;
}

OpenCV人形检测Hog-LMLPHP

边框嵌套去重:

int main(int argc, char** argv){
Mat img;
vector<Rect> found, foundRect; img = imread("C:\\C_C++ code\\Photo and video\\text007.jpg"); HOGDescriptor defaultHog;
defaultHog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); //进行检测
defaultHog.detectMultiScale(img, found); //遍历found寻找没有被嵌套的长方形
for (int i = ; i < found.size(); i++){
Rect r = found[i]; int j = ;
for (; j < found.size(); j++){
//如果时嵌套的就推出循环
if (j != i && (r & found[j]) == r)
break;
}
if (j == found.size()){
foundRect.push_back(r);
}
} //画长方形,圈出行人
for (int i = ; i < foundRect.size(); i++){
Rect r = foundRect[i];
rectangle(img, r.tl(), r.br(), Scalar(, , ), );
} namedWindow("检测行人", CV_WINDOW_AUTOSIZE);
imshow("检测行人", img); waitKey();
return ;
}
int main()
{
Mat image = imread("C:\\C_C++ code\\Photo and video\\text007jpg");
imshow("hog", image);
if (image.empty())
{
cout << "read image failed" << endl;
}
// 1. 定义HOG对象
HOGDescriptor hog(Size(,), Size(, ), Size(, ), Size(, ), ); // 2. 设置SVM分类器
hog.setSVMDetector(HOGDescriptor::getDaimlerPeopleDetector()); // 采用已经训练好的行人检测分类器 // 3. 在测试图像上检测行人区域
std::vector<cv::Rect> regions;
hog.detectMultiScale(image, regions, , cv::Size(, ), cv::Size(, ), 1.05, ); // 显示
for (size_t i = ; i < regions.size(); i++)
{
cv::rectangle(image, regions[i], cv::Scalar(, , ), );
} cv::imshow("hog", image);
cv::waitKey(); return ;
}
04-26 14:47