2022-11-26 14:42:48 +01:00
|
|
|
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():
|
2022-11-28 10:30:48 +01:00
|
|
|
#one_a()
|
2022-11-26 16:06:18 +01:00
|
|
|
one_b()
|
2022-11-26 14:42:48 +01:00
|
|
|
|
|
|
|
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))
|
2022-11-26 16:06:18 +01:00
|
|
|
fig.suptitle("Hessian corner detection")
|
2022-11-26 14:42:48 +01:00
|
|
|
|
|
|
|
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")
|
2022-11-26 18:02:39 +01:00
|
|
|
# Plot scatter hessian points (x, y)
|
2022-11-26 16:06:18 +01:00
|
|
|
axs[1, i].scatter(hessian_points[:, 1], hessian_points[:, 0], s=20, c="r", marker="x")
|
2022-11-26 14:42:48 +01:00
|
|
|
|
|
|
|
plt.show()
|
|
|
|
|
2022-11-26 16:06:18 +01:00
|
|
|
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()
|
2022-11-26 18:02:39 +01:00
|
|
|
|
|
|
|
def ex2():
|
2022-11-28 10:30:48 +01:00
|
|
|
#two_a()
|
|
|
|
two_b()
|
2022-11-26 18:02:39 +01:00
|
|
|
|
|
|
|
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
|
2022-11-26 23:03:08 +01:00
|
|
|
print(np.argwhere(matches_b == np.flip(match)))
|
2022-11-26 18:02:39 +01:00
|
|
|
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:
|
|
|
|
"""
|
2022-11-27 16:46:25 +01:00
|
|
|
also as two_c as it has improved method included
|
2022-11-26 18:02:39 +01:00
|
|
|
"""
|
2022-11-27 12:58:38 +01:00
|
|
|
#graph_a_small = uz_image.imread_gray("datam/img1.jpg", uz_image.ImageType.float64)
|
|
|
|
#graph_b_small = uz_image.imread_gray("datam/img2.jpg", uz_image.ImageType.float64)
|
|
|
|
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)
|
2022-11-26 18:02:39 +01:00
|
|
|
|
2022-11-26 23:03:08 +01:00
|
|
|
a, b = uz_image.find_matches(graph_a_small, graph_b_small)
|
2022-11-27 12:58:38 +01:00
|
|
|
|
2022-11-26 23:03:08 +01:00
|
|
|
uz_image.display_matches(graph_a_small, a, graph_b_small, b)
|
2022-11-26 18:02:39 +01:00
|
|
|
|
2022-11-27 12:58:38 +01:00
|
|
|
|
|
|
|
def ex3():
|
2022-11-28 10:30:48 +01:00
|
|
|
#three_a()
|
|
|
|
three_b()
|
2022-11-27 12:58:38 +01:00
|
|
|
|
|
|
|
def three_a() -> None:
|
|
|
|
"""
|
|
|
|
hello
|
|
|
|
"""
|
|
|
|
keypoints_path = ["data/newyork/newyork.txt", "data/graf/graf.txt"]
|
|
|
|
images_a_path = ["data/newyork/newyork_a.jpg", "data/graf/graf_a.jpg"]
|
|
|
|
images_b_path = ["data/newyork/newyork_b.jpg", "data/graf/graf_b.jpg"]
|
2022-11-27 16:46:25 +01:00
|
|
|
|
2022-11-27 12:58:38 +01:00
|
|
|
def map_keypoints(keypoints):
|
|
|
|
# Map the keypoints
|
|
|
|
a_points =[]
|
|
|
|
b_points = []
|
|
|
|
for row in keypoints:
|
|
|
|
a_points.append((row[0], row[1]))
|
|
|
|
b_points.append((row[2], row[3]))
|
|
|
|
return np.array(a_points), np.array(b_points)
|
|
|
|
|
|
|
|
fig, axs = plt.subplots(4, 2)
|
|
|
|
fig.suptitle("Transformation and rotation using homography")
|
|
|
|
|
|
|
|
for i in range(len(keypoints_path)):
|
|
|
|
|
|
|
|
keypoints = np.loadtxt(keypoints_path[i], dtype=np.float64)
|
|
|
|
image_a = uz_image.imread_gray(images_a_path[i], uz_image.ImageType.float64)
|
|
|
|
image_b = uz_image.imread_gray(images_b_path[i], uz_image.ImageType.float64)
|
|
|
|
|
|
|
|
axs[i*2, 0].imshow(image_a, cmap="gray")
|
|
|
|
axs[i*2, 1].imshow(image_b, cmap="gray")
|
|
|
|
axs[i*2, 0].set_title("A")
|
|
|
|
axs[i*2, 1].set_title("B")
|
|
|
|
|
|
|
|
homography_matrix = uz_image.estimate_homography(image_a, image_b, keypoints)
|
|
|
|
img_output = cv2.warpPerspective(image_a, homography_matrix, (image_a.shape[1], image_a.shape[0]))
|
|
|
|
axs[i*2+1, 0].imshow(img_output, cmap="gray")
|
|
|
|
axs[i*2+1, 0].set_title("A transformed")
|
|
|
|
|
|
|
|
# invert keypoints
|
|
|
|
keypoints[:,[0, 1, 2, 3]] = keypoints[:,[2, 3, 0, 1]]
|
|
|
|
homography_matrix = uz_image.estimate_homography(image_a, image_b, keypoints)
|
|
|
|
img_output = cv2.warpPerspective(image_b, homography_matrix, (image_b.shape[1], image_b.shape[0]))
|
|
|
|
axs[i*2+1, 1].imshow(img_output, cmap="gray")
|
|
|
|
axs[i*2+1, 1].set_title("B transformed")
|
|
|
|
plt.show()
|
2022-11-27 16:46:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
def three_b() -> None:
|
|
|
|
"""
|
|
|
|
Hi
|
|
|
|
"""
|
2022-11-28 10:30:48 +01:00
|
|
|
#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)
|
2022-11-27 16:46:25 +01:00
|
|
|
#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
|
|
|
|
|
|
|
|
# Get the keypoints
|
|
|
|
keypoints_a, keypoints_b = uz_image.find_matches(image_a, image_b, sigma=3)
|
|
|
|
|
|
|
|
hm, best_inliers = uz_image.ransac(image_a,keypoints_a, image_b, keypoints_b)
|
|
|
|
|
|
|
|
# Plot the matches
|
|
|
|
|
|
|
|
def map_keypoints(keypoints):
|
|
|
|
# Map the keypoints
|
|
|
|
a_points =[]
|
|
|
|
b_points = []
|
|
|
|
for row in keypoints:
|
|
|
|
a_points.append((row[0], row[1]))
|
|
|
|
b_points.append((row[2], row[3]))
|
|
|
|
return np.array(a_points), np.array(b_points)
|
2022-11-26 16:06:18 +01:00
|
|
|
|
2022-11-27 16:46:25 +01:00
|
|
|
a,b = map_keypoints(best_inliers)
|
|
|
|
|
|
|
|
uz_image.display_matches(image_a, a, image_b, b)
|
|
|
|
|
|
|
|
|
2022-11-26 14:42:48 +01:00
|
|
|
# ######## #
|
|
|
|
# SOLUTION #
|
|
|
|
# ######## #
|
|
|
|
|
|
|
|
def main():
|
2022-11-26 18:02:39 +01:00
|
|
|
#ex1()
|
2022-11-27 12:58:38 +01:00
|
|
|
#ex2()
|
|
|
|
ex3()
|
2022-11-26 14:42:48 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|