Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:3DOM-FBK/deep-image-matching into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
franioli committed Feb 28, 2024
2 parents 69cc604 + e7af714 commit 04edd6f
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 27 deletions.
37 changes: 37 additions & 0 deletions scripts/mount_video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import cv2
import os

# Function to create video from images in a folder
def images_to_video(image_folder, output_video_path, frame_duration=0.5):
images = [img for img in os.listdir(image_folder) if img.endswith(".png")]
frame = cv2.imread(os.path.join(image_folder, images[0]))

# Get image dimensions
height, width, layers = frame.shape

# Define the video codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'mp4v') # You can change the codec as needed
#fourcc = cv2.CV_FOURCC(-1) # You can try 'H264' as well
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
video = cv2.VideoWriter(output_video_path, fourcc, 1 / frame_duration, (width, height))

for i, image in enumerate(images):
img_path = os.path.join(image_folder, image)
frame = cv2.imread(img_path)

# Resize the image to match the dimensions of the first image (if needed)
# frame = cv2.resize(frame, (width, height))

# Write the frame to the video file
video.write(frame)
print(f"Processed frame {i}")

# Release the VideoWriter object
video.release()

# Example usage
images_folder = r'C:\Users\threedom\Desktop\DIM_VIDEO\semperoper\superglue\sift_screen_resized'
output_video_path = r'C:\Users\threedom\Desktop\DIM_VIDEO\semperoper\superglue\sift_screen_resized\semperoper_sift.avi'
frame_duration = 0.5 # seconds

images_to_video(images_folder, output_video_path, frame_duration)
95 changes: 68 additions & 27 deletions scripts/show_matches.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import argparse
from pathlib import Path

Expand All @@ -9,6 +10,13 @@
pair_id_to_image_ids,
)

def generate_pairs(imgs_dir):
pairs = []
n_images = len(os.listdir(imgs_dir))
for i in range(n_images-1):
if i%2 == 0:
pairs.append((i+1, i+2))
return pairs

class ShowPairMatches:
def __init__(
Expand Down Expand Up @@ -140,11 +148,11 @@ def GeneratePlot(
pt2 = tuple(np.array(kpts1_int[match[1]]) + np.array([img0.shape[1], 0]))

# Draw a line connecting the keypoints
cv2.line(img_matches, pt1, pt2, (0, 255, 0), 2)
cv2.line(img_matches, pt1, pt2, (0, 255, 0), 1)

# Draw circles around keypoints
cv2.circle(img_matches, pt1, 5, (255, 0, 0), -1)
cv2.circle(img_matches, pt2, 5, (255, 0, 0), -1)
cv2.circle(img_matches, pt1, 3, (255, 0, 0), -1)
cv2.circle(img_matches, pt2, 3, (255, 0, 0), -1)

img_matches_resized = self.resize_image(img_matches, self.max_out_img_size)

Expand Down Expand Up @@ -179,18 +187,21 @@ def parse_args():
parser.add_argument(
"-f", "--imgsdir", type=str, help="Path to images directory", required=True
)
parser.add_argument(
"-o", "--output", type=str, help="Path to output folder", required=True
)
parser.add_argument(
"--all", action='store_true', help="Export matches for all pairs", required=False
)
parser.add_argument(
"-i",
"--images",
type=str,
help="Images IDs or names. E.g.: 'img1.jpg img2.jpg' or '37 98'. Max two images.",
required=True,
required=False,
)
parser.add_argument(
"-t", "--type", type=str, choices=["names", "ids"], required=True
)
parser.add_argument(
"-o", "--output", type=str, help="Path to output folder", required=True
"-t", "--type", type=str, choices=["names", "ids"], required=False
)
parser.add_argument(
"-m",
Expand All @@ -208,29 +219,59 @@ def parse_args():
def main():
args = parse_args()
database_path = Path(args.database)
out_file = Path(args.output)
out_dir = Path(args.output)
imgs_dir = Path(args.imgsdir)
max_size = args.max_size

print(f"database path: {database_path}")
print(f"output dir: {out_file}")

i1, i2 = args.images.split()
imgs = {
"type": args.type,
"data": (i1, i2),
}
print("images: ", imgs)

show_pair_matches = ShowPairMatches(
database_path=database_path,
imgs_dict=imgs,
imgs_dir=imgs_dir,
out_file=out_file,
max_size=max_size,
)
print(f"output dir: {out_dir}")

if args.all == False:
i1, i2 = args.images.split()
out_file = out_dir / f"{i1}_{i2}.png"
imgs = {
"type": args.type,
"data": (i1, i2),
}
print("images: ", imgs)

show_pair_matches = ShowPairMatches(
database_path=database_path,
imgs_dict=imgs,
imgs_dir=imgs_dir,
out_file=out_file,
max_size=max_size,
)

show_pair_matches.LoadDatabase()
show_pair_matches.ShowMatches()

else:
pairs = generate_pairs(imgs_dir)
print(pairs)
for pair in pairs:
i1, i2 = pair[0], pair[1]
out_file = out_dir / f"{i1}_{i2}.png"
imgs = {
"type": "ids",
"data": (i1, i2),
}
print("images: ", imgs)

show_pair_matches = ShowPairMatches(
database_path=database_path,
imgs_dict=imgs,
imgs_dir=imgs_dir,
out_file=out_file,
max_size=max_size,
)

show_pair_matches.LoadDatabase()

show_pair_matches.LoadDatabase()
show_pair_matches.ShowMatches()
try:
show_pair_matches.ShowMatches()
except:
print("No verified matches found")


if __name__ == "__main__":
Expand Down

0 comments on commit 04edd6f

Please sign in to comment.