Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Dense SIFT feature type. http://www.vlfeat.org/overview/dsift.html #236

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ feature_process_size: 2048 # Resize the image if its size is larger than spec
feature_use_adaptive_suppression: no

# Params for SIFT
sift_peak_threshold: 0.1 # Smaller value -> more features
sift_peak_threshold: 0.1 # Smaller value -> more features
sift_edge_threshold: 10 # See OpenCV doc

# Params for DSIFT
dsift_step: 16 # Sampling step, smaller value -> more features
dsift_bin_size: 8
dsift_use_flat_window: yes # See VLFeat DSIFT doc
dsift_gaussian_blur: no # Smooth image using CV2 Gaussian Blur

# Params for SURF
surf_hessian_threshold: 3000 # Smaller value -> more features
surf_n_octaves: 4 # See OpenCV doc
Expand Down
8 changes: 7 additions & 1 deletion opensfm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@
feature_use_adaptive_suppression: no

# Params for SIFT
sift_peak_threshold: 0.1 # Smaller value -> more features
sift_peak_threshold: 0.1 # Smaller value -> more features
sift_edge_threshold: 10 # See OpenCV doc

# Params for DSIFT
dsift_step: 16 # Sampling step, smaller value -> more features
dsift_bin_size: 8
dsift_use_flat_window: yes # See VLFeat DSIFT doc
dsift_gaussian_blur: no # Smooth image using CV2 Gaussian Blur

# Params for SURF
surf_hessian_threshold: 3000 # Smaller value -> more features
surf_n_octaves: 4 # See OpenCV doc
Expand Down
19 changes: 18 additions & 1 deletion opensfm/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ def extract_features_sift(image, config):
return points, desc


def extract_features_dsift(image, config):
t = time.time()

if config.get('dsift_gaussian_blur', False): image = cv2.GaussianBlur(image, (5,5), 0)
points, desc = csfm.dsift(image.astype(np.float32) / 255, # VlFeat expects pixel values between 0, 1
step = config.get('dsift_step', 16),
bin_size = config.get('dsift_bin_size', 8),
use_flat_window = config.get('dsift_use_flat_window', True))

if config.get('feature_root', False): desc = np.sqrt(desc)

logger.debug('Found {0} points in {1}s'.format( len(points), time.time()-t ))
return points, desc


def extract_features_surf(image, config):
surf_hessian_threshold = config.get('surf_hessian_threshold', 3000)
if context.OPENCV3:
Expand Down Expand Up @@ -260,6 +275,8 @@ def extract_features(color_image, config, mask=None):
feature_type = config['feature_type'].upper()
if feature_type == 'SIFT':
points, desc = extract_features_sift(image, config)
elif feature_type == 'DSIFT':
points, desc = extract_features_dsift(image, config)
elif feature_type == 'SURF':
points, desc = extract_features_surf(image, config)
elif feature_type == 'AKAZE':
Expand All @@ -269,7 +286,7 @@ def extract_features(color_image, config, mask=None):
elif feature_type == 'ORB':
points, desc = extract_features_orb(image, config)
else:
raise ValueError('Unknown feature type (must be SURF, SIFT, AKAZE, HAHOG or ORB)')
raise ValueError('Unknown feature type (must be SURF, SIFT, DSIFT, AKAZE, HAHOG or ORB)')

xs = points[:, 0].round().astype(int)
ys = points[:, 1].round().astype(int)
Expand Down
8 changes: 8 additions & 0 deletions opensfm/src/csfm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "types.h"
#include "hahog.cc"
#include "dsift.cc"
#include "multiview.cc"
#include "akaze.cc"
#include "bundle.h"
Expand Down Expand Up @@ -78,6 +79,13 @@ BOOST_PYTHON_MODULE(csfm) {
)
);

def("dsift", csfm::dsift,
(boost::python::arg("step") = 16,
boost::python::arg("bin_size") = 8,
boost::python::arg("use_flat_window") = false
)
);

def("triangulate_bearings_dlt", csfm::TriangulateBearingsDLT);
def("triangulate_bearings_midpoint", csfm::TriangulateBearingsMidpoint);

Expand Down
52 changes: 52 additions & 0 deletions opensfm/src/dsift.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "types.h"
#include "dsift.h"

#include <vector>
#include <iostream>

extern "C" {
#include "vl/dsift.h"
#include "vl/imopv.h"
#include <time.h>
}


namespace csfm {

bp::object dsift(PyObject *image,
int step,
int bin_size,
bool use_flat_window) {
PyArrayContiguousView<float> im((PyArrayObject *)image);

if (im.valid()) {
VlDsiftFilter *dsift = vl_dsift_new_basic(im.shape(1), im.shape(0), step, bin_size);
vl_dsift_set_flat_window(dsift, use_flat_window);
vl_dsift_process(dsift, im.data());
int const numkp = vl_dsift_get_keypoint_num(dsift);
VlDsiftKeypoint const * keypoints = vl_dsift_get_keypoints(dsift);
std::vector<float> points(4 * numkp);
for (int i = 0; i < numkp; ++i) {
VlDsiftKeypoint const & kp = keypoints[i];
points[4 * i + 0] = (float) kp.x;
points[4 * i + 1] = (float) kp.y;
points[4 * i + 2] = (float) kp.s;
points[4 * i + 3] = (float) kp.norm;
}
// Get the descriptors: size is descsize * numkp:
int const descsize = vl_dsift_get_descriptor_size(dsift);
std::vector<float> desc(descsize * numkp);
float const *descriptors = vl_dsift_get_descriptors(dsift);
bp::list retn;
npy_intp points_shape[2] = {npy_intp(numkp), 4};
retn.append(bpn_array_from_data(2, points_shape, &points[0]));
npy_intp desc_shape[2] = {npy_intp(numkp), npy_intp(descsize)};
retn.append(bpn_array_from_data(2, desc_shape, descriptors));

vl_dsift_delete(dsift);
return retn;
}
return bp::object();
}

}
15 changes: 15 additions & 0 deletions opensfm/src/dsift.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef __DSIFT_H__
#define __DSIFT_H__

#include "types.h"

namespace csfm {

bp::object dsift(PyObject *image,
int step,
int bin_size,
bool use_flat_window);

}

#endif // __DSIFT_H__