Add annotations

main
Spagnolo Gasper 2022-12-27 11:44:31 +01:00
parent cd45a6bcb8
commit 77c062f5b4
1 changed files with 46 additions and 6 deletions

View File

@ -1467,6 +1467,11 @@ def sift(grayscale_image: npt.NDArray[np.float64],
def get_disparity(left_image: npt.NDArray[np.float64], right_image: npt.NDArray[np.float64],\
window_size: int = 35, patch_size: int = 11, reduce_size: float = 0.3) -> npt.NDArray[np.float64]:
"""
Disparity algorithm
Accepts left & right image, window size, patch size, and reduce size
Returns diff in images, from left image prespsective
"""
if(patch_size % 2 == 0 or window_size % 2 == 0):
raise ValueError("Patch/window size must be odd")
@ -1548,6 +1553,11 @@ def draw_epiline(l,h,w):
plt.gca().invert_yaxis()
def fundamential_matrix(P1, P2):
"""
Fundamental matrix estimation
accepts points from left and right image
Returns fundamental matrix
"""
# P1 and P2 are Nx2 vectors of points
# returns: fundamental matrix
@ -1581,6 +1591,11 @@ def fundamential_matrix(P1, P2):
def get_epipolar_lines(p1, p2):
"""
compute Epipolar lines
Accepts points p1 and p2 from images
Returns epipolar lines
"""
F = fundamential_matrix(p1, p2)
p1 = np.hstack((p1, np.ones((p1.shape[0], 1))))
@ -1593,6 +1608,11 @@ def get_epipolar_lines(p1, p2):
def reprojection_error(F, p1, p2):
"""
compute reprojection error
Accepts points p1 and p2 from images and fundamental matrix
Returns reprojection error
"""
# Add a column of ones to the points, even if there is single point
p1 = np.hstack((p1, np.ones((p1.shape[0], 1))))
@ -1659,9 +1679,9 @@ def ransac_fundamental(correspondences_a: npt.NDArray[np.float64], correspondenc
iterations: int = 5000,
threshold: float = 3):
"""
RANSAC algorithm for estimating homography.
RANSAC algorithm for estimating fundamental matrix.
Accepts two images and their corresponding keypoints.
Returns the best homography matrix and the inliers.
Returns the best fundamental matrix matrix and the inliers.
"""
# Find the best homography
best_inliers = []
@ -1726,8 +1746,11 @@ def drawEllipse(mu, cov, n_std=1):
plt.gca().add_patch(ellipse)
def compute_PCA(points: npt.NDArray[np.float64], plot: bool = False):
# 1. b)
# Build matrix X
"""
Algorithm to compute PCA
accepts points, and plot bool variable
returns U, S, VT and mean
"""
X = points.T.copy()
mean = np.mean(X, axis=1)
@ -1756,6 +1779,10 @@ def compute_PCA(points: npt.NDArray[np.float64], plot: bool = False):
def plot_histogram_pca(U: npt.NDArray[np.float64], S: npt.NDArray[np.float64], VT: npt.NDArray[np.float64]):
"""
Accepts U, S, VT
Plots histogram pca
"""
eigvals = S.copy()
# Normalize eginevalues
eigvals = eigvals / np.sum(eigvals)
@ -1765,6 +1792,11 @@ def plot_histogram_pca(U: npt.NDArray[np.float64], S: npt.NDArray[np.float64], V
plt.show()
def dual_PCA(points: npt.NDArray[np.float64]):
"""
Algorithm to compute dual PCA
accepts points, and plot bool variable
returns U, S, VT and mean
"""
X = points.T.copy()
mean = np.mean(X, axis=1)
X = X - mean[:, np.newaxis]
@ -1780,6 +1812,11 @@ def dual_PCA(points: npt.NDArray[np.float64]):
return U, S, VT, mean
def read_images(data_path: str):
"""
Read images from directory
Accepts path to direcgtory
returns vectorized images
"""
imgs = np.array([])
for filename in os.listdir(data_path):
img = imread_gray(os.path.join(data_path, filename), ImageType.float64)
@ -1792,6 +1829,11 @@ def read_images(data_path: str):
return imgs.T
def LDA(points, c, n):
"""
LDA algorithm
Accepts points, number of participants in every class, number of classes
returns reprojected points into LDA space
"""
MM = np.mean(points)
Ms = []
@ -1808,8 +1850,6 @@ def LDA(points, c, n):
eig_vals, eig_vecs = np.linalg.eig(SWB)
print(eig_vecs)
Ms = np.array(Ms)
Ms = eig_vecs.T.dot(Ms.T).T