Skip to content

Commit

Permalink
Remove dublicated SIFT-Dectector instance in feature matching (#1053)
Browse files Browse the repository at this point in the history
Summary:
It seems to me that the SIFT detector is instantiated, but then not used, because the object is created again in the following while loop. This merge request removes the unnecessary code.
I have also made the missing parameters of the OpenCV-Sift implementation configurable.

Pull Request resolved: #1053

Reviewed By: paulinus

Differential Revision: D57765783

Pulled By: fabianschenk

fbshipit-source-id: 59a24bbbc60f55c1cb5e1f81a9896ec13f82e5d8
  • Loading branch information
kielnino authored and facebook-github-bot committed Jun 3, 2024
1 parent 40b73c0 commit b18e638
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
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

0 comments on commit b18e638

Please sign in to comment.