opencv行人检测

向右看齐 2022-06-03 02:54 302阅读 0赞

行人检测是视觉领域很热也很有用的一个主题,特别是在无人驾驶中,行人检测的重要性不言而喻。

在之前进行了人脸检测之后,行人检测就显得简单多了。过程大致与人脸检测一样,都是先加载分类器,然后进行多尺度检测。就偷懒不再赘述。感兴趣的可以看人脸检测的这一篇文章:OpenCV实践之路——人脸检测(C++/Python)

图片检测

  1. #include<opencv2/highgui/highgui.hpp>
  2. #include<opencv2/imgproc/imgproc.hpp>
  3. #include<opencv2/objdetect.hpp>
  4. #include<iostream>
  5. using namespace std;
  6. using namespace cv;
  7. int main(int argc, char **argv)
  8. {
  9. Mat img;
  10. vector<Rect> found;
  11. /*if (argc != 2) {
  12. printf("can't find picture\n");
  13. return -1;
  14. }*/
  15. img = imread("F:\\1.jpg",1);
  16. HOGDescriptor defaultHog;
  17. defaultHog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
  18. //默认模型,这个模型数据在OpenCV源码中是一堆常量数字,
  19. //这些数字是通过原作者提供的行人样本INRIAPerson.tar训练得到的
  20. defaultHog.detectMultiScale(img, found);
  21. // 画出长方形,框出人
  22. for (int i = 0; i < found.size(); i++) {
  23. Rect r = found[i];
  24. rectangle(img, r.tl(), r.br(), Scalar(0, 0, 255), 3);
  25. }
  26. namedWindow("Detect pedestrain", WINDOW_AUTOSIZE);
  27. imshow("Detect pedestrain", img);
  28. char c = waitKey(0);
  29. return 0;
  30. }

结果:

Center

视频检测代码

  1. #include<opencv2/highgui/highgui.hpp>
  2. #include<opencv2/imgproc/imgproc.hpp>
  3. #include<opencv2/objdetect.hpp>
  4. #include<iostream>
  5. using namespace std;
  6. using namespace cv;
  7. int main(int argc, char **argv)
  8. {
  9. Mat img;
  10. VideoCapture
  11. cap;
  12. vector<Rect> found;
  13. if(argc != 2) {
  14. printf("can't find picture\n");
  15. return -1;
  16. }
  17. cap.open(argv[1]);
  18. //img = cv::imread(argv[1]);
  19. while(1) {
  20. cap >> img;
  21. if (img.empty())
  22. break;
  23. HOGDescriptor defaultHog;
  24. defaultHog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
  25. //默认模型,这个模型数据在OpenCV源码中是一堆常量数字,
  26. //这些数字是通过原作者提供的行人样本INRIAPerson.tar训练得到的
  27. defaultHog.detectMultiScale(img, found);
  28. // 画出长方形,框出人
  29. for (int i = 0; i < found.size(); i++) {
  30. Rect r = found[i];
  31. rectangle(img, r.tl(), r.br(), Scalar(0, 0, 255), 3);
  32. }
  33. namedWindow("Detect pedestrain", WINDOW_AUTOSIZE);
  34. imshow("Detect pedestrain", img);
  35. char c = waitKey(33);
  36. if (c == 27)
  37. break;
  38. }
  39. return 0;
  40. }

发表评论

表情:
评论列表 (有 0 条评论,302人围观)

还没有评论,来说两句吧...

相关阅读

    相关 opencv行人检测

    行人检测是视觉领域很热也很有用的一个主题,特别是在无人驾驶中,行人检测的重要性不言而喻。 在之前进行了人脸检测之后,行人检测就显得简单多了。过程大致与人脸检测一样,都是先加载