Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1.  
  2. #include <string.h>
  3. #include "opencv2/imgproc/imgproc.hpp"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <opencv/cv.h>
  7. #include <opencv/cv.h>
  8. #include <opencv2/nonfree/nonfree.hpp>
  9. #include <opencv2/nonfree/features2d.hpp>
  10. #include <opencv2/highgui/highgui.hpp>
  11. #include <opencv2/core/core.hpp>
  12. #include <opencv2/legacy/legacy.hpp>
  13. #include <opencv2/opencv.hpp>
  14. #include <vector>
  15. #include <windows.h>
  16. #include <fstream> // std::ifstream
  17. #include <iostream>
  18. #include <sys/stat.h>
  19. #include <sys/types.h>
  20.  
  21.  
  22. using namespace cv;
  23. using namespace std;
  24.  
  25. int main( int argc, char** argv ) {
  26.  
  27.  
  28. Mat initial_img;
  29.  
  30. string train_img_dir,
  31. train_results_file_path,
  32. test_img_dir;
  33.  
  34. if (argc != 4) {
  35.  
  36. cout << endl;
  37. cout << "Training images directory path: ";
  38. cin >> train_img_dir;
  39. cout << endl;
  40.  
  41. cout << "Training images solution file path: ";
  42. cin >> train_results_file_path;
  43. cout << endl;
  44.  
  45. cout << "Test images directory path: ";
  46. cin >> test_img_dir;
  47. cout << endl;
  48.  
  49. } else {
  50. train_img_dir = argv[1];
  51. train_results_file_path = argv[2];
  52. test_img_dir = argv[3];
  53. }
  54.  
  55. cout << endl;
  56. cout << "Training images directory path selected: " << train_img_dir << endl;
  57. cout << "Training images solution file path selected: " << train_results_file_path << endl;
  58. cout << "Test images directory path selected: " << test_img_dir << endl;
  59. cout << endl;
  60.  
  61. // detecting keypoints
  62. SurfFeatureDetector detector(50);
  63. vector<KeyPoint> keypoints;
  64.  
  65. // computing descriptors
  66. Ptr<DescriptorExtractor > extractor(new SurfDescriptorExtractor());// extractor;
  67. Mat descriptors;
  68. Mat training_descriptors(1,extractor->descriptorSize(),extractor->descriptorType());
  69. Mat img;
  70.  
  71. cout << "Building vocabulary...\n";
  72. int count = 0;
  73.  
  74. HANDLE dir;
  75. WIN32_FIND_DATA file_data;
  76.  
  77. if ((dir = FindFirstFile((train_img_dir + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE) {
  78. cout << "No files found." << endl;
  79. return 0;
  80. }
  81.  
  82. do {
  83. const string file_name = file_data.cFileName;
  84. const string full_file_name = train_img_dir + "/" + file_name;
  85. const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
  86.  
  87. if (file_name[0] == '.')
  88. continue;
  89.  
  90. if (is_directory)
  91. continue;
  92.  
  93. img = imread(full_file_name);
  94. detector.detect(img, keypoints);
  95. extractor->compute(img, keypoints, descriptors);
  96.  
  97. training_descriptors.push_back(descriptors);
  98. cout << '\r' << "Total descriptors: " << training_descriptors.rows;
  99.  
  100.  
  101. } while (FindNextFile(dir, &file_data));
  102.  
  103. cout << endl << endl;
  104.  
  105. BOWKMeansTrainer bowtrainer(120); //num clusters
  106. bowtrainer.add(training_descriptors);
  107. cout << "Cluster Bag of Words features..." << endl;
  108. Mat vocabulary = bowtrainer.cluster();
  109.  
  110. cout << endl;
  111.  
  112. Ptr<DescriptorMatcher > matcher(new BruteForceMatcher<L2<float> >());
  113. BOWImgDescriptorExtractor bowide(extractor,matcher);
  114. bowide.setVocabulary(vocabulary);
  115.  
  116. //setup training data for classifiers
  117. map<string,Mat> classes_training_data;
  118. classes_training_data.clear();
  119.  
  120. cout << "Training SVMs...\n";
  121.  
  122. Mat response_hist;
  123. count = 0;
  124. char buf[255];
  125. ifstream ifs(train_results_file_path);
  126. int total_samples = 0;
  127. do
  128. {
  129. ifs.getline(buf, 255);
  130. string line(buf);
  131. istringstream iss(line);
  132. string id, label;
  133. getline(iss, id, ',');
  134. getline(iss, label);
  135.  
  136. string imgpath = train_img_dir + "\\" + id + ".png";
  137. img = imread(imgpath);
  138. cout << '\r' << "Loading: " << imgpath;
  139.  
  140. bowide.compute(img, keypoints, response_hist);
  141.  
  142. if(classes_training_data.count(label) == 0) {
  143. classes_training_data[label].create(0,response_hist.cols,response_hist.type());
  144. }
  145. classes_training_data[label].push_back(response_hist);
  146. total_samples++;
  147.  
  148. } while (!ifs.eof());
  149. cout << '\r' << endl << endl;
  150.  
  151.  
  152. cout << "Training classes..." << endl;
  153.  
  154. //train 1-vs-all SVMs
  155. map<string, Ptr<CvSVM>> classes_classifiers;
  156. int num = 1;
  157.  
  158. for (map<string,Mat>::iterator it = classes_training_data.begin(); it != classes_training_data.end(); ++it) {
  159. string class_ = (*it).first;
  160. cout << "#" << num++ << " " << class_ << "..." << endl;
  161.  
  162. Mat samples(0,response_hist.cols,response_hist.type());
  163. Mat labels(0,1,CV_32FC1);
  164.  
  165. //copy class samples and label
  166. samples.push_back(classes_training_data[class_]);
  167. Mat class_label = Mat::ones(classes_training_data[class_].rows, 1, CV_32FC1);
  168. labels.push_back(class_label);
  169.  
  170. //copy rest samples and label
  171. for (map<string,Mat>::iterator it1 = classes_training_data.begin(); it1 != classes_training_data.end(); ++it1) {
  172. string not_class_ = (*it1).first;
  173. if(not_class_[0] == class_[0]) continue;
  174. samples.push_back(classes_training_data[not_class_]);
  175. class_label = Mat::zeros(classes_training_data[not_class_].rows, 1, CV_32FC1);
  176. labels.push_back(class_label);
  177. }
  178.  
  179. Mat samples_32f; samples.convertTo(samples_32f, CV_32F);
  180.  
  181. Ptr<CvSVM> tmp_svm = new CvSVM();
  182. auto lastInsertPtr = classes_classifiers.insert(make_pair(class_, tmp_svm));
  183. lastInsertPtr.first->second->train(samples_32f,labels);
  184. }
  185.  
  186. cout << endl;
  187.  
  188. cout << "Testing...\n";
  189.  
  190. //HANDLE dir;
  191. //WIN32_FIND_DATA file_data;
  192.  
  193. /*
  194.  
  195. /////////// MANUAL TESTING FOR NOW /////////////////
  196.  
  197. if ((dir = FindFirstFile((test_img_dir + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE) {
  198. cout << "No files found." << endl;
  199. return 0;
  200. }
  201.  
  202. do {
  203. const string file_name = file_data.cFileName;
  204. const string full_file_name = test_img_dir + "/" + file_name;
  205. const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
  206.  
  207. if (file_name[0] == '.')
  208. continue;
  209.  
  210. if (is_directory)
  211. continue;
  212.  
  213. img = imread(full_file_name);
  214. bowide.compute(img, keypoints, response_hist);
  215. //test vs. SVMs
  216. for (map<string, Ptr<CvSVM>>::iterator it = classes_classifiers.begin(); it != classes_classifiers.end(); ++it) {
  217. float res = (*it).second->predict(response_hist,false);
  218. cout << "class: " << (*it).first << ", response: " << res << endl;
  219. }
  220.  
  221.  
  222. } while (FindNextFile(dir, &file_data));
  223. */
  224.  
  225.  
  226. string ans;
  227. while (true) {
  228. cout << "File to test(q to quit): ";
  229. cin >> ans;
  230. if (ans.compare("q") == 0) break;
  231. img = imread(test_img_dir + "/" + ans + ".png");
  232. bowide.compute(img, keypoints, response_hist);
  233. for (map<string, Ptr<CvSVM>>::iterator it = classes_classifiers.begin(); it != classes_classifiers.end(); ++it) {
  234. float res = (*it).second->predict(response_hist,false);
  235. cout << "class: " << (*it).first << ", response: " << res << endl;
  236. }
  237. }
  238.  
  239. cout << endl << endl;
  240. system("PAUSE");
  241.  
  242. return 0;
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement