Opencv中的DPM应用

使用Opencv的DPM算法进行检测行人,并提取存储

 

 

使用环境:win10+opencv3.4.0+opencv_contrib3.4.0+VS2017

 

环境配置方法: 参考该博客 https://www.cnblogs.com/aiwuzhi/p/7334514.html

 

本文代码GitHub地址:

---

待更新

---

 

 

使用方法: 

编译本项目后,有三个参数:

model_path : 使用的模型,使用的模型在Opencv_contrib的包里
image_dir : 图片所在目录
image_list : 所处理文件名的文件

注意const string model_path = "D:\dpm\inriaperson.xml"; 是固定的
需要设置你的inriaperson.xml所在目录

编译完成后,可以只行exe文件

例: 

PedestrianDetectorbyDPM.exe D:\dataset D:\test\files

第一个参数给image_dir,第二个参数给image_list

 


 
 
  1. #include "dpm.hpp"
  2. #include <opencv2/core.hpp>
  3. #include <opencv2/imgproc.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include <stdio.h>
  6. #include <iostream>
  7. #include <fstream>
  8. #include<vector>
  9. #include <io.h>
  10. #include <string>
  11. #include <sstream>
  12. using namespace cv;
  13. using namespace cv::dpm;
  14. using namespace std;
  15. const string model_path = "D:\\dpm\\inriaperson.xml";
  16. int save_results(const string id, const vector<DPMDetector::ObjectDetection> ds, ofstream &out);
  17. static void help()
  18. {
  19. cout << "\nThis example shows object detection on image sequences using \"Deformable Part-based Model (DPM) cascade detection API\n"
  20. "Call:\n"
  21. "./example_dpm_cascade_detect_sequence <model_path> <image_dir>\n"
  22. "The image names has to be provided in \"files.txt\" under <image_dir>.\n"
  23. << endl;
  24. }
  25. static bool readImageLists(const string &file, vector<string> &imgFileList)
  26. {
  27. ifstream in(file.c_str(), ios::binary);
  28. if (in.is_open())
  29. {
  30. while (in)
  31. {
  32. string line;
  33. getline(in, line);
  34. imgFileList.push_back(line);
  35. }
  36. return true;
  37. }
  38. else
  39. {
  40. cerr << "Invalid image index file: " << file << endl;
  41. return false;
  42. }
  43. }
  44. void drawBoxes(Mat &frame,
  45. vector<DPMDetector::ObjectDetection> ds,
  46. Scalar color,
  47. string text);
  48. void storeBoxes(Mat &frame, \
  49. vector<DPMDetector::ObjectDetection> ds,
  50. Scalar color,
  51. string text,
  52. size_t i,
  53. string output_dir);
  54. bool isInside(Rect rect1, Rect rect2);
  55. void getFiles(string path, vector<string>& files);
  56. vector< string> getFilesCompatible( string cate_dir);
  57. int main(int argc, char** argv)
  58. {
  59. const char* keys =
  60. {
  61. "{@model_path | | Path of the DPM cascade model}"
  62. "{@image_dir | | Directory of the images }"
  63. };
  64. CommandLineParser parser(argc, argv, keys);
  65. //args
  66. /*string model_path = "D:\\test\\dpm\\inriaperson.xml";
  67. string image_dir = "D:\\dataset";
  68. string image_list = "D:\\test\\files";*/
  69. string image_dir = argv[ 1];
  70. string image_list = argv[ 1];
  71. string output_dir = argv[ 2];
  72. cout << "input dir is: "<< image_dir << endl;
  73. cout << "output dir is: "<<output_dir << endl;
  74. if (model_path.empty() || image_dir.empty())
  75. {
  76. help();
  77. return -1;
  78. }
  79. //vector<string> imgFileList;
  80. //if (!readImageLists(image_list, imgFileList))
  81. // return -1;
  82. //string imgfile = "..\\srcImg";
  83. vector< string>imgFileList;
  84. /*getFiles(image_list, imgFileList);
  85. for (int i = 0; i<imgFileList.size(); i++) {
  86. cout << imgFileList.at(i) << endl;
  87. }*/
  88. imgFileList = getFilesCompatible(image_dir);
  89. for ( int t = 0; t<imgFileList.size(); t++)
  90. {
  91. cout << imgFileList[t] << endl;
  92. }
  93. cout << "**********************************" << endl;
  94. //for (int k = 0; k < imgFileList.size()-1; k++)
  95. //{
  96. // cout << imgFileList[k].size() << ' ';
  97. //}
  98. //
  99. //cout << endl;
  100. //cout << (int)imgFileList[9].size() << endl;
  101. #ifdef HAVE_TBB
  102. cout << "Running with TBB" << endl;
  103. #else
  104. #ifdef _OPENMP
  105. cout << "Running with OpenMP" << endl;
  106. #else
  107. cout << "Running without OpenMP and without TBB" << endl;
  108. #endif
  109. #endif
  110. cv::Ptr<DPMDetector> detector = \
  111. DPMDetector::create( vector< string>( 1, model_path));
  112. //namedWindow("DPM Cascade Detection", 1);
  113. // the color of the rectangle
  114. Scalar color(0, 255, 255); // yellow
  115. Mat frame;
  116. for ( size_t i = 0; i < imgFileList.size() -1; i++)
  117. {
  118. double t = ( double)getTickCount();
  119. vector<DPMDetector::ObjectDetection> ds;
  120. //gstring imageFile = image_dir + "\\" + imgFileList[i];
  121. string imageFile = imgFileList[i];
  122. //getchar();
  123. Mat image = imread(imageFile);
  124. /*if (image.empty()) {
  125. cerr << "\nInvalid image:\n" << imgFileList[i] << endl;
  126. }
  127. imshow("show", image);*/
  128. frame = image.clone();
  129. if (image.empty()) {
  130. cerr << "\nInvalid image:\n" << imgFileList[i] << endl;
  131. return -1;
  132. }
  133. // detection
  134. detector->detect(image, ds);
  135. // compute frame per second (fps)
  136. t = (( double)getTickCount() - t) / getTickFrequency(); //elapsed time
  137. cout << t << endl;
  138. // draw boxes
  139. string text = format( "%0.1f fps", 1.0 / t);
  140. //drawBoxes(frame, ds, color, text);
  141. storeBoxes(frame, ds, color, text, i, output_dir);
  142. }
  143. system( "pause");
  144. return 0;
  145. }
  146. void drawBoxes(Mat &frame, \
  147. vector<DPMDetector::ObjectDetection> ds, Scalar color, string text)
  148. {
  149. for ( unsigned int i = 0; i < ds.size(); i++)
  150. {
  151. rectangle(frame, ds[i].rect, color, 2);
  152. Mat dst = frame(ds[i].rect);
  153. imshow( "cut", dst); //注意需要加waitkey,否则会只显示最后切割的图片
  154. waitKey( 0);
  155. }
  156. // draw text on image
  157. Scalar textColor(0, 0, 250);
  158. putText(frame, text, Point( 10, 50), FONT_HERSHEY_PLAIN, 2, textColor, 2);
  159. }
  160. void storeBoxes(Mat &frame, \
  161. vector<DPMDetector::ObjectDetection> ds, Scalar color, string text, size_t i, string output_dir)
  162. {
  163. for ( unsigned int j = 0; j < ds.size(); j++)
  164. {
  165. //rectangle(frame, ds[j].rect, color, 0);
  166. Rect frame_rect(0, 0, frame.cols, frame.rows);
  167. if (isInside(ds[j].rect, frame_rect))
  168. {
  169. Mat dst = frame(ds[j].rect);
  170. char ImagePathName[ 100];
  171. sprintf_s(ImagePathName, "%s\\%zd_%d%s", output_dir.c_str(), i, j, ".jpg"); //指定保存路径
  172. cout << "generate image:" << ImagePathName << endl;
  173. imwrite(ImagePathName, dst); //保存图像
  174. }
  175. }
  176. }
  177. bool isInside(Rect rect1, Rect rect2)
  178. {
  179. return (rect1 == (rect1&rect2));
  180. }
  181. void getFiles(string path, vector<string>& files)
  182. {
  183. //文件句柄
  184. intptr_t hFile = 0;
  185. //文件信息
  186. struct _finddata_t fileinfo;
  187. string p;
  188. if ((hFile = _findfirst(p.assign(path).append( "\\*").c_str(), &fileinfo)) != -1)
  189. {
  190. do
  191. {
  192. //如果是目录,迭代之
  193. //如果不是,加入列表
  194. if ((fileinfo.attrib & _A_SUBDIR))
  195. {
  196. if ( strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
  197. getFiles(p.assign(path).append( "\\").append(fileinfo.name), files);
  198. }
  199. else
  200. {
  201. files.push_back(p.assign(path).append( "\\").append(fileinfo.name));
  202. }
  203. } while (_findnext(hFile, &fileinfo) == 0);
  204. _findclose(hFile);
  205. }
  206. }
  207. vector< string> getFilesCompatible( string cate_dir)
  208. {
  209. vector< string> files; //存放文件名
  210. cout << "^^^^^^^^^^^^^^^^^" << endl;
  211. #ifdef _WIN64
  212. struct _finddata_t file;
  213. intptr_t lf= 0;
  214. //输入文件夹路径
  215. string p;
  216. if ((lf = _findfirst(p.assign(cate_dir).append( "\\*").c_str(), &file)) == -1) {
  217. cout << cate_dir << " not found!!!" << endl;
  218. }
  219. else {
  220. do
  221. {
  222. //如果是目录,迭代之
  223. //如果不是,加入列表
  224. if ((file.attrib & _A_SUBDIR))
  225. {
  226. if ( strcmp(file.name, ".") != 0 && strcmp(file.name, "..") != 0)
  227. getFiles(p.assign(cate_dir).append( "\\").append(file.name), files);
  228. }
  229. else
  230. {
  231. files.push_back(p.assign(cate_dir).append( "\\").append(file.name));
  232. }
  233. } while (_findnext(lf, &file) == 0);
  234. }
  235. _findclose(lf);
  236. #endif
  237. #ifdef linux
  238. DIR *dir;
  239. struct dirent *ptr;
  240. char base[ 1000];
  241. if ((dir = opendir(cate_dir.c_str())) == NULL)
  242. {
  243. perror( "Open dir error...");
  244. exit( 1);
  245. }
  246. while ((ptr = readdir(dir)) != NULL)
  247. {
  248. if ( strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) ///current dir OR parrent dir
  249. continue;
  250. else if (ptr->d_type == 8) ///file
  251. //printf("d_name:%s/%s\n",basePath,ptr->d_name);
  252. files.push_back(ptr->d_name);
  253. else if (ptr->d_type == 10) ///link file
  254. //printf("d_name:%s/%s\n",basePath,ptr->d_name);
  255. continue;
  256. else if (ptr->d_type == 4) ///dir
  257. {
  258. files.push_back(ptr->d_name);
  259. /*
  260. memset(base,'\0',sizeof(base));
  261. strcpy(base,basePath);
  262. strcat(base,"/");
  263. strcat(base,ptr->d_nSame);
  264. readFileList(base);
  265. */
  266. }
  267. }
  268. closedir(dir);
  269. #endif
  270. //排序,按从小到大排序
  271. sort(files.begin(), files.end());
  272. return files;
  273. }

 

 

 

——————

 

效果:

待更新

 

可能遇到的问题:

 

待更新

 

 

——————

 

输入:图片路径,输出:目标图片

 

 

参考代码:

https://www.cnblogs.com/louyihang-loves-baiyan/p/4913164.html

https://blog.csdn.net/kh1445291129/article/details/51149849

获取图片文件代码参考:

https://blog.csdn.net/u012005313/article/details/50687297

https://blog.csdn.net/xjz18298268521/article/details/54631168

转载自:https://blog.csdn.net/Limbos/article/details/80734879

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值