PageRenderTime 57ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/training/features/feature_densesift.cpp

https://github.com/makerere-compute/ocula
C++ | 125 lines | 90 code | 14 blank | 21 comment | 15 complexity | ecaa6b56f473b8a5a27c26b4361ae5ea 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 = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE );
  38. vector<KeyPoint> keypoints;
  39. width=image->width;
  40. height=image->height;
  41. y = ystep;
  42. while (y<height) {
  43. x = xstep;
  44. while (x<width) {
  45. KeyPoint point = KeyPoint(x, y, size);
  46. keypoints.push_back(point);
  47. x+=xstep;
  48. }
  49. y += ystep;
  50. }
  51. if (strcmp(featuretype,"surf")) {
  52. fprintf("SURF features\n");
  53. SurfDescriptorExtractor extractor;
  54. }
  55. if (strcmp(featuretype,"surf")) {
  56. fprintf("SIFT features\n");
  57. SiftDescriptorExtractor extractor = SiftDescriptorExtractor(1.0);
  58. }
  59. //BriefDescriptorExtractor extractor;
  60. Mat descriptors;
  61. extractor.compute(image, keypoints, descriptors);
  62. int ndescriptors, descriptorsize;
  63. ndescriptors = descriptors.rows;
  64. descriptorsize = descriptors.cols;
  65. //printf("Descriptor size %d\n",descriptorsize);
  66. int kpx, kpy, kpx_old, kpy_old, missingpoints, i, foundkeypoint;
  67. kpx_old = -1;
  68. kpy_old = -1;
  69. i = 0;
  70. y = ystep;
  71. while (y<height) {
  72. x = xstep;
  73. while (x<width) {
  74. kpx = int(keypoints[i].pt.x);
  75. kpy = int(keypoints[i].pt.y);
  76. // if there is a missing line, fill it in
  77. if ((kpx>x && kpy==y) || kpy>y) {
  78. printf("%d %d %d %d %d ",x,y,kpx,kpy,size);
  79. for (int j=0;j<descriptorsize;j++) {
  80. printf("0 ");
  81. }
  82. printf("\n");
  83. }
  84. else {
  85. // if this is not a duplicate, output the descriptor
  86. foundkeypoint = 0;
  87. while (foundkeypoint==0) {
  88. if (x==kpx && y==kpy) {
  89. printf("%d %d %d ", int(keypoints[i].pt.x),int(keypoints[i].pt.y),size);
  90. for (int j=0;j<descriptorsize;j++) {
  91. //printf("%.5f ",descriptors.at<float>(i,j));
  92. printf("%.0f ",descriptors.at<float>(i,j));
  93. }
  94. printf("\n");
  95. foundkeypoint = 1;
  96. }
  97. i++;
  98. kpx = int(keypoints[i].pt.x);
  99. kpy = int(keypoints[i].pt.y);
  100. }
  101. }
  102. x+=xstep;
  103. }
  104. y += ystep;
  105. }
  106. cvReleaseImage( &image );
  107. }