Popravki
parent
1742437304
commit
bd76aa0534
111
assignment4/]
111
assignment4/]
|
@ -1,111 +0,0 @@
|
|||
import numpy as np
|
||||
import numpy.typing as npt
|
||||
from matplotlib import pyplot as plt
|
||||
import cv2
|
||||
import uz_framework.image as uz_image
|
||||
import uz_framework.text as uz_text
|
||||
import os
|
||||
|
||||
##############################################
|
||||
# EXCERCISE 1: Exercise 1: Image derivatives #
|
||||
##############################################
|
||||
|
||||
def ex1():
|
||||
one_a()
|
||||
one_b()
|
||||
|
||||
def one_a() -> None:
|
||||
img = uz_image.imread_gray("data/graf/graf_a.jpg", uz_image.ImageType.float64)
|
||||
sigmas = [3, 6, 9, 12]
|
||||
# Plot the points
|
||||
fig, axs = plt.subplots(2, len(sigmas))
|
||||
fig.suptitle("Hessian corner detection")
|
||||
|
||||
for i, sigma in enumerate(sigmas):
|
||||
determinant, hessian_points = uz_image.hessian_points(img, sigma, 0.004)
|
||||
# Plot determinant
|
||||
axs[0, i].imshow(determinant)
|
||||
axs[0, i].set_title(f"Sigma: {sigma}")
|
||||
# Plot grayscale image
|
||||
axs[1, i].imshow(img, cmap="gray")
|
||||
# Plot scatter hessian points (x, y)
|
||||
axs[1, i].scatter(hessian_points[:, 1], hessian_points[:, 0], s=20, c="r", marker="x")
|
||||
|
||||
plt.show()
|
||||
|
||||
def one_b() -> None:
|
||||
img = uz_image.imread_gray("data/graf/graf_a.jpg", uz_image.ImageType.float64)
|
||||
sigmas = [3, 6, 9]
|
||||
# Plot the points
|
||||
fig, axs = plt.subplots(2, len(sigmas))
|
||||
fig.suptitle("Harris corner detection")
|
||||
|
||||
for i, sigma in enumerate(sigmas):
|
||||
determinant, harris_points = uz_image.harris_detector(img, sigma, treshold=1e-6)
|
||||
# Plot determinant
|
||||
axs[0, i].imshow(determinant)
|
||||
axs[0, i].set_title(f"Sigma: {sigma}")
|
||||
# Plot grayscale image
|
||||
axs[1, i].imshow(img, cmap="gray")
|
||||
# Plot scatter hessian points
|
||||
axs[1, i].scatter(harris_points[:, 1], harris_points[:, 0], s=20, c="r", marker="x")
|
||||
|
||||
plt.show()
|
||||
|
||||
def ex2():
|
||||
two_a()
|
||||
|
||||
def two_a() -> None:
|
||||
"""
|
||||
Hello
|
||||
"""
|
||||
graph_a_small = uz_image.imread_gray("data/graf/graf_a_small.jpg", uz_image.ImageType.float64)
|
||||
graph_b_small = uz_image.imread_gray("data/graf/graf_b_small.jpg", uz_image.ImageType.float64)
|
||||
|
||||
# Get the keypoints
|
||||
_, graph_a_keypoints = uz_image.harris_detector(graph_a_small, 3, treshold=1e-6)
|
||||
_, graph_b_keypoints = uz_image.harris_detector(graph_b_small, 3, treshold=1e-6)
|
||||
|
||||
# Get the descriptors
|
||||
graph_a_descriptors = uz_image.simple_descriptors(graph_a_small, graph_a_keypoints[:,0], graph_a_keypoints[:,1])
|
||||
graph_b_descriptors = uz_image.simple_descriptors(graph_b_small, graph_b_keypoints[:,0], graph_b_keypoints[:,1])
|
||||
|
||||
# Find the correspondences
|
||||
matches_a = uz_image.find_correspondences(graph_a_descriptors, graph_b_descriptors)
|
||||
matches_b = uz_image.find_correspondences(graph_b_descriptors, graph_a_descriptors)
|
||||
|
||||
matches_a_coordinates = []
|
||||
matche_b_coordinates = []
|
||||
for i, match in enumerate(matches_a):
|
||||
if i % 2 == 0: # plot every second one
|
||||
if np.flip(match) in matches_b: # Check if the match is reciprocal
|
||||
print(match)
|
||||
print(np.flip(match))
|
||||
print(matches_b)
|
||||
print(np.argwhere(matches_b == np.flip(match)))
|
||||
matches_a_coordinates.append(np.flip(graph_a_keypoints[match[0]]))
|
||||
matche_b_coordinates.append(np.flip(graph_b_keypoints[match[1]]))
|
||||
else:
|
||||
print("Not reciprocal")
|
||||
|
||||
# Plot the matches
|
||||
uz_image.display_matches(graph_a_small, matches_a_coordinates, graph_b_small, matche_b_coordinates)
|
||||
|
||||
def two_b() -> None:
|
||||
"""
|
||||
jjjjj
|
||||
"""
|
||||
|
||||
|
||||
|
||||
|
||||
# ######## #
|
||||
# SOLUTION #
|
||||
# ######## #
|
||||
|
||||
def main():
|
||||
#ex1()
|
||||
ex2()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
|
@ -11,7 +11,7 @@ import os
|
|||
##############################################
|
||||
|
||||
def ex1():
|
||||
one_a()
|
||||
#one_a()
|
||||
one_b()
|
||||
|
||||
def one_a() -> None:
|
||||
|
@ -53,8 +53,8 @@ def one_b() -> None:
|
|||
plt.show()
|
||||
|
||||
def ex2():
|
||||
two_a()
|
||||
#two_b()
|
||||
#two_a()
|
||||
two_b()
|
||||
|
||||
def two_a() -> None:
|
||||
"""
|
||||
|
@ -100,14 +100,12 @@ def two_b() -> None:
|
|||
|
||||
a, b = uz_image.find_matches(graph_a_small, graph_b_small)
|
||||
|
||||
print(a)
|
||||
print(b)
|
||||
uz_image.display_matches(graph_a_small, a, graph_b_small, b)
|
||||
|
||||
|
||||
def ex3():
|
||||
three_a()
|
||||
#three_b()
|
||||
#three_a()
|
||||
three_b()
|
||||
|
||||
def three_a() -> None:
|
||||
"""
|
||||
|
@ -158,10 +156,10 @@ def three_b() -> None:
|
|||
"""
|
||||
Hi
|
||||
"""
|
||||
image_a = uz_image.imread_gray("data/graf/graf_a.jpg", uz_image.ImageType.float64)
|
||||
image_b = uz_image.imread_gray("data/graf/graf_b.jpg", uz_image.ImageType.float64)
|
||||
#image_a = uz_image.imread_gray("datam/img1.jpg", uz_image.ImageType.float64)
|
||||
#image_b = uz_image.imread_gray("datam/img2.jpg", uz_image.ImageType.float64)
|
||||
#image_a = uz_image.imread_gray("data/graf/graf_a.jpg", uz_image.ImageType.float64)
|
||||
#image_b = uz_image.imread_gray("data/graf/graf_b.jpg", uz_image.ImageType.float64)
|
||||
image_a = uz_image.imread_gray("datam/img1.jpg", uz_image.ImageType.float64)
|
||||
image_b = uz_image.imread_gray("datam/img2.jpg", uz_image.ImageType.float64)
|
||||
#image_a = uz_image.imread_gray("data/newyork/newyork_a.jpg", uz_image.ImageType.float64)
|
||||
#image_b = uz_image.imread_gray("data/newyork/newyork_b.jpg", uz_image.ImageType.float64)
|
||||
# Does not work for newyork dataset, becouse the keypoints are not reciprocal
|
||||
|
|
|
@ -1057,7 +1057,7 @@ def display_matches(I1, pts1, I2, pts2):
|
|||
|
||||
def find_matches(image_a: npt.NDArray[np.float64],
|
||||
image_b: npt.NDArray[np.float64],
|
||||
sigma=6, treshold=1e-6):
|
||||
sigma=3, treshold=1e-6):
|
||||
"""
|
||||
Finds matches between two images.
|
||||
|
||||
|
@ -1128,11 +1128,22 @@ def find_matches(image_a: npt.NDArray[np.float64],
|
|||
|
||||
print("[+] Correspondences reciprocated")
|
||||
|
||||
# Map correspondences to keypoints
|
||||
image_a_keypoints = np.flip(image_a_keypoints[correspondences_a[:, 0]])
|
||||
image_b_keypoints = np.flip(image_b_keypoints[correspondences_b[:, 0]])
|
||||
# Now map correspondences to the keypoints
|
||||
def map_indexes_to_points(a_points, b_points, corrs_a, corrs_b, img_a_k, img_b_k):
|
||||
for correspondence in corrs_a:
|
||||
ix = np.argwhere(correspondence[0] == corrs_b[:, 1])
|
||||
if ix.size > 0:
|
||||
a_points.append(np.flip(img_a_k[correspondence[0]]))
|
||||
b_points.append(np.flip(img_b_k[correspondence[1]]))
|
||||
|
||||
return image_a_keypoints, image_b_keypoints
|
||||
image_a_points = []
|
||||
image_b_points = []
|
||||
|
||||
map_indexes_to_points(image_a_points, image_b_points, correspondences_a, correspondences_b, image_a_keypoints, image_b_keypoints)
|
||||
map_indexes_to_points(image_b_points, image_a_points, correspondences_b, correspondences_a, image_b_keypoints, image_a_keypoints)
|
||||
print("[+] Correspondences mapped to points")
|
||||
|
||||
return np.array(image_a_points),np.array(image_b_points)
|
||||
|
||||
|
||||
def estimate_homography(image_a: npt.NDArray[np.float64],
|
||||
|
@ -1164,7 +1175,7 @@ def estimate_homography(image_a: npt.NDArray[np.float64],
|
|||
|
||||
def ransac(image_a: npt.NDArray[np.float64], correspondences_a: npt.NDArray[np.float64],
|
||||
image_b: npt.NDArray[np.float64], correspondences_b: npt.NDArray[np.float64],
|
||||
iterations: int = 10000,
|
||||
iterations: int = 1000,
|
||||
threshold: float = 1.5):
|
||||
"""
|
||||
RANSAC algorithm for estimating homography.
|
||||
|
|
Loading…
Reference in New Issue