Najsss2
parent
ecf7355b64
commit
6af7bbb4b8
|
@ -93,13 +93,63 @@ def two_b() -> None:
|
|||
"""
|
||||
jjjjj
|
||||
"""
|
||||
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("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)
|
||||
|
||||
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()
|
||||
|
||||
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"]
|
||||
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()
|
||||
|
||||
# ######## #
|
||||
# SOLUTION #
|
||||
|
@ -107,7 +157,8 @@ def two_b() -> None:
|
|||
|
||||
def main():
|
||||
#ex1()
|
||||
ex2()
|
||||
#ex2()
|
||||
ex3()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
@ -1065,17 +1065,23 @@ def find_matches(image_a: npt.NDArray[np.float64],
|
|||
"""
|
||||
|
||||
# Get the keypoints
|
||||
_, image_a_keypoints = harris_detector(image_a, 6, treshold=1e-6)
|
||||
_, image_b_keypoints = harris_detector(image_b, 6, treshold=1e-6)
|
||||
_, image_a_keypoints = harris_detector(image_a, 3, treshold=1e-6)
|
||||
_, image_b_keypoints = harris_detector(image_b, 3, treshold=1e-6)
|
||||
|
||||
print("[+] Keypoints detected")
|
||||
|
||||
# Get the descriptors
|
||||
image_a_descriptors = simple_descriptors(image_a, image_a_keypoints[:, 0], image_a_keypoints[:, 1])
|
||||
image_b_descriptors = simple_descriptors(image_b, image_b_keypoints[:, 0], image_b_keypoints[:, 1])
|
||||
|
||||
print("[+] Descriptors computed")
|
||||
|
||||
# Find correspondences
|
||||
correspondences_a = find_correspondences(image_a_descriptors, image_b_descriptors)
|
||||
correspondences_b = find_correspondences(image_b_descriptors, image_a_descriptors)
|
||||
|
||||
print("[+] Correspondences found")
|
||||
|
||||
def select_best_correspondences(c_a, d_a, c_b, d_b):
|
||||
#Find correspondances that map into same index a->b (b)
|
||||
unique, counts = np.unique(c_a[:, 1], return_counts=True)
|
||||
|
@ -1107,7 +1113,8 @@ def find_matches(image_a: npt.NDArray[np.float64],
|
|||
|
||||
correspondences_a = select_best_correspondences(correspondences_a, image_a_descriptors, correspondences_b, image_b_descriptors)
|
||||
correspondences_b = select_best_correspondences(correspondences_b, image_b_descriptors, correspondences_a, image_a_descriptors)
|
||||
|
||||
|
||||
print("[+] Correspondences filtered")
|
||||
def check_repciporacvbillity(c_a, c_b):
|
||||
for _, match in enumerate(c_a):
|
||||
if np.flip(match) not in c_b:
|
||||
|
@ -1119,8 +1126,38 @@ def find_matches(image_a: npt.NDArray[np.float64],
|
|||
correspondences_a = check_repciporacvbillity(correspondences_a, correspondences_b)
|
||||
correspondences_b = check_repciporacvbillity(correspondences_b, correspondences_a)
|
||||
|
||||
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]])
|
||||
|
||||
return image_a_keypoints, image_b_keypoints
|
||||
|
||||
|
||||
def estimate_homography(image_a: npt.NDArray[np.float64],
|
||||
image_b: npt.NDArray[np.float64],
|
||||
keypoints: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]:
|
||||
|
||||
"""
|
||||
[x_r1 yr_1 1 0 0 0 -x_t1*x_r1 -x_t1*yr_1 -x_t1]
|
||||
[0 0 0 x_r1 yr_1 1 -y_t1*x_r1 -y_t1*yr_1 -y_t1]
|
||||
....
|
||||
"""
|
||||
|
||||
# Construct the A matrix
|
||||
A = np.zeros((2 * keypoints.shape[0], 9))
|
||||
|
||||
for ix, pair in enumerate(keypoints):
|
||||
x_r, y_r, x_t, y_t = pair
|
||||
A[ix*2] = [x_r, y_r, 1, 0, 0, 0, -x_t*x_r, -x_t*y_r, -x_t]
|
||||
A[ix*2+1] = [0, 0, 0, x_r, y_r, 1, -y_t*x_r, -y_t*y_r, -y_t]
|
||||
|
||||
# Perform SVD
|
||||
_, _, V = np.linalg.svd(A)
|
||||
# Compute vector h
|
||||
h = V[-1] / V[-1][-1]
|
||||
# Reshape to 3x3
|
||||
H = h.reshape(3, 3)
|
||||
|
||||
return H
|
||||
|
|
Loading…
Reference in New Issue