Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdexcept>
  4. #include <iomanip>
  5. #include <string>
  6. #include <opencv2/objdetect.hpp>
  7. #include <opencv2/highgui.hpp>
  8. #include <opencv2/imgproc.hpp>
  9. #include <opencv2/imgcodecs.hpp>
  10. #include <opencv2/video.hpp>
  11. #include <opencv2/videoio.hpp>
  12.  
  13. using namespace cv;
  14. using namespace std;
  15. typedef vector<vector<float> > HogData;
  16.  
  17.  
  18. const char* keys =
  19. {
  20. "{ help h | | print help message }"
  21. "{ directory d | | images directory}"
  22. };
  23.  
  24. static void computeHOG(HOGDescriptor &hog, Mat &img, HogData &hogdata, int value)
  25. {
  26. Mat gray;
  27. cvtColor(img, gray, COLOR_BGR2GRAY );
  28. resize(gray, gray, Size(80, 80));
  29. hog.winSize = Size(80,80);
  30. vector<Point> positions;
  31. vector<float> descriptor;
  32. hog.compute(gray,descriptor,cv::Size(8,8),cv::Size(0,0),positions);
  33. descriptor.insert(descriptor.begin(),value);
  34. hogdata.push_back(descriptor);
  35. }
  36.  
  37. int main(int argc, char** argv)
  38. {
  39. CommandLineParser parser(argc, argv, keys);
  40. HogData hogdata;
  41.  
  42. HOGDescriptor hog;
  43.  
  44. string pattern_glob = "";
  45. if (parser.has("directory"))
  46. {
  47. pattern_glob = parser.get<string>("directory");
  48. }
  49.  
  50. if (!pattern_glob.empty())
  51. {
  52. //Read from input image files
  53. vector<String> filenames;
  54. //Read from video file
  55. Mat frame;
  56. int value;
  57.  
  58. if (!pattern_glob.empty())
  59. {
  60. String folder(pattern_glob);
  61. glob(folder, filenames);
  62. }
  63.  
  64. vector<String>::const_iterator it_image = filenames.begin();
  65.  
  66. for (; it_image != filenames.end(); ++it_image)
  67. {
  68. cout << "\nRead: " << *it_image << endl;
  69. value = ((string) *it_image).at(((((string) *it_image).find_first_of("/")) + 1)) - '0';
  70. cout << "Real value : " << value << endl;
  71. frame = imread(*it_image);
  72.  
  73. if (!frame.empty())
  74. {
  75. computeHOG(hog, frame, hogdata, value);
  76. }
  77.  
  78. }
  79. }
  80. ofstream myfile ("resultHOGElliot.data", ofstream::trunc);
  81. cout << hogdata.at(0).size() << endl;
  82. if (myfile.is_open())
  83. {
  84. for (vector<float> vf : hogdata) {
  85. for (float f : vf) {
  86. myfile << setprecision(2) << f << ",";
  87. }
  88. myfile <<'\b'; //Cursor moves 1 position backwards
  89. myfile << endl;
  90. }
  91. myfile.close();
  92. }
  93.  
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement