PageRenderTime 32ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/src/training/features/feature_densedescriptors.cpp

https://github.com/makerere-compute/ocula
C++ | 129 lines | 97 code | 13 blank | 19 comment | 20 complexity | c6ff8cac2494754035ea893e1f90c03b MD5 | raw file
  1. /*
  2. Calculate feature descriptors at regular positions on a grid
  3. Usage:
  4. densedescriptors imagefilename featuretype xstep ystep size
  5. featuretype = [surf|sift|brief|orb]
  6. Output:
  7. One line for each descriptor of the form:
  8. x y size d1 d2 d3... d64
  9. where d1 ... d64 are the SIFT descriptor values.
  10. Compile with:
  11. g++ `pkg-config --cflags opencv` -lopencv_core -lopencv_features2d -o feature_densedescriptors feature_densedescriptors.cpp
  12. */
  13. #include <stdio.h>
  14. #include "opencv2/core/core.hpp"
  15. #include "opencv2/features2d/features2d.hpp"
  16. #include "opencv2/highgui/highgui.hpp"
  17. #include <iostream>
  18. #include <vector>
  19. #include <stdio.h>
  20. using namespace cv;
  21. int main(int argc, char** argv)
  22. {
  23. const char* filename = argv[1];
  24. const char* featuretype = argv[2];
  25. int xstep;
  26. int ystep;
  27. int width;
  28. int height;
  29. int size;
  30. float scale;
  31. int extended;
  32. int x=0;
  33. int y=0;
  34. sscanf(argv[3], "%d", &xstep);
  35. sscanf(argv[4], "%d", &ystep);
  36. sscanf(argv[5], "%d", &size);
  37. IplImage* image;
  38. if (strcmp(featuretype,"OpponentSURF")==0 || strcmp(featuretype,"OpponentSIFT")==0) {
  39. image = cvLoadImage(filename);
  40. }
  41. else {
  42. image = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE );
  43. }
  44. vector<KeyPoint> keypoints;
  45. width=image->width;
  46. height=image->height;
  47. y = ystep;
  48. while (y<height) {
  49. x = xstep;
  50. while (x<width) {
  51. KeyPoint point = KeyPoint(x, y, size);
  52. keypoints.push_back(point);
  53. x+=xstep;
  54. }
  55. y += ystep;
  56. }
  57. Ptr<DescriptorExtractor> extractor = DescriptorExtractor::create(featuretype);
  58. Mat descriptors;
  59. extractor->compute(image, keypoints, descriptors);
  60. int ndescriptors, descriptorsize;
  61. ndescriptors = descriptors.rows;
  62. descriptorsize = descriptors.cols;
  63. //printf("Descriptor size %d\n",descriptorsize);
  64. int kpx, kpy, kpx_old, kpy_old, missingpoints, i, foundkeypoint;
  65. kpx_old = -1;
  66. kpy_old = -1;
  67. i = 0;
  68. y = ystep;
  69. while (y<height) {
  70. x = xstep;
  71. while (x<width) {
  72. kpx = int(keypoints[i].pt.x);
  73. kpy = int(keypoints[i].pt.y);
  74. // if there is a missing line, fill it in
  75. if ((kpx>x && kpy==y) || kpy>y) {
  76. printf("%d %d %d ",x,y,size);
  77. for (int j=0;j<descriptorsize;j++) {
  78. printf("0 ");
  79. }
  80. printf("\n");
  81. }
  82. else {
  83. // if this is not a duplicate, output the descriptor
  84. foundkeypoint = 0;
  85. while (foundkeypoint==0) {
  86. if (x==kpx && y==kpy) {
  87. printf("%d %d %d ", int(keypoints[i].pt.x),int(keypoints[i].pt.y),size);
  88. for (int j=0;j<descriptorsize;j++) {
  89. if (strcmp(featuretype,"SURF")==0) {
  90. printf("%.5f ",descriptors.at<float>(i,j));
  91. }
  92. else if (strcmp(featuretype,"ORB")==0) {
  93. printf("%u ",descriptors.at<uchar>(i,j));
  94. }
  95. else {
  96. printf("%.0f ",descriptors.at<float>(i,j));
  97. }
  98. }
  99. printf("\n");
  100. foundkeypoint = 1;
  101. }
  102. i++;
  103. kpx = int(keypoints[i].pt.x);
  104. kpy = int(keypoints[i].pt.y);
  105. }
  106. }
  107. x+=xstep;
  108. }
  109. y += ystep;
  110. }
  111. cvReleaseImage( &image );
  112. }