Assignment 2
CS477 – Computer Vision
Fall 2024
Do the following.
Question 1: Implement the Harris Corner Detection algorithm using Python and OpenCV. Apply
your implementation to image, and analyze the results.
1. Write a Python program that applies the Harris Corner Detection algorithm to detect
corners in an image. The program should mark the detected corners on the output
image.
2. Explain how the Harris Corner Detection algorithm identifies corner points in an
image and discuss the significance of its parameters such as window size, Sobel
operator size, and the Harris detector free parameter.
3. Analyze the results on the chosen image, noting any challenges encountered. Discuss
how these challenges could be overcome and suggest any improvements to the
algorithm.
Question 2: Implement the RANSAC algorithm to fit a line to a noisy dataset with outliers.
1. Create a synthetic dataset of at least 50 data points where some points are
intentionally added as outliers. Implement the RANSAC algorithm to fit a line to these
data points.
2. Describe the RANSAC algorithm's approach to managing outliers in the dataset. Discuss
how the choice of parameters, such as the number of iterations and the threshold for
inlier determination, affects the outcome.
These questions are designed to test your practical skills in implementing algorithms as
well as your analytical ability to evaluate their performance under various conditions.
Authors Contributions. Mention each author’s contribution at the end of each question (Mandatory).
Author 1. Moeez Ahmed Qaiser
Author 2. Malaikah Javed
Author 3. Hashaam Zafar
1
Assignment 2
CS477 – Computer Vision
Question 1:
Code:
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
img = cv2.imread('/content/shapes.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2_imshow(img)
gray = np.float32(gray)
Detected = cv2.cornerHarris(gray, 5, 3, 0.04)
Detected = cv2.dilate(Detected, None)
img[Detected>0.01*Detected.max()] = [255,188,0]
cv2_imshow(img)
Output:
Original Image: Corner Detected Image:
2
Assignment 2
CS477 – Computer Vision
ii) Explanation:
A 5x5 chunk of the image is taken out. Then, a 3x3 Sobel operator is applied to this chunk so that
the derivative is taken of the 5x5 image in both the X and Y directions. Afterwards, the new
derived images are used to calculate the Eigen values, on the basis of which it is determined
whether it is a corner, edge, or a flat region.
Window size:
5x5 is used so that the local edge is detected. Using a very small window would lead
to the edge not being detected. A very large window may detect multiple edges in one
window.
Sobel Operator Size:
A kernel of 3 is used. I selected this value as it is the most common size and one
that gives the best value. Using a larger value will lead to smoothing and noise reduction,
however it may blur the corner to an undetectable degree.
Harris free parameter:
This value is in the range of 0.04 to 0.06 and defines the criteria for choosing
whether it is an edge, corner or flat.
iii) Challenges:
The window size is kept constant in this approach. Should we be faced by an edge of a
larger magnitude, it won’t be detected. Alternatively, if we choose a larger window size, it may
pick on multiple smaller edges and give an inaccurate edge. In the above image, this issue can be
seen in the pentagon shape. The perimeter consists of multiple small lines, hence having edges
throughout. However, only some are detected due to their size difference. To fix this issue, we can
deploy a technique which will change the size of the window to accommodate all the edges.
3
Assignment 2
CS477 – Computer Vision
Question 2:
Code:
import numpy as np
from matplotlib import pyplot as plt
from sklearn import datasets, linear_model
#Number of samples and outliers in the dataset
n_samples = 100
n_outliers = 5
#Generating a synthetic linear regression dataset
X, y, coef = datasets.make_regression(
n_samples=n_samples,
n_features=1,
n_informative=1,
noise=10, #Standard deviation of Gaussian noise
coef=True, #true coefficient of the underlying model
random_state=0,
)
np.random.seed(0) #Guarantees reproducibility
#Add outlier data to the dataset
X[:n_outliers] = 2 + 0.5 * np.random.normal(size=(n_outliers, 1))
y[:n_outliers] = -1 + 50 * np.random.normal(size=n_outliers)
#Fitting a robust linear model using the RANSAC algorithm
ransac = linear_model.RANSACRegressor(
max_trials=200,
residual_threshold=10,
min_samples=2
)
# Fit the model using the RANSAC algorithm
ransac.fit(X, y)
# Identify inliers and outliers
inlier_mask = ransac.inlier_mask_
outlier_mask = np.logical_not(inlier_mask)
# Plot the data points and the fitted line
plt.scatter(X[inlier_mask], y[inlier_mask], color="green", label="Inliers")
plt.scatter(X[outlier_mask], y[outlier_mask], color="red", label="Outliers")
4
Assignment 2
CS477 – Computer Vision
# Predict y values for a range of x values using the RANSAC model
line_X = np.arange(X.min(), X.max())[:, np.newaxis]
line_y_ransac = ransac.predict(line_X)
# Plot the RANSAC regression line
plt.plot(line_X, line_y_ransac, color="navy", linewidth=2, label="RANSAC
regressor")
plt.legend(loc="lower right")
plt.xlabel("Input")
plt.ylabel("Response")
plt.show()
OUTPUT:
Residual_threshold = 10 Residual_threshold = 15
Residual_threshold = 20 Residual_threshold = 25
5
Assignment 2
CS477 – Computer Vision
Residual_threshold = 30
Describe the RANSAC algorithm's approach to managing outliers in the dataset. Discuss
how the choice of parameters, such as the number of iterations and the threshold for inlier
determination, affects the outcome.
Answer:
RANSAC (Random Sample Consensus) is an algorithm used to fit a model to data with outliers. It
works by randomly selecting subsets of data points, fitting the model, and identifying inliers based
on a residual threshold. Outliers, which have large residuals, are excluded from the model.
ransac = linear_model.RANSACRegressor(
max_trials=200,
residual_threshold=10,
min_samples=2
)
Parameters:
1. Number of Iterations (max_trials): Determines how many random subsets are tested.
More iterations increase the chances of finding a good model but takes longer time.
2. Minimum Samples (min_samples): Specifies the minimum number of points required to
fit a model.
3. Residual Threshold (residual_threshold): Defines the maximum allowable error for a
point to be considered an inlier. A lower threshold is stricter, and a higher threshold may
include more outliers.
6
Assignment 2
CS477 – Computer Vision
Working:
Residual Calculation:
After fitting a model to a random subset of data, the residual for each point is
calculated as the absolute difference between the observed and predicted values:
residual=∣ y − ypredicted ∣
Threshold Comparison:
o If the residual is less than or equal to the residual threshold, the point is considered
an inlier.
o If the residual exceeds the threshold, the point is treated as an outlier.