pd_inria.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "opencv2/datasets/pd_inria.hpp"
  2. #include <opencv2/highgui.hpp>
  3. #include <opencv2/imgproc.hpp>
  4. #include <iostream>
  5. using namespace std;
  6. using namespace cv;
  7. using namespace cv::datasets;
  8. int main(int argc, char *argv[])
  9. {
  10. const char *keys =
  11. "{ help h usage ? | | show this message }"
  12. "{ path p |true | path to dataset }"
  13. "{ save s |false| save resized positive images }"
  14. "{ rwidth rw |64 | width of resized positive images }"
  15. "{ rheight rh |128 | height of resized positive images }"
  16. "{ padding |8 | vertical padding of resized positive images }";
  17. CommandLineParser parser(argc, argv, keys);
  18. bool savebbox = parser.get<bool>("save");
  19. int rwidth = parser.get<int>("rwidth");
  20. int rheight = parser.get<int>("rheight");
  21. int padding = parser.get<int>("padding");
  22. string path(parser.get<string>("path"));
  23. if (parser.has("help") || path=="true")
  24. {
  25. parser.printMessage();
  26. return -1;
  27. }
  28. Ptr<PD_inria> dataset = PD_inria::create();
  29. dataset->load(path);
  30. size_t train_size = dataset->getTrain().size();
  31. size_t test_size = dataset->getTest().size();
  32. cout << "train size: " << train_size << endl;
  33. cout << "test size: " << test_size << endl;
  34. int bbox_count = 0;
  35. for( size_t i = 0; i < train_size; i++ )
  36. {
  37. PD_inriaObj *example = static_cast<PD_inriaObj *>(dataset->getTrain()[i].get());
  38. cout << "\ntrain object index: " << i << endl;
  39. cout << "file name: " << example->filename << endl;
  40. // image size
  41. cout << "image size: " << endl;
  42. cout << " - width: " << example->width << endl;
  43. cout << " - height: " << example->height << endl;
  44. cout << " - depth: " << example->depth << endl;
  45. Mat img = imread( example->filename );
  46. // bounding boxes
  47. for ( size_t j = 0; j < example->bndboxes.size(); j++ )
  48. {
  49. Rect obj_bndbox = example->bndboxes[j]; // bounding box of object
  50. cout << " - bounding box: " << j << " - " << obj_bndbox << endl;
  51. int vpadding, hpadding;
  52. Rect ex_bndbox; // variable used for calculating expanded bounding box
  53. vpadding = cvRound(padding * obj_bndbox.height / rheight); // calculate vertical padding
  54. ex_bndbox.y = obj_bndbox.y - vpadding;
  55. ex_bndbox.height = 2 * vpadding + obj_bndbox.height;
  56. ex_bndbox.x = obj_bndbox.x + (obj_bndbox.width / 2);
  57. ex_bndbox.width = ex_bndbox.height * rwidth / rheight;
  58. ex_bndbox.x -= (ex_bndbox.width + 1) / 2;
  59. if (obj_bndbox.width > ex_bndbox.width)
  60. {
  61. obj_bndbox.x += (obj_bndbox.width - ex_bndbox.width + 1) / 2;
  62. obj_bndbox.width = ex_bndbox.width;
  63. }
  64. hpadding = obj_bndbox.x - ex_bndbox.x; // calculate horizontal padding
  65. if(savebbox)
  66. {
  67. Mat dst;
  68. copyMakeBorder(img(obj_bndbox), dst, vpadding, vpadding, hpadding, hpadding, BORDER_REFLECT);
  69. resize(dst, dst, Size(rwidth, rheight), 0, 0, INTER_AREA);
  70. imwrite(path + format("person_%04d.png", bbox_count++), dst);
  71. }
  72. else
  73. rectangle(img, obj_bndbox, Scalar(0, 0, 255), 2);
  74. }
  75. if (savebbox)
  76. continue; // skip UI updates
  77. imshow("INRIAPerson Dataset Train Images", img);
  78. cout << "\nPress a key to continue or ESC to exit." << endl;
  79. int key = waitKey();
  80. if( key == 27 ) break;
  81. }
  82. return 0;
  83. }