Skip to content

Commit

Permalink
DSPSIFT working
Browse files Browse the repository at this point in the history
  • Loading branch information
pierotofy committed Oct 29, 2023
1 parent 6073bea commit 76d48df
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 32 deletions.
1 change: 1 addition & 0 deletions opensfm/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ def extract_features_dspsift(
peak_threshold=0.0066666666666666671, # config["sift_peak_threshold"],
edge_threshold=config["sift_edge_threshold"],
target_num_features=features_count,
estimate_affine_shape=False,
)

logger.debug("Found {0} points in {1}s".format(len(points), time.time() - t))
Expand Down
2 changes: 1 addition & 1 deletion opensfm/src/features/dspsift.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace features {

py::tuple dspsift(foundation::pyarray_f image, float peak_threshold,
float edge_threshold, int target_num_features,
bool feature_root, bool domain_size_pooling);
bool feature_root, bool domain_size_pooling, bool estimate_affine_shape);

}
3 changes: 2 additions & 1 deletion opensfm/src/features/python/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ PYBIND11_MODULE(pyfeatures, m) {
py::arg("peak_threshold") = 0.003, py::arg("edge_threshold") = 10,
py::arg("target_num_features") = 0,
py::arg("feature_root") = true,
py::arg("domain_size_pooling") = true);
py::arg("domain_size_pooling") = true,
py::arg("estimate_affine_shape") = true);

m.def("match_using_words", features::match_using_words);
m.def("compute_vlad_descriptor", features::compute_vlad_descriptor,
Expand Down
59 changes: 29 additions & 30 deletions opensfm/src/features/src/dspsift.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,23 @@ namespace features {

py::tuple dspsift(foundation::pyarray_f image, float peak_threshold,
float edge_threshold, int target_num_features,
bool feature_root, bool domain_size_pooling) {
bool feature_root, bool domain_size_pooling, bool estimate_affine_shape) {
if (!image.size()) {
return py::none();
}

// bool estimate_affine_shape = true;

double dsp_min_scale = 1.0 / 6.0;
double dsp_max_scale = 3.0;
int dsp_num_scales = 10;

FeatureDescriptors descriptors;
FeatureKeypoints keypoints;
int keypoints_count = 0;
const int kpDimension = 4;

{
py::gil_scoped_release release;

double dsp_min_scale = 1.0 / 6.0;
double dsp_max_scale = 3.0;
int dsp_num_scales = 10;

// Setup covariant SIFT detector.
std::unique_ptr<VlCovDet, void (*)(VlCovDet*)> covdet(
vl_covdet_new(VL_COVDET_METHOD_DOG), &vl_covdet_delete);
Expand All @@ -80,29 +79,28 @@ py::tuple dspsift(foundation::pyarray_f image, float peak_threshold,
vl_covdet_set_edge_threshold(covdet.get(), edge_threshold);

vl_covdet_put_image(covdet.get(), image.data(), image.shape(1), image.shape(0));

// vl_covdet_set_non_extrema_suppression_threshold(covdet.get(), 0);

vl_covdet_detect(covdet.get(), target_num_features);
int num_features = vl_covdet_get_num_features(covdet.get());
// vl_covdet_detect(covdet.get(), target_num_features);
// int num_features = vl_covdet_get_num_features(covdet.get());

// int num_features = 0;
// while(true){
// int prev_num_features = num_features;
// vl_covdet_detect(covdet.get(), target_num_features);
// num_features = vl_covdet_get_num_features(covdet.get());
int num_features = 0;
while(true){
int prev_num_features = num_features;
vl_covdet_detect(covdet.get(), target_num_features);
num_features = vl_covdet_get_num_features(covdet.get());

// if (num_features < target_num_features && peak_threshold > 0.0001 && prev_num_features < num_features){
// peak_threshold = (peak_threshold * 2.0f) / 3.0f;
// vl_covdet_set_peak_threshold(covdet.get(), peak_threshold);
// }else break;
// }
if (num_features < target_num_features && peak_threshold > 0.0001 && prev_num_features < num_features){
peak_threshold = (peak_threshold * 2.0f) / 3.0f;
vl_covdet_set_peak_threshold(covdet.get(), peak_threshold);
}else break;
}

// if (estimate_affine_shape){
// vl_covdet_extract_affine_shape(covdet.get());
// } else {
if (estimate_affine_shape){
vl_covdet_extract_affine_shape(covdet.get());
} else {
vl_covdet_extract_orientations(covdet.get());
// }
}

VlCovDetFeature* features = vl_covdet_get_features(covdet.get());

Expand All @@ -124,16 +122,17 @@ py::tuple dspsift(foundation::pyarray_f image, float peak_threshold,
int prev_octave_scale_idx = std::numeric_limits<int>::max();
keypoints_count = std::min(target_num_features, num_features);

keypoints.resize(4 * keypoints_count);
keypoints.resize(kpDimension * keypoints_count);
int i = 0;
for (; i < keypoints_count; ++i) {
keypoints[kpDimension * i + 0] = features[i].frame.x;
keypoints[kpDimension * i + 1] = features[i].frame.y;

float det = features[i].frame.a11 * features[i].frame.a22 - features[i].frame.a12 * features[i].frame.a21;
float size = sqrt(fabs(det));
float angle = atan2(features[i].frame.a21, features[i].frame.a11) * 180.0f / M_PI;
keypoints[4 * i + 0] = features[i].frame.x;
keypoints[4 * i + 1] = features[i].frame.y;
keypoints[4 * i + 2] = size;
keypoints[4 * i + 3] = angle;
keypoints[kpDimension * i + 2] = size;
keypoints[kpDimension * i + 3] = angle;

const int octave_scale_idx =
features[i].o * kMaxOctaveResolution + features[i].s;
Expand Down Expand Up @@ -243,7 +242,7 @@ py::tuple dspsift(foundation::pyarray_f image, float peak_threshold,
}

return py::make_tuple(
foundation::py_array_from_data(keypoints.data(), keypoints_count, 4),
foundation::py_array_from_data(keypoints.data(), keypoints_count, kpDimension),
foundation::py_array_from_data(descriptors.data(), keypoints_count, 128));
}

Expand Down

0 comments on commit 76d48df

Please sign in to comment.