MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
feature_set.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#include <iostream>
11#include <algorithm>
12
13#include "sfm/feature_set.h"
14
16
17namespace
18{
19 template <typename T>
20 bool
21 compare_scale (T const& descr1, T const& descr2)
22 {
23 return descr1.scale > descr2.scale;
24 }
25} /* namespace */
26
27void
28FeatureSet::compute_features (mve::ByteImage::Ptr image)
29{
30 this->colors.clear();
31 this->positions.clear();
32 this->width = image->width();
33 this->height = image->height();
34
35 /* Make sure these are in the right order. Matching relies on it. */
36 if (this->opts.feature_types & FEATURE_SIFT)
37 this->compute_sift(image);
38 if (this->opts.feature_types & FEATURE_SURF)
39 this->compute_surf(image);
40}
41
42void
43FeatureSet::normalize_feature_positions (float px, float py)
44{
45 /* Normalize image coordinates. */
46 float const fwidth = static_cast<float>(this->width);
47 float const fheight = static_cast<float>(this->height);
48 float const fnorm = std::max(fwidth, fheight);
49 for (std::size_t i = 0; i < this->positions.size(); ++i)
50 {
51 math::Vec2f& pos = this->positions[i];
52 pos[0] = (pos[0] + 0.5f - fwidth * px) / fnorm;
53 pos[1] = (pos[1] + 0.5f - fheight * py) / fnorm;
54 }
55}
56
57void
58FeatureSet::compute_sift (mve::ByteImage::ConstPtr image)
59{
60 /* Compute features. */
62 {
63 Sift sift(this->opts.sift_opts);
64 sift.set_image(image);
65 sift.process();
66 descr = sift.get_descriptors();
67 }
68
69 /* Sort features by scale for low-res matching. */
70 std::sort(descr.begin(), descr.end(), compare_scale<sfm::Sift::Descriptor>);
71
72 /* Prepare and copy to data structures. */
73 std::size_t offset = this->positions.size();
74 this->positions.resize(offset + descr.size());
75 this->colors.resize(offset + descr.size());
76
77 for (std::size_t i = 0; i < descr.size(); ++i)
78 {
79 Sift::Descriptor const& d = descr[i];
80 this->positions[offset + i] = math::Vec2f(d.x, d.y);
81 image->linear_at(d.x, d.y, this->colors[offset + i].begin());
82 }
83
84 /* Keep SIFT descriptors. */
85 std::swap(descr, this->sift_descriptors);
86}
87
88void
89FeatureSet::compute_surf (mve::ByteImage::ConstPtr image)
90{
91 /* Compute features. */
92 Surf::Descriptors descr;
93 {
94 Surf surf(this->opts.surf_opts);
95 surf.set_image(image);
96 surf.process();
97 descr = surf.get_descriptors();
98 }
99
100 /* Sort features by scale for low-res matching. */
101 std::sort(descr.begin(), descr.end(), compare_scale<sfm::Surf::Descriptor>);
102
103 /* Prepare and copy to data structures. */
104 std::size_t offset = this->positions.size();
105 this->positions.resize(offset + descr.size());
106 this->colors.resize(offset + descr.size());
107
108 for (std::size_t i = 0; i < descr.size(); ++i)
109 {
110 Surf::Descriptor const& d = descr[i];
111 this->positions[offset + i] = math::Vec2f(d.x, d.y);
112 image->linear_at(d.x, d.y, this->colors[offset + i].begin());
113 }
114
115 /* Keep SURF descriptors. */
116 std::swap(descr, this->surf_descriptors);
117}
118
119void
120FeatureSet::clear_descriptors (void)
121{
122 this->sift_descriptors.clear();
123 this->sift_descriptors.shrink_to_fit();
124 this->surf_descriptors.clear();
125 this->surf_descriptors.shrink_to_fit();
126}
127
Vector class for arbitrary dimensions and types.
Definition vector.h:87
std::shared_ptr< Image< T > > Ptr
Definition image.h:42
std::shared_ptr< Image< T > const > ConstPtr
Definition image.h:43
Implementation of the SIFT feature detector and descriptor.
Definition sift.h:43
std::vector< Descriptor > Descriptors
Definition sift.h:153
Vector< float, 2 > Vec2f
Definition vector.h:30
void swap(mve::Image< T > &a, mve::Image< T > &b)
Specialization of std::swap for efficient image swapping.
Definition image.h:478
#define SFM_NAMESPACE_END
Definition defines.h:14
#define SFM_NAMESPACE_BEGIN
Definition defines.h:13