当前位置 : 首页 » 文章分类 :  科研  »  OpenCV2.4.4从完全不包含人体的图片中随机剪裁出用于人体检测的负样本

OpenCV2.4.4从完全不包含人体的图片中随机剪裁出用于人体检测的负样本

进行行人检测的分类器训练时,负样本是从完全不包含人体的图片中随机剪裁出来的,下面程序的目的就是从完全不包含人体的图片中随机剪裁出64×128大小的用于人体检测的负样本:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h> //srand()和rand()函数
  4. #include <time.h> //time()函数
  5. #include <opencv2/core/core.hpp>
  6. #include <opencv2/highgui/highgui.hpp>
  7. #include <opencv2/imgproc/imgproc.hpp>
  8. #include <opencv2/objdetect/objdetect.hpp>
  9. #include <opencv2/ml/ml.hpp>
  10. using namespace std;
  11. using namespace cv;
  12. int CropImageCount = 0; //裁剪出来的负样本图片个数
  13. int main()
  14. {
  15. Mat src;
  16. string ImgName;
  17. char saveName[256];//裁剪出来的负样本图片文件名
  18. ifstream fin("INRIANegativeImageList.txt");//打开原始负样本图片文件列表
  19. //ifstream fin("subset.txt");
  20. //一行一行读取文件列表
  21. while(getline(fin,ImgName))
  22. {
  23. cout<<"处理:"<<ImgName<<endl;
  24. ImgName = "D:\\DataSet\\INRIAPerson\\INRIAPerson\\Train\\neg\\" + ImgName;
  25. src = imread(ImgName);//读取图片
  26. //cout<<"宽:"<<src.cols<<",高:"<<src.rows<<endl;
  27. //图片大小应该能能至少包含一个64*128的窗口
  28. if(src.cols >= 64 && src.rows >= 128)
  29. {
  30. srand(time(NULL));//设置随机数种子
  31. //从每张图片中随机裁剪10个64*128大小的不包含人的负样本
  32. for(int i=0; i<10; i++)
  33. {
  34. int x = ( rand() % (src.cols-64) ); //左上角x坐标
  35. int y = ( rand() % (src.rows-128) ); //左上角y坐标
  36. //cout<<x<<","<<y<<endl;
  37. Mat imgROI = src(Rect(x,y,64,128));
  38. sprintf(saveName,"noperson%06d.jpg",++CropImageCount);//生成裁剪出的负样本图片的文件名
  39. imwrite(saveName, imgROI);//保存文件
  40. }
  41. }
  42. }
  43. system("pause");
  44. }

源码下载,环境为VS2010 + OpenCV2.4.4
http://download.csdn.net/detail/masikkk/6547869

上一篇 OpenCV2.4.4中训练自己的SVM分类器进行HOG行人检测

下一篇 利用TinyXML读取VOC2012数据集的XML标注文件裁剪出所有人体目标保存为文件

阅读
评论
441
阅读预计2分钟
创建日期 2013-11-13
修改日期 2017-07-02
类别

页面信息

location:
protocol: http:
host: masikkk.com
hostname: masikkk.com
origin: http://masikkk.com
pathname: /article/OpenCV-Cut-Negative-Example/
href: http://masikkk.com/article/OpenCV-Cut-Negative-Example/
document:
referrer:
navigator:
platform: Linux x86_64
userAgent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)

评论