ZiadMansourM / photogrammetry

Photogrammetry: Close Range 3D scanning. Our graduation project 🎓
https://docs.scanmate.sreboy.com/
GNU General Public License v3.0
5 stars 0 forks source link

RANSIC #28

Open ZiadMansourM opened 1 year ago

ZiadMansourM commented 1 year ago

To apply the RANSAC algorithm to remove outliers from the matched features, you can use OpenCV's findHomography function, which has a RANSAC option built-in. Here's how you can modify your data_feature_matching function to apply RANSAC:

import numpy as np
import cv2 as OpenCV

@timeit
def feature_matching(
        img_one_descriptors: np.ndarray, 
        img_two_descriptors: np.ndarray
    ) -> list[OpenCV.DMatch]:
    # ... (same as before)

@timeit
def apply_ransac(matches, keypoints1, keypoints2, threshold = 3.0):
    src_pts = np.float32([keypoints1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
    dst_pts = np.float32([keypoints2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)

    H, mask = OpenCV.findHomography(src_pts, dst_pts, OpenCV.RANSAC, threshold)
    matches_mask = mask.ravel().tolist()
    ransac_matches = [m for m, keep in zip(matches, matches_mask) if keep]

    return ransac_matches

@timeit
def data_feature_matching(images: Images) -> None:
    # ... (same as before, up to the feature_matching_output line)

                feature_matching_output = feature_matching(image.descriptors, matched_image.descriptors)
                ransac_output = apply_ransac(feature_matching_output, image.keypoints, matched_image.keypoints)
                images.feature_matches.append(FeatureMatches(image, matched_image, ransac_output))
                log_to_file("logs/tune.log", f"({image.img_id}, {matched_image.img_id}) with {len(ransac_output)} after RANSAC.")
                checked[image.img_id][matched_image.img_id], checked[matched_image.img_id][image.img_id] = 1, 1

    # RANSAC algorithm is now applied within the loop

This modification adds a apply_ransac function that takes the matched features as input along with the keypoints of the two images. It calculates the homography matrix using RANSAC and filters the matches based on the mask obtained from RANSAC. The filtered matches are then added to the FeatureMatches object.