image_output_1.png
from PIL import Image

import matplotlib.pyplot as plt

import os

import pandas as pd

import random

import read_write_model

root_dir = '/kaggle/input/image-matching-challenge-2023'
train_labels_file = os.path.join(root_dir, 'train/train_labels.csv')
train_labels = pd.read_csv(train_labels_file)

train_labels.columns

dataset_counts = train_labels['dataset'].value_counts()
scene_counts = train_labels['scene'].value_counts()

# Visualize images
num_images_to_show = 5
random_image_indices = random.sample(range(len(train_labels)), num_images_to_show)

fig, axes = plt.subplots(1, num_images_to_show, figsize=(20, 5))
train_folder = os.path.join(root_dir, 'train')

for i, image_index in enumerate(random_image_indices):
    relative_image_path = train_labels.iloc[image_index]['image_path']
    image_path = os.path.join(train_folder, relative_image_path)
    img = Image.open(image_path)
    axes[i].imshow(img)
    axes[i].set_title(f"Dataset: {train_labels.iloc[image_index]['dataset']} | Scene: {train_labels.iloc[image_index]['scene']}")
    axes[i].axis('off')

plt.show()

def plot_sfm_3d_reconstruction(reconstruction, num_points=1000, num_cameras=50):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    point3D_ids = list(reconstruction[2].keys())
    selected_point3D_ids = random.sample(point3D_ids, min(num_points, len(point3D_ids)))
    for point3D_id in selected_point3D_ids:
        point3D = reconstruction[2][point3D_id]
        ax.scatter(point3D.xyz[0], point3D.xyz[1], point3D.xyz[2], c='b', marker='o')
    image_ids = list(reconstruction[1].keys())
    selected_image_ids = random.sample(image_ids, min(num_cameras, len(image_ids)))
    for image_id in selected_image_ids:
        image = reconstruction[1][image_id]
        camera_center = -image.qvec2rotmat().T @ image.tvec
        ax.scatter(camera_center[0], camera_center[1], camera_center[2], c='r', marker='^')
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    plt.show()
sample_sfm_folder = os.path.join(train_folder, 'urban/kyiv-puppet-theater/sfm')
cameras, images, points3D = read_write_model.read_model(sample_sfm_folder)
reconstruction = (cameras, images, points3D)
plot_sfm_3d_reconstruction(reconstruction)