Direct Graphical Models  v.1.7.0
SIFT.cpp
1 #include "SIFT.h"
2 #include "opencv/SIFT.h"
3 #include "LinearMapper.h"
4 #include "macroses.h"
5 
6 namespace DirectGraphicalModels { namespace fex
7 {
8  Mat CSIFT::get(const Mat &img)
9  {
10  int i; // feature index
11  int x, y;
12  int width = img.cols;
13  int height = img.rows;
14 
15  // Converting to one channel image
16  Mat I;
17  if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
18  else img.copyTo(I);
19 
20  Ptr<xfeatures2d::SIFT> sift = xfeatures2d::SIFT::create();
21 
22  // Prepare key points
23  std::vector<KeyPoint> vKeyPoints;
24 // sift->detect(img, vKeyPoints);
25  for (y = 0; y < height; y++)
26  for (x = 0; x < width; x++)
27  vKeyPoints.push_back(KeyPoint(static_cast<float>(x), static_cast<float>(y), 1.0f));
28 
29 // drawKeypoints(img, vKeyPoints, img, Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
30 
31  Mat descriptors;
32  sift->detectAndCompute(I, Mat(), vKeyPoints, descriptors, true);
33 
34  const int nFeatures = descriptors.cols; // 128
35  DGM_ASSERT(nFeatures == 128);
36  DGM_ASSERT(nFeatures < CV_CN_MAX);
37 
38  // Initializing features
39  vec_mat_t vFeatures(nFeatures);
40  for (i = 0; i < nFeatures; i++) vFeatures[i].create(img.size(), CV_8UC1);
41 
42  std::vector<byte *> pFeatures(nFeatures);
43 
44  for (y = 0; y < height; y++) {
45  for (i = 0; i < nFeatures; i++) pFeatures[i] = vFeatures[i].ptr<byte>(y);
46  for (x = 0; x < width; x++) {
47  float *pDescriptors = descriptors.ptr<float>(y * width + x);
48  for (i = 0; i < nFeatures; i++)
49  pFeatures[i][x] = linear_mapper<byte>(pDescriptors[i], 0.0f, 255.0f); // features[i] (x, y) = descriptors (i, pixel_idx)
50  } // x
51  } // y
52 
53  Mat res;
54  merge(vFeatures, res);
55 
56  return res;
57  }
58 } }
virtual Mat get(void) const
Extracts and returns the required feature.
Definition: SIFT.h:26