12/16/2015

Opencv Hog+SVM example of pedestrian detection


source code


#include < iostream>    
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\cudaobjdetect.hpp"
#include "opencv2\cudaimgproc.hpp"
#include "opencv2\cudawarping.hpp"


#ifdef _DEBUG               
#pragma comment(lib, "opencv_core300d.lib")       
#pragma comment(lib, "opencv_highgui300d.lib")    
#pragma comment(lib, "opencv_imgcodecs300d.lib")  
#pragma comment(lib, "opencv_objdetect300d.lib")  
#pragma comment(lib, "opencv_imgproc300d.lib")  
#pragma comment(lib, "opencv_videoio300d.lib")    
#pragma comment(lib, "opencv_cudaobjdetect300d.lib")  
#pragma comment(lib, "opencv_cudawarping300d.lib")
#pragma comment(lib, "opencv_cudaimgproc300d.lib")

#else       
#pragma comment(lib, "opencv_core300.lib")       
#pragma comment(lib, "opencv_highgui300.lib")    
#pragma comment(lib, "opencv_imgcodecs300.lib")    
#pragma comment(lib, "opencv_objdetect300.lib")  
#pragma comment(lib, "opencv_imgproc300.lib")  
#pragma comment(lib, "opencv_videoio300.lib")    
#pragma comment(lib, "opencv_cudaobjdetect300.lib")
#pragma comment(lib, "opencv_cudawarping300.lib")
#pragma comment(lib, "opencv_cudaimgproc300.lib")
#endif        

using namespace std;
using namespace cv;


void main()
{


 cv::Ptr< cv::cuda::HOG> d_hog = cv::cuda::HOG::create(Size(48, 96)); //Size(64, 128));// 
 d_hog->setSVMDetector(d_hog->getDefaultPeopleDetector());
 

 //video loading
 Mat img; 
 VideoCapture cap("M:\\____videoSample____\\tracking\\TownCentreXVID.avi");

 //loading test
 cap >> img;
 if (img.empty())
  return;

 //window
 namedWindow("pedestrian", 0);

 //processing time check
 unsigned long AAtime = 0, BBtime = 0;
 
 //resize
 double scale = float(800) / img.cols;
 cuda::GpuMat GpuImg, rGpuImg;
 GpuImg.upload(img);
 cuda::resize(GpuImg, rGpuImg, Size(GpuImg.cols * scale, GpuImg.rows * scale));
 Mat rInimg;
 rGpuImg.download(rInimg);

 while (1)
 {
  //time check
  AAtime = getTickCount();

  //loading
  cap >> img;
  if (img.empty())
   break;

  //resize
  GpuImg.upload(img);
  cuda::resize(GpuImg, rGpuImg, Size(GpuImg.cols * scale, GpuImg.rows * scale));
  rGpuImg.download(rInimg);
  cuda::cvtColor(rGpuImg, rGpuImg, CV_BGR2GRAY);
  
  
  vector< Point> found_locations;
  d_hog->detect(rGpuImg, found_locations);

  std::vector< cv::Rect> found_locations_rect;
  d_hog->detectMultiScale(rGpuImg, found_locations_rect);


  for (int i = 0; i < found_locations_rect.size(); ++i)
  {

   cv::rectangle(rInimg, found_locations_rect[i], CvScalar(0, 0, 255), 1);   
  }


  imshow("pedestrian", rInimg);
  waitKey(10);

  //time check
  BBtime = getTickCount();
  double s_time = (BBtime - AAtime) / getTickFrequency();
  double fps_time = 1/s_time;
  printf("%.2lf sec / %.2lf fps \n", s_time, fps_time); 

 }


}

3 comments:

  1. 안녕하세요 블로그 항상 잘 보고 있습니다.
    object tracking 관련하여 공부하고있는 학생입니다.
    이 코드를 한번 테스트해보고 싶어서 CUDA 를 깔고 opencv랑 연동을 하였는데
    GpuImg, rGpuImg 등에서 빨간줄이 뜨고 아래와 같은 오류가 뜨는데
    no suitable user-defined conversion from "cv::cuda::GpuMat" to "const cv::_InputArray" exists
    CUDA 이외에도 더 설정해주어야 할 것이 있나요? 답변 부탁드립니다 T.T

    ReplyDelete
    Replies
    1. cuda 관련 문제로 보이네요..
      빈 파일에서 cuda::Gpumat 선언, upload, resize, download 와 같은 기본적인 코드로 테스트를 해보세요.

      Delete
    2. 네 감사합니다 좋은하루되세요 :)

      Delete