OpenSfM icon indicating copy to clipboard operation
OpenSfM copied to clipboard

GPU Accelerated Feature Matching

Open Elliot-Construct opened this issue 1 year ago • 1 comments

I am wondering if it's possible to carry out FLANN or BRUTEFORCE using cv2.cuda.

My research indicates it's possible but attempting to alter matching.py with the following fails:

def match_brute_force(
    f1: np.ndarray,
    f2: np.ndarray,
    config: Dict[str, Any],
    maskij: Optional[np.ndarray] = None,
) -> List[Tuple[int, int]]:
    """
    Brute force matching and Lowe's ratio filtering using CUDA and Stream.

    Args:
        f1: feature descriptors of the first image
        f2: feature descriptors of the second image
        config: config parameters
        maskij: optional boolean mask of len(i descriptors) x len(j descriptors)
    """
    assert f1.dtype.type == f2.dtype.type
    if f1.dtype.type == np.uint8:
        matcher_type = "BruteForce-Hamming"
    else:
        matcher_type = "BruteForce"
    matcher = cv2.cuda.DescriptorMatcher.createBFMatcher
    matcher.add([f2])
    stream = cv2.cuda_Stream()
    matches = matcher.knnMatchConvert(f1, k=2, stream=stream)
    stream.waitForCompletion()
    ratio = config["lowes_ratio"]
    good_matches = []
    for match in matches:
        if match and len(match) == 2:
            m, n = match
            if m.distance < ratio * n.distance:
                good_matches.append(m)
    return _convert_matches_to_vector(good_matches)

with the error: AttributeError: module 'cv2.cuda' has no attribute 'DescriptorMatcher.createBFMatcher'

OpenCV.org: CUDA Descriptor Matcher says that:

createBFMatcher()

[static Ptrcuda::DescriptorMatcher cv::cuda::DescriptorMatcher::createBFMatcher | (| int | normType = cv::NORM_L2 | )] For each descriptor in the first set, this matcher finds the closest descriptor in the second set by trying each one. This descriptor matcher supports masking permissible matches of descriptor sets.

Parameters

normType | One of NORM_L1, NORM_L2, NORM_HAMMING. L1 and L2 norms are preferable choices for SIFT and SURF descriptors, NORM_HAMMING should be used with ORB, BRISK and BRIEF).

Any ideas?

Elliot-Construct avatar Mar 06 '24 10:03 Elliot-Construct

@Elliot-Construct this error message normally indicates that your python-opencv-package was not build with CUDA-support. Maybe you can install a wheel from this repository: https://github.com/cudawarped/opencv-python-cuda-wheels

kielnino avatar Mar 16 '24 17:03 kielnino