Exemplo n.º 1
0
double EuclidianMetric::getDistance(Descriptor &desc1, Descriptor &desc2)
{
    if (desc1.size() != desc2.size())
        throw std::logic_error("Error: out of bounds!");

    double distance = 0;
    for (int i = 0; i < desc1.size(); i++)
        distance += sqr(desc1[i] - desc2[i]);

    return std::sqrt(distance);
}
Exemplo n.º 2
0
/*
 * SSD
 * @short Computes the squareroot of squared differences
 * @param a First descriptor
 * @param b second descriptor
 * @return value of squareroot of squared differences
 */
double SSD(Descriptor& a, Descriptor& b)
{
  double diff = 0;

  for (unsigned int i = 0; i < a.size(); ++i) {
    float delta = a[i] - b[i];
    diff += delta*delta;
  }

  return diff;
}
// Add descriptors to the index.
void FlannDescriptorKDTree::AddDescriptor(Descriptor& descriptor) {

  // Copy the input descriptor into FLANN's Matrix type.
  const size_t cols = descriptor.size();
  flann::Matrix<double> flann_descriptor(new double[cols], 1, cols);
  for (size_t ii = 0; ii < cols; ++ii) {
    flann_descriptor[0][ii] = descriptor(ii);
  }

  // If this is the first point in the index, create the index and exit.
  if (index_ == nullptr) {
    // Single kd-tree. No approximation.
    const int kNumRandomizedKDTrees = 1;
    index_.reset(new flann::Index<flann::L2<double> >(
        flann_descriptor, flann::KDTreeIndexParams(kNumRandomizedKDTrees)));
    index_->buildIndex();
    return;
  }

  // If the index is already created, add the data point to the index. Rebuild
  // every time the index doubles in size to occasionally rebalance the kd tree.
  const int kRebuildThreshold = 2;
  index_->addPoints(flann_descriptor, kRebuildThreshold);
}