-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalign_SIFT.py
More file actions
62 lines (48 loc) · 2.08 KB
/
align_SIFT.py
File metadata and controls
62 lines (48 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# align_SIFT.py
# Minimal SIFT-based alignment script (no fallback, no logging)
import os
import cv2
import numpy as np
from glob import glob
from config.config import INPUT_DIR, OUTPUT_DIR_PASS_01
from tymepkg.image_utils import preprocess_gray, convert_to_bgra, get_date_taken
from tymepkg.align import detect_keypoints_and_descriptors, match_keypoints_flann, compute_homography, warp_image
def main():
os.makedirs(OUTPUT_DIR_PASS_01, exist_ok=True)
image_paths = sorted(glob(os.path.join(INPUT_DIR, '*.jpg')))
if not image_paths:
print("No input images found.")
return
selected = input(f"Enter index of reference image (0 to {len(image_paths) - 1}): ")
try:
ref_img_path = image_paths[int(selected)]
except (IndexError, ValueError):
print("Invalid selection. Using middle image by default.")
ref_img_path = image_paths[len(image_paths) // 2]
print(f"Using '{os.path.basename(ref_img_path)}' as reference image.")
ref_img = cv2.imread(ref_img_path)
ref_gray = preprocess_gray(ref_img)
ref_bgra = convert_to_bgra(ref_img)
ref_kp, ref_desc = detect_keypoints_and_descriptors(ref_gray, method='sift')
for img_path in image_paths:
img_name = os.path.basename(img_path)
print(f"Processing '{img_name}'...")
img = cv2.imread(img_path)
img_gray = preprocess_gray(img)
img_bgra = convert_to_bgra(img)
kp, desc = detect_keypoints_and_descriptors(img_gray, method='sift')
if desc is None or len(kp) < 10:
continue
matches = match_keypoints_flann(ref_desc, desc)
if len(matches) < 10:
continue
H = compute_homography(ref_kp, kp, matches)
if H is None:
continue
aligned = warp_image(img_bgra, H, (ref_bgra.shape[1], ref_bgra.shape[0]))
date_taken = get_date_taken(img_path)
out_name = f"{date_taken}.png" if date_taken else f"aligned_{img_name[:-4]}.png"
out_path = os.path.join(OUTPUT_DIR_PASS_01, out_name)
cv2.imwrite(out_path, aligned)
if __name__ == "__main__":
main()