【OpenCV】如何使用HOG特征对行人检测

举报
王博Kings 发表于 2020/12/29 22:46:09 2020/12/29
【摘要】 代码示例 #include <string> #include <iostream> #include <algorithm> #include <iterator> #include <stdio.h>#include <string.h>#include <ctype.h> #include "opencv2/core/core.hpp"#include"openc...

代码示例


  
  1. #include <string>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <iterator>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include "opencv2/core/core.hpp"
  9. #include"opencv2/highgui/highgui.hpp"
  10. #include "opencv2/imgproc/imgproc.hpp"
  11. #include "opencv2/objdetect/objdetect.hpp"
  12. #include <vector>
  13. #include <stdio.h>
  14. using namespace cv;
  15. using namespace std;
  16. //int main(int argc, char** argv){
  17. // Mat img;
  18. // vector<Rect> found, foundRect;
  19. //
  20. // //img = imread(argv[1]);
  21. //if (argc != 2 || !img.data){
  22. // printf("没有图片\n");
  23. // return -1;
  24. //}
  25. int main()
  26. {
  27. Mat img;
  28. vector<Rect> found, foundRect;
  29. img = imread("test.png");
  30. imshow("【原图】", img);
  31. cout << "读进去图片了" << endl;
  32. if (!img.data){
  33. printf("没有图片\n");
  34. return -1;
  35. }
  36. cout << "设置特征" << endl;
  37. HOGDescriptor defaultHog;
  38. defaultHog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
  39. //进行检测
  40. cout << "进行检验" << endl;
  41. defaultHog.detectMultiScale(img, found);
  42. //遍历found寻找没有被嵌套的长方形
  43. for (int i = 0; i < found.size(); i++){
  44. Rect r = found[i];
  45. int j = 0;
  46. for (; j < found.size(); j++){
  47. //如果时嵌套的就推出循环
  48. if (j != i && (r & found[j]) == r)
  49. break;
  50. }
  51. if (j == found.size()){
  52. foundRect.push_back(r);
  53. }
  54. cout << "寻找嵌套: " <<i<< endl;
  55. }
  56. //画长方形,圈出行人
  57. for (int i = 0; i < foundRect.size(); i++){
  58. Rect r = foundRect[i];
  59. rectangle(img, r.tl(), r.br(), Scalar(0, 0, 255), 3);
  60. cout << "画出方框: " << i << endl;
  61. }
  62. cout << "显示结果"<< endl;
  63. namedWindow("检测行人", CV_WINDOW_AUTOSIZE);
  64. imshow("检测行人", img);
  65. waitKey(0);
  66. return 0;
  67. }

 

 

文章来源: kings.blog.csdn.net,作者:人工智能博士,版权归原作者所有,如需转载,请联系作者。

原文链接:kings.blog.csdn.net/article/details/99758018

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。