Step-by-Step Guide to Using RANSAC in OpenCV using Python
Last Updated :
28 Jun, 2024
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
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.
Python
pip install opencv-python
Step 2 - Import the required necessary libraries.
Python
import cv2
import numpy as np
Step 3 - Load Data - a set of points, an image pair, or data relevant to the problem.
Python
# 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.
Python
# 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.
Python
# 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.
Python
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.
image1.jpg
image2.jpg
Python
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.findHomography
is called with the method cv2.RANSAC
. The function returns the homography matrix H
and a mask indicating which matches were considered inliers by the RANSAC algorithm.
- Image Warping:
- We use the homography matrix
H
to warp the first image to align it with the second image.
Notes
- Parameter Tuning:
- The
cv2.RANSAC
method in cv2.findHomography
takes a reprojection error threshold as an argument (5.0 in this case). This parameter can be tuned based on your data.
- 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.
Similar Reads
Reading an image in OpenCV using Python Prerequisite: Basics of OpenCVIn this article, we'll try to open an image by using OpenCV (Open Source Computer Vision) library.  Following types of files are supported in OpenCV library:Windows bitmaps - *.bmp, *.dibJPEG files - *.jpeg, *.jpgPortable Network Graphics - *.png WebP - *.webp Sun raste
6 min read
Image Transformations using OpenCV in Python In this tutorial, we are going to learn Image Transformation using the OpenCV module in Python. What is Image Transformation? Image Transformation involves the transformation of image data in order to retrieve information from the image or preprocess the image for further usage. In this tutorial we
5 min read
How to subtract two images using Python-OpenCV ? In this article, we are going to see how to subtract two images using OpenCV in Python. Now, before getting into the topic we shall discuss some of the use cases of Arithmetic operations. Arithmetic operations like addition and subtraction can help us to make images brighter or darker. Specifically,
3 min read
Setting up ROS with Python 3 and Python OpenCV Setting up a Robot Operating System (ROS) with Python 3 and OpenCV can be a powerful combination for robotics development, enabling you to leverage ROS's robotics middleware with the flexibility and ease of Python programming language along with the computer vision capabilities provided by OpenCV. H
3 min read
Draw Multiple Rectangles in Image using Python-Opencv In this article, we are going to see how to draw multiple rectangles in an image using Python and OpenCV. Function used:imread(): In the OpenCV, the cv2.imread() function is used to read an image in Python. Syntax: cv2.imread(path_of_image, flag) rectangle(): In the OpenCV, the cv2.rectangle functio
2 min read