Geometry-based Methods in Vision
Assignment 1
Q1: Affine Rectification
Book1:
- Input Image:
- Parallel Lines on Input Image:
- Affine-Rectified Image:
- Test Lines on Input Image:
- Test Lines on Affine-Rectified Image:
- Before Rectification (Dot Product of Lines): 0.999, 0.957
- After Rectification (Dot Product of Lines): 0.9999, 0.9999
Chess1:
- Input Image:
- Parallel Lines on Input Image:
- Affine-Rectified Image:
- Test Lines on Input Image:
- Test Lines on Affine-Rectified Image:
- Before Rectification (Dot Product of Lines): -0.987, 0.985
- After Rectification (Dot Product of Lines): -0.999, 0.999
Tiles5:
- Input Image:
- Parallel Lines on Input Image:
- Affine-Rectified Image:
- Test Lines on Input Image:
- Test Lines on Affine-Rectified Image:
- Before Rectification (Dot Product of Lines): 0.986, 0.9996
- After Rectification (Dot Product of Lines): 0.999, 0.999
Table:
- Input Image:
- Parallel Lines on Input Image:
- Affine-Rectified Image:
- Test Lines on Input Image:
- Test Lines on Affine-Rectified Image:
- Before Rectification (Dot Product of Lines): -0.954, 0.947
- After Rectification (Dot Product of Lines): -0.999, 0.999
Home:
- Input Image:
- Parallel Lines on Input Image:
- Affine-Rectified Image:
- Test Lines on Input Image:
- Test Lines on Affine-Rectified Image:
- Before Rectification (Dot Product of Lines): 0.999, 0.999
- After Rectification (Dot Product of Lines): 1.0, 1.0
Description:
Affine Rectification
Affine Rectification
Affine rectification aims to correct the perspective distortion of an image, restoring affine geometric properties. Here is a detailed breakdown of the algorithm and relevant equations:
- User Annotation of Lines: The user interactively annotates four line segments on the image. Each segment is defined by two points, and the user clicks on the image to specify the coordinates of these points. A mouse event is used to capture the user's click coordinates, and these points are stored.
- Line Equation Calculation: For each pair of annotated line segment endpoints, the line equation is calculated using the cross product of the two points. The cross product formula is:
\[ l = \mathbf{p_1} \times \mathbf{p_2} \]
where \(\mathbf{p_1}\) and \(\mathbf{p_2}\) are the homogeneous coordinates of the two points. The resulting \(l\) is the equation of the line.
- Calculating Vanishing Points and the Line at Infinity: For each pair of parallel lines (the four lines annotated by the user), the vanishing point is computed as the cross product of the two lines. The vanishing point represents the intersection of parallel lines in the image plane. The cross product is calculated as:
\[ v = l_1 \times l_2 \]
where \( l_1 \) and \( l_2 \) are the equations of two parallel lines, and \( v \) is their vanishing point.
By computing vanishing points for two pairs of parallel lines, their cross product gives the line at infinity \( l_\infty \):
\[ l_\infty = v_1 \times v_2 \]
- Affine Rectification Homography Matrix: The goal of affine rectification is to transform the image so that parallel lines remain parallel. Using the line at infinity, the homography matrix for affine rectification can be constructed as:
\[
H_A =
\begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
l_{\infty,1} & l_{\infty,2} & l_{\infty,3}
\end{bmatrix}
\]
where \( l_{\infty,1} \), \( l_{\infty,2} \), and \( l_{\infty,3} \) are the coordinates of the line at infinity in homogeneous form. This matrix transforms the image to affine space.
- Image Rectification and Display: The original image is transformed using the homography matrix \( H_A \) via OpenCV's
warpPerspective
function. The rectified image, where parallel lines have been restored, is then displayed and saved.
Q2: Metric Rectification
Book1:
- Input Image:
- Annotated Perpendicular Lines on Input Image:
- Annotated Perpendicular Lines on Affine-Rectified Image:
- Metric Rectified Image:
- Test Lines on Input Image:
- Test Lines on Metric-Rectified Image:
- Before Rectification (Dot Product of Lines): 0.120, -0.166
- After Rectification (Dot Product of Lines): 0.0001, 0.00001
Chess1:
- Input Image:
- Annotated Perpendicular Lines on Input Image:
- Annotated Perpendicular Lines on Affine-Rectified Image:
- Metric Rectified Image:
- Test Lines on Input Image:
- Test Lines on Metric-Rectified Image:
- Before Rectification (Dot Product of Lines): -0.658, 0.078
- After Rectification (Dot Product of Lines): -0.001, 0
Tiles5:
- Input Image:
- Annotated Perpendicular Lines on Input Image:
- Annotated Perpendicular Lines on Affine-Rectified Image:
- Metric Rectified Image:
- Test Lines on Input Image:
- Test Lines on Metric-Rectified Image:
- Before Rectification (Dot Product of Lines): 0.1653, 0.0227
- After Rectification (Dot Product of Lines): 0.091, -0.003
Table:
- Input Image:
- Annotated Perpendicular Lines on Input Image:
- Annotated Perpendicular Lines on Affine-Rectified Image:
- Metric Rectified Image:
- Test Lines on Input Image:
- Test Lines on Metric-Rectified Image:
- Before Rectification (Dot Product of Lines): 0.0845, -0.7717
- After Rectification (Dot Product of Lines): -0.0012, -0.002
Home:
- Input Image:
- Annotated Perpendicular Lines on Input Image:
- Annotated Perpendicular Lines on Affine-Rectified Image:
- Metric Rectified Image:
- Test Lines on Input Image:
- Test Lines on Metric-Rectified Image:
- Before Rectification (Dot Product of Lines): 0.2783, 0.7938
- After Rectification (Dot Product of Lines): 0.1752, 0.7632
Description:
Metric Rectification
After affine rectification, metric rectification further restores the image's scale and angles so that orthogonal lines in the real scene appear orthogonal in the image. The detailed algorithm steps are:
- User Re-annotation of Lines: After affine rectification, the user is required to annotate four new line segments. Again, the user clicks on the image to specify the start and end points of these segments, which are used for metric rectification.
- Calculating Orthogonality Constraints: Metric rectification aims to restore the orthogonality of line pairs. From the user-annotated lines, their equations are recalculated. The row constraint for two lines \( l_1 \) and \( l_2 \) is given by:
\[ r(l_1, l_2) = [x_1 z_2, x_1 y_2 + x_2 y_1, y_1 z_2] \]
where \( (x_1, y_1) \) and \( (x_2, y_2) \) are the normalized coordinates of the two lines.
- Computing the Conic at Infinity \( C'_{\infty} \): Using the orthogonal line pairs, the metric rectification constraint is derived by calculating the conic at infinity matrix \( C'_{\infty} \). From the row constraints, the matrix coefficients are computed by taking the cross product of the constraints:
\[ c = r(l_1, l_2) \times r(l_3, l_4) \]
The resulting conic at infinity matrix is:
\[
C'_{\infty} =
\begin{bmatrix}
a & b & 0 \\
b & 1 & 0 \\
0 & 0 & 0
\end{bmatrix}
\]
where \( a \) and \( b \) are derived from the computed coefficients \( c \).
- Verifying Orthogonality: Using the conic matrix \( C'_{\infty} \), the orthogonality of the annotated line pairs is verified by checking that the dot product of the line equations satisfies the orthogonality condition:
\[ l_1^T C'_{\infty} l_2 \approx 0 \]
If this condition holds, the lines are orthogonal.
- Computing the Metric Rectification Homography Matrix: By performing Singular Value Decomposition (SVD) on the conic matrix \( C'_{\infty} \), the metric rectification homography matrix \( H_S \) is calculated. The SVD is given by:
\[ C'_{\infty} = U S V^T \]
By adjusting the singular values in \( S \), the metric rectification homography matrix \( H_S \) is computed as:
\[ H_S = U \sqrt{S^{-1}} V^T \]
This matrix transforms the image into metric space.
- Image Rectification and Display: Finally, the complete transformation matrix is obtained by multiplying the affine rectification matrix \( H_A \) and the metric rectification matrix \( H_S \):
\[ H = H_A \cdot H_S \]
This matrix is used to perform metric rectification on the image, restoring the true scale and orthogonal relationships. The rectified image, where orthogonal lines are correctly orthogonal, is displayed and saved.
Q3: Planar Homography from Point Correspondences
Book onto Desk:
- Normal Image:
- Perspective Image:
- Annotated Corners in Perspective Image:
- Result:
Book onto Phone:
- Normal Image:
- Perspective Image:
- Annotated Corners in Perspective Image:
- Result:
Description:
Algorithm Steps:
-
User Interaction (Obtaining Corresponding Points):
- I first used OpenCV to display the background image and allowed the user to select four points on the background image via mouse clicks using the
click_event
function. These points are stored in the global list points
.
- These four points (
desk_points
) represent the locations on the background image where the target image will be mapped.
- At the same time, I defined the four corner points of the target image (
target_points
):
Point 1: (0, 0, 1)
Point 2: (w, 0, 1)
Point 3: (w, h, 1)
Point 4: (0, h, 1)
where \( w \) and \( h \) are the width and height of the target image, respectively.
-
Computing the Homography Matrix \( H \):
- I utilized the set of corresponding points to compute the homography matrix \( H \), satisfying the following relationship:
\[
s \begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = H \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}
\]
where \( (x, y, 1) \) are points in the target image, \( (x', y', 1) \) are the corresponding points in the background image, and \( s \) is a scale factor.
- To solve for \( H \), I constructed a linear system \( A \vec{h} = 0 \), where \( \vec{h} \) is the nine-dimensional vector form of \( H \).
- Constructing Matrix \( A \): For each pair of corresponding points, I used the
construct_A_matrix
function to build the matrix \( A \):
\[
A =
\begin{bmatrix}
-x_i & -y_i & -1 & 0 & 0 & 0 & x_i' x_i & x_i' y_i & x_i' \\
0 & 0 & 0 & -x_i & -y_i & -1 & y_i' x_i & y_i' y_i & y_i'
\end{bmatrix}
\]
- Solving for \( H \): I employed Singular Value Decomposition (SVD) to solve the above equation (via the
get_homography_matrix
function):
\[
A = U \Sigma V^T
\]
\( H \) corresponds to the column of \( V \) associated with the smallest singular value, reshaped into a \( 3 \times 3 \) matrix.
-
Image Perspective Transformation and Composition:
Main Function Descriptions:
click_event
: Handles mouse click events, records the points selected by the user on the background image, and draws green circles at the clicked positions.
draw_circle
: Draws circles at specified positions to mark the points selected by the user.
add_mouse_response
: Sets up the mouse callback function and waits for the user to input four points.
construct_A_matrix
: Constructs the matrix \( A \) based on the corresponding points, used to compute the homography matrix.
get_homography_matrix
: Uses the SVD method to solve for the homography matrix \( H \).
composite_image
: Uses the computed \( H \) to perform a perspective transformation on the target image and composes it with the background image.