Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <dirent.h>
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7. #include <iterator>
  8. #include <opencv2/opencv.hpp>
  9. #include <opencv2/core/core.hpp>
  10. #include <opencv2/highgui/highgui.hpp>
  11. #include <opencv2/nonfree/features2d.hpp>
  12. #include <opencv2/features2d/features2d.hpp>
  13.  
  14. using namespace std;
  15. using namespace cv;
  16.  
  17. int main(int argc, char** argv)
  18. {
  19. if(argc != 3)
  20. {
  21. cout << "Usage: ./output <training database path> <test image>" << endl;
  22. return -1;
  23. }
  24.  
  25. // Reading training images paths
  26. vector<string> namesTrainingImages;
  27. vector<string>::iterator stringIt;
  28.  
  29. DIR *dir;
  30. struct dirent *ent;
  31. if ((dir = opendir (argv[1])) != NULL)
  32. {
  33. while ((ent = readdir (dir)) != NULL)
  34. {
  35. if((strcmp(ent -> d_name, ".") == 0) || (strcmp(ent -> d_name, "..") == 0))
  36. {
  37. continue;
  38. }
  39. string tempName(string(argv[1]) + string(ent -> d_name));
  40. namesTrainingImages.push_back(tempName);
  41. }
  42. }
  43. closedir(dir);
  44.  
  45. vector<Mat> trainingImages;
  46. vector<Mat>::iterator imageIt;
  47.  
  48. // Reading training images
  49. for(stringIt = namesTrainingImages.begin(); stringIt != namesTrainingImages.end(); ++stringIt)
  50. {
  51. Mat tempImage;
  52. tempImage = imread(*stringIt, CV_LOAD_IMAGE_GRAYSCALE);
  53. waitKey(5000);
  54. if(!tempImage.data )
  55. {
  56. cout << "Could not open or find the image" << std::endl ;
  57. continue;
  58. }
  59. else
  60. {
  61. trainingImages.push_back(tempImage);
  62. }
  63. }
  64.  
  65. vector< vector<KeyPoint> > keypoints;
  66. vector<Mat> descriptor;
  67. vector<Mat>::iterator descriptorIt;
  68. SIFT sift;
  69.  
  70. double percentage = 0.95;
  71.  
  72. for(imageIt = trainingImages.begin(); imageIt != trainingImages.end(); ++imageIt)
  73. {
  74. vector<KeyPoint> tempKeypoints;
  75. Mat tempDescriptor1, tempDescriptor2, tempDescriptor3;
  76.  
  77. // Generating keypoints and descriptors from training images
  78. sift.operator()(*imageIt, *imageIt, tempKeypoints, tempDescriptor1, false);
  79. keypoints.push_back(tempKeypoints);
  80.  
  81. // Using PCA to project and then backProject the descriptors of training images
  82. PCA pca(tempDescriptor1, Mat(), CV_PCA_DATA_AS_ROW, percentage);
  83. pca.PCA::project(tempDescriptor1, tempDescriptor2);
  84. tempDescriptor3 = pca.PCA::backProject(tempDescriptor2);
  85. descriptor.push_back(tempDescriptor3);
  86. }
  87.  
  88.  
  89. // Reading the test image
  90. Mat testImage;
  91. testImage = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
  92. waitKey(5000);
  93. if(!testImage.data)
  94. {
  95. cout << "Could not open or find the image" << std::endl ;
  96. return -1;
  97. }
  98.  
  99. vector<KeyPoint> testKeypoints;
  100. Mat testDescriptor, testDescriptor1, testDescriptor2;
  101.  
  102. // Generating teh keypoints and the descriptor of the test image
  103. sift.operator()(testImage, testImage, testKeypoints, testDescriptor1, false);
  104.  
  105. // Using PCA to project and then backProject the descriptor of the test image
  106. PCA pca(testDescriptor1, Mat(), CV_PCA_DATA_AS_ROW, percentage);
  107. pca.PCA::project(testDescriptor1, testDescriptor2);
  108. testDescriptor = pca.PCA::backProject(testDescriptor2);
  109.  
  110. // Flann based matching to determine the best matching training image
  111. FlannBasedMatcher matcher;
  112. vector<DMatch> matches;
  113. vector<DMatch> good_matches;
  114. int goodMatchesCtr = -1, tempCtr = -1, ctr = -1;
  115.  
  116. for(descriptorIt = descriptor.begin(); descriptorIt != descriptor.end(); ++descriptorIt)
  117. {
  118. ++tempCtr;
  119. vector<DMatch> tempMatches;
  120. vector<DMatch> temp_good_matches;
  121. int tempGoodMatchesCtr = 0;
  122. double min_dist = 100;
  123.  
  124. matcher.match(testDescriptor, *descriptorIt, tempMatches);
  125.  
  126. for(int i = 0; i < testDescriptor.rows && i < (*descriptorIt).rows; ++i)
  127. {
  128. double dist = tempMatches[i].distance;
  129. if(dist < min_dist)
  130. {
  131. min_dist = dist;
  132. }
  133. }
  134.  
  135. for(int i = 0; i < testDescriptor.rows && i < (*descriptorIt).rows; ++i)
  136. {
  137.  
  138. if(tempMatches[i].distance <= max(2 * min_dist, 0.02))
  139. {
  140. temp_good_matches.push_back(tempMatches[i]);
  141. ++tempGoodMatchesCtr;
  142. }
  143. }
  144.  
  145. if(tempGoodMatchesCtr > goodMatchesCtr)
  146. {
  147. goodMatchesCtr = tempGoodMatchesCtr;
  148. matches = tempMatches;
  149. good_matches = temp_good_matches;
  150. ctr = tempCtr;
  151. }
  152. }
  153.  
  154. // Displaying the test image alogside the best matching training image
  155. Mat img_matches;
  156.  
  157. drawMatches(testImage, testKeypoints, trainingImages[ctr], keypoints[ctr], good_matches, img_matches, Scalar ::all(-1), Scalar ::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
  158. for( int i = 0; i < (int)good_matches.size(); ++i )
  159. {
  160. printf( "-- Good Match [%d] Keypoint 1: %d -- Keypoint 2: %d n", i, good_matches[i].queryIdx, good_matches[i].trainIdx );
  161. }
  162. imshow("Good Matches", img_matches);
  163. waitKey(0);
  164.  
  165. cout << "Executed!" << 'n';
  166. return 0;
  167. }
  168.  
  169. #include <iostream>
  170. #include <stdio.h>
  171. #include <dirent.h>
  172. #include <fstream>
  173. #include <string>
  174. #include <vector>
  175. #include <iterator>
  176. #include <opencv2/opencv.hpp>
  177. #include <opencv2/core/core.hpp>
  178. #include <opencv2/highgui/highgui.hpp>
  179. #include <opencv2/nonfree/features2d.hpp>
  180. #include <opencv2/features2d/features2d.hpp>
  181.  
  182. using namespace std;
  183. using namespace cv;
  184.  
  185. int main(int argc, char** argv)
  186. {
  187. if(argc != 3)
  188. {
  189. cout << "Usage: ./output <training database path> <test image>" << endl;
  190. return -1;
  191. }
  192.  
  193. // Reading training images paths
  194. vector<string> namesTrainingImages;
  195. vector<string>::iterator stringIt;
  196.  
  197. DIR *dir;
  198. struct dirent *ent;
  199. if ((dir = opendir (argv[1])) != NULL)
  200. {
  201. while ((ent = readdir (dir)) != NULL)
  202. {
  203. if((strcmp(ent -> d_name, ".") == 0) || (strcmp(ent -> d_name, "..") == 0))
  204. {
  205. continue;
  206. }
  207. string tempName(string(argv[1]) + string(ent -> d_name));
  208. namesTrainingImages.push_back(tempName);
  209. }
  210. }
  211. closedir(dir);
  212.  
  213. vector<Mat> trainingImages;
  214. vector<Mat>::iterator imageIt;
  215.  
  216. // Reading training images
  217. for(stringIt = namesTrainingImages.begin(); stringIt != namesTrainingImages.end(); ++stringIt)
  218. {
  219. Mat tempImage;
  220. tempImage = imread(*stringIt, CV_LOAD_IMAGE_GRAYSCALE);
  221. waitKey(5000);
  222. if(!tempImage.data )
  223. {
  224. cout << "Could not open or find a training image" << std::endl ;
  225. continue;
  226. }
  227. else
  228. {
  229. trainingImages.push_back(tempImage);
  230. }
  231. }
  232.  
  233. vector< vector<KeyPoint> > keypoints;
  234. vector<Mat> descriptor;
  235. vector<Mat>::iterator descriptorIt;
  236. SIFT sift;
  237.  
  238. for(imageIt = trainingImages.begin(); imageIt != trainingImages.end(); ++imageIt)
  239. {
  240. vector<KeyPoint> tempKeypoints;
  241. Mat tempDescriptor;
  242.  
  243. // Generating keypoints and descriptors from training images
  244. sift.operator()(*imageIt, *imageIt, tempKeypoints, tempDescriptor, false);
  245. keypoints.push_back(tempKeypoints);
  246.  
  247. // Trying to use LDA to project the descriptors of training images
  248. LDA lda(tempDescriptor1, Mat());
  249. lda.LDA::project(tempDescriptor);
  250. descriptor.push_back(tempDescriptor);
  251. }
  252.  
  253. // Reading the test image
  254. Mat testImage;
  255. testImage = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
  256. waitKey(5000);
  257. if(!testImage.data)
  258. {
  259. cout << "Could not open or find the image" << std::endl ;
  260. return -1;
  261. }
  262.  
  263. vector<KeyPoint> testKeypoints;
  264. Mat testDescriptor;
  265.  
  266. // Generating the keypoints and the descriptor of the test image
  267. sift.operator()(testImage, testImage, testKeypoints, testDescriptor, false);
  268.  
  269. // Trying to use LDA to project the descriptor of the test image
  270. LDA lda(testDescriptor, Mat());
  271. lda.LDA::project(testDescriptor);
  272.  
  273. // Flann based matching to determine the best matching training image
  274. FlannBasedMatcher matcher;
  275. vector<DMatch> matches;
  276. vector<DMatch> good_matches;
  277. int goodMatchesCtr = -1, tempCtr = -1, ctr = -1;
  278.  
  279. for(descriptorIt = descriptor.begin(); descriptorIt != descriptor.end(); ++descriptorIt)
  280. {
  281. ++tempCtr;
  282. vector<DMatch> tempMatches;
  283. vector<DMatch> temp_good_matches;
  284. int tempGoodMatchesCtr = 0;
  285. double min_dist = 100;
  286.  
  287. matcher.match(testDescriptor, *descriptorIt, tempMatches);
  288.  
  289. for(int i = 0; i < testDescriptor.rows && i < (*descriptorIt).rows; ++i)
  290. {
  291. double dist = tempMatches[i].distance;
  292. if(dist < min_dist)
  293. {
  294. min_dist = dist;
  295. }
  296. }
  297.  
  298. for(int i = 0; i < testDescriptor.rows && i < (*descriptorIt).rows; ++i)
  299. {
  300.  
  301. if(tempMatches[i].distance <= max(2 * min_dist, 0.02))
  302. {
  303. temp_good_matches.push_back(tempMatches[i]);
  304. ++tempGoodMatchesCtr;
  305. }
  306. }
  307.  
  308. if(tempGoodMatchesCtr > goodMatchesCtr)
  309. {
  310. goodMatchesCtr = tempGoodMatchesCtr;
  311. matches = tempMatches;
  312. good_matches = temp_good_matches;
  313. ctr = tempCtr;
  314. }
  315. }
  316.  
  317. // Displaying the test image alogside the best matching training image
  318. Mat img_matches;
  319.  
  320. drawMatches(testImage, testKeypoints, trainingImages[ctr], keypoints[ctr], good_matches, img_matches, Scalar ::all(-1), Scalar ::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
  321. for( int i = 0; i < (int)good_matches.size(); ++i )
  322. {
  323. printf( "-- Good Match [%d] Keypoint 1: %d -- Keypoint 2: %d n", i, good_matches[i].queryIdx, good_matches[i].trainIdx );
  324. }
  325. imshow("Good Matches", img_matches);
  326. waitKey(0);
  327.  
  328. cout << "Executed!" << 'n';
  329. return 0;
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement