RANSAC (Random Sample Consensus) is an iterative method to estimate the parameters of a mathematical model from a set of observed data that contains outliers. In computer vision, it's often used for tasks like estimating the fundamental matrix, homography, or any fitting problem with noisy data.
In Python, OpenCV provides built-in support for RANSAC. Below, I'll show you how to use RANSAC with OpenCV to estimate a homography matrix from point correspondences between two images.
Step-by-Step Guide to Using RANSAC in OpenCV
Table of Content
- Step-by-Step Guide to Using RANSAC in OpenCV
- Step 1 - Install OpenCV.
- Step 2 - Import the required necessary libraries.
- Step 3 - Load Data - a set of points, an image pair, or data relevant to the problem.
- Step 4 - Detect Key points and Descriptors.
- Step 5 - Match Descriptors.
- Step 6 - Apply RANSAC to Find Homography.
- Implementation of RANSAC in OpenCV
- Conclusion
Here's a detailed step-by-step guide on how to apply RANSAC in Python using OpenCV, specifically for estimating a homography matrix between two images.
Step 1 - Install OpenCV.
pip install opencv-python
Step 2 - Import the required necessary libraries.
import cv2
import numpy as np
Step 3 - Load Data - a set of points, an image pair, or data relevant to the problem.
# Example: Load two images
img1 = cv2.imread('image1.jpg', 0)
img2 = cv2.imread('image2.jpg', 0)
Step 4 - Detect Key points and Descriptors.
Detecting key points involves identifying distinctive points in an image and descriptors are numerical representations of these keypoints' local neighbourhoods.
# Using ORB detector
orb = cv2.ORB_create()
keypoints1, descriptors1 = orb.detectAndCompute(img1, None)
keypoints2, descriptors2 = orb.detectAndCompute(img2, None)
Step 5 - Match Descriptors.
It helps in identifying similar regions or objects across multiple images, which is crucial for tasks like image alignment and recognition.
# Using BFMatcher
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(descriptors1, descriptors2)
matches = sorted(matches, key=lambda x: x.distance)
Step 6 - Apply RANSAC to Find Homography.
Technically, Projective – mapping between any two projection planes with the same centre of projection is called Homography.
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 = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
Implementation of RANSAC in OpenCV
Combining all these steps, let us have a look at one basic example of this implementation by showing the homography matrix.
For this example, we used to GFG logos on grayscale called as image1 and image2.

-100.jpg)
import cv2
import numpy as np
img1 = cv2.imread('image1.jpg', 0)
img2 = cv2.imread('image2.jpg', 0)
orb = cv2.ORB_create()
keypoints1, descriptors1 = orb.detectAndCompute(img1, None)
keypoints2, descriptors2 = orb.detectAndCompute(img2, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(descriptors1, descriptors2)
matches = sorted(matches, key=lambda x: x.distance)
print("Number of keypoints in image 1:", len(keypoints1))
print("Number of keypoints in image 2:", len(keypoints2))
print("Number of matches found:", len(matches))
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 = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
print("Homography Matrix (H):")
print(H)
Output:
Number of keypoints in image 1: 137
Number of keypoints in image 2: 500
Number of matches found: 17
Homography Matrix (H):
[[-2.36968104e-01 -1.02475500e-01 1.47166550e+02]
[-6.91043155e-01 -2.22894760e-01 4.06596070e+02]
[-1.67955133e-03 -5.71908258e-04 1.00000000e+00]]
Explanation
- Feature Detection and Description:
- We use ORB (Oriented FAST and Rotated BRIEF) to detect keypoints and compute their descriptors.
- Feature Matching:
- We use BFMatcher (Brute Force Matcher) with the Hamming distance (appropriate for ORB descriptors) to find matches between the descriptors from the two images.
- RANSAC for Homography Estimation:
cv2.findHomographyis called with the methodcv2.RANSAC. The function returns the homography matrixHand a mask indicating which matches were considered inliers by the RANSAC algorithm.
- Image Warping:
- We use the homography matrix
Hto warp the first image to align it with the second image.
- We use the homography matrix
Notes
- Parameter Tuning:
- The
cv2.RANSACmethod incv2.findHomographytakes a reprojection error threshold as an argument (5.0 in this case). This parameter can be tuned based on your data.
- The
- Alternative Methods:
- You can use other feature detectors and matchers like SIFT, SURF, or FLANN-based matcher depending on your requirements and data.
Using RANSAC with OpenCV is a powerful way to handle noisy data and outliers in computer vision tasks
Conclusion
The RANSAC algorithm is a highly effective tool in the field of computer vision that is used to work with noisy data and outliers. Due to it's iterative approach, even the presence significant noise allows for highly robust model fitting. The OpenCV implementation of RANSAC is simple but very powerful— thus finding use from different applications such as line fitting down to homography estimation.