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

Remove dublicated SIFT-Dectector instance in feature matching #1053

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions opensfm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class OpenSfMConfig:
sift_peak_threshold: float = 0.1
# See OpenCV doc
sift_edge_threshold: int = 10
# See OpenCV doc
sift_nfeatures: int = 0
# See OpenCV doc
sift_octave_layers: int = 3
# See OpenCV doc
sift_sigma: float = 1.6

##################################
# Params for SURF
Expand Down
48 changes: 20 additions & 28 deletions opensfm/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,45 +353,36 @@ def extract_features_sift(
) -> Tuple[np.ndarray, np.ndarray]:
sift_edge_threshold = config["sift_edge_threshold"]
sift_peak_threshold = float(config["sift_peak_threshold"])
# SIFT support is in cv2 main from version 4.4.0
if context.OPENCV44 or context.OPENCV5:
# OpenCV versions concerned /** 3.4.11, >= 4.4.0 **/ ==> Sift became free since March 2020
detector = cv2.SIFT_create(
edgeThreshold=sift_edge_threshold, contrastThreshold=sift_peak_threshold
)
descriptor = detector
elif context.OPENCV3 or context.OPENCV4:
try:
# OpenCV versions concerned /** 3.2.x, 3.3.x, 3.4.0, 3.4.1, 3.4.2, 3.4.10, 4.3.0, 4.4.0 **/
detector = cv2.xfeatures2d.SIFT_create(
edgeThreshold=sift_edge_threshold, contrastThreshold=sift_peak_threshold
)
except AttributeError as ae:
# OpenCV versions concerned /** 3.4.3, 3.4.4, 3.4.5, 3.4.6, 3.4.7, 3.4.8, 3.4.9, 4.0.x, 4.1.x, 4.2.x **/
if "no attribute 'xfeatures2d'" in str(ae):
logger.error(
"OpenCV Contrib modules are required to extract SIFT features"
)
raise
descriptor = detector
else:
detector = cv2.FeatureDetector_create("SIFT")
descriptor = cv2.DescriptorExtractor_create("SIFT")
detector.setDouble("edgeThreshold", sift_edge_threshold)
sift_nfeatures = config["sift_nfeatures"]
sift_octave_layers = config["sift_octave_layers"]
sift_sigma = float(config["sift_sigma"])
while True:
logger.debug("Computing sift with threshold {0}".format(sift_peak_threshold))
t = time.time()
# SIFT support is in cv2 main from version 4.4.0
if context.OPENCV44 or context.OPENCV5:
detector = cv2.SIFT_create(
edgeThreshold=sift_edge_threshold, contrastThreshold=sift_peak_threshold
nfeatures=sift_nfeatures,
nOctaveLayers=sift_octave_layers,
contrastThreshold=sift_peak_threshold,
edgeThreshold=sift_edge_threshold,
sigma=sift_sigma,
)
descriptor = detector
elif context.OPENCV3:
detector = cv2.xfeatures2d.SIFT_create(
edgeThreshold=sift_edge_threshold, contrastThreshold=sift_peak_threshold
nfeatures=sift_nfeatures,
nOctaveLayers=sift_octave_layers,
contrastThreshold=sift_peak_threshold,
edgeThreshold=sift_edge_threshold,
sigma=sift_sigma,
)
descriptor = detector
else:
detector.setDouble("contrastThreshold", sift_peak_threshold)
detector = cv2.FeatureDetector_create("SIFT")
descriptor = cv2.DescriptorExtractor_create("SIFT")
detector.setDouble("edgeThreshold", sift_edge_threshold)

points = detector.detect(image)
logger.debug("Found {0} points in {1}s".format(len(points), time.time() - t))
if len(points) < features_count and sift_peak_threshold > 0.0001:
Expand All @@ -400,6 +391,7 @@ def extract_features_sift(
else:
logger.debug("done")
break

points, desc = descriptor.compute(image, points)

if desc is not None:
Expand Down
Loading