public
Last active

OpenCV cv::HOGDescriptor & cv::gpu::HOGDescriptor sample

  • Download Gist
gpu_hog.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include <SDKDDKVer.h>
#include <Windows.h>
 
#pragma warning(disable:4819)
#pragma warning(disable:4996)
 
// for OpenCV2
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/gpu/gpu.hpp"
#include "opencv2/highgui/highgui.hpp"
 
#ifdef _DEBUG
#pragma comment(lib, "opencv_core231d.lib")
#pragma comment(lib, "opencv_imgproc231d.lib")
#pragma comment(lib, "opencv_objdetect231d.lib")
#pragma comment(lib, "opencv_gpu231d.lib")
#pragma comment(lib, "opencv_features2d231d.lib")
#pragma comment(lib, "opencv_highgui231d.lib")
#else
#pragma comment(lib, "opencv_core231.lib")
#pragma comment(lib, "opencv_imgproc231.lib")
#pragma comment(lib, "opencv_objdetect231.lib")
#pragma comment(lib, "opencv_objdetect231d.lib")
#pragma comment(lib, "opencv_gpu231.lib")
#pragma comment(lib, "opencv_features2d231.lib")
#pragma comment(lib, "opencv_highgui231.lib")
#endif
 
//#define USE_VIDEO_CAPTURE
//#define USE_GPU
 
int main(int argc, char** argv)
{
cv::Mat src_img;
 
#ifdef USE_VIDEO_CAPTURE
cv::VideoCapture capture;
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
capture.open(0);
#else
cv::Mat target_img = cv::imread("..\\img\\test_hog.jpg");
#endif
 
#ifdef USE_GPU
cv::gpu::GpuMat src_gpu, mono_gpu;
cv::gpu::HOGDescriptor hog;
hog.setSVMDetector(cv::gpu::HOGDescriptor::getDefaultPeopleDetector());
#else
cv::HOGDescriptor hog;
cv::Mat mono_img;
hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());
#endif
 
std::vector<cv::Rect> found;
while(true) {
#ifdef USE_VIDEO_CAPTURE
capture >> src_img;
#else
src_img = target_img.clone();
#endif
 
#ifdef USE_GPU
src_gpu.upload(src_img);
cv::gpu::cvtColor(src_gpu, mono_gpu, CV_BGR2GRAY);
hog.detectMultiScale(mono_gpu, found);
#else
cv::cvtColor(src_img, mono_img, CV_BGR2GRAY);
hog.detectMultiScale(mono_img, found);
#endif
 
for(unsigned i = 0; i < found.size(); i++) {
cv::Rect r = found[i];
rectangle(src_img, r.tl(), r.br(), cv::Scalar(0,255,0), 2);
}
cv::imshow("test", src_img);
 
int c = cv::waitKey(1);
if( c == 27) break;
}
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.