Methodology:

Image Pre-processing:
The initial step involved loading the provided images and ensuring uniformity in their dimensions by resizing.
from matplotlib import pyplot as plt
import cv2

# Load the two images
img1 = cv2.imread('/mnt/data/IMG1.jpg', cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread('/mnt/data/IMG2.jpg', cv2.IMREAD_GRAYSCALE)

# Resize the images if they are not of the same size
if img1.shape != img2.shape:
img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))

# Display the images
fig, ax = plt.subplots(1, 2, figsize=(12, 6))

ax[0].imshow(img1, cmap='gray')
ax[0].set_title('Image 1')
ax[0].axis('off')

ax[1].imshow(img2, cmap='gray')
ax[1].set_title('Image 2')
ax[1].axis('off')

plt.tight_layout()
plt.show()
image.png
Feature Detection & Matching:
Utilizing the ORB (Oriented FAST and Rotated BRIEF) algorithm, distinct keypoints in both images were pinpointed. These keypoints, representative of unique image features, were then matched between the two images using a Brute-Force Matcher. This step was pivotal in determining corresponding points in the two images, illustrating the car's movement.
# Initialize ORB detector
orb = cv2.ORB_create()

# Find keypoints and descriptors with ORB
keypoints1, descriptors1 = orb.detectAndCompute(img1, None)
keypoints2, descriptors2 = orb.detectAndCompute(img2, None)

# Create BFMatcher object
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

# Match descriptors
matches = bf.match(descriptors1, descriptors2)

# Sort matches based on their distances
matches = sorted(matches, key=lambda x: x.distance)

# Draw the matches
img_matches = cv2.drawMatches(img1, keypoints1, img2, keypoints2, matches[:50], None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)

# Display the matches
plt.figure(figsize=(14, 7))
plt.imshow(img_matches)
plt.title('Key Point Matches')
plt.axis('off')
plt.show()
image.png
The average displacement of the car between the two images is approximately:
Δx≈−10.44 pixels (in the horizontal direction)
y≈−23.62 pixels (in the vertical direction)
The negative sign indicates the direction of movement. For instance, a negative Δx suggests that the car moved to the left, and a negative Δy implies an upward movement.
Motion Estimation:
With the matched keypoints, we calculated the average displacement, signifying the car's movement in the image domain.
Reference Object-based Physical Distance Calculation:
A vital step was identifying a consistent reference object in both images - a white pipe cap on the road. This object's diameter in the image was gauged, and with its known real-world diameter (assumed 15 cm), we derived a pixel-to-real-world ratio. This conversion metric was essential to translate the car's pixel-based movement into an actual physical distance.
image.png
The corrected detection of the white pipe cap (south of the car) is highlighted with a green contour in the displayed region of interest. Based on our refined detection, the diameter of the pipe cap is approximately 19 pixels.
Given that the actual diameter of the pipe cap is assumed to be 0.15 meters (or 1515 cm), we can compute the new pixel-to-real-world ratio.
Pixel-to-Real-World Ratio = (0.15 meters / 0.19 pixels)​
Using this updated ratio, we can convert the car's movement from pixels to meters.
# Compute the updated pixel-to-real-world ratio
pixel_to_real_world_ratio_corrected = 0.15 / diameter_pixels_corrected

# Convert the car's movement from pixels to meters using the updated ratio
dx_meters_corrected = avg_dx * pixel_to_real_world_ratio_corrected
dy_meters_corrected = avg_dy * pixel_to_real_world_ratio_corrected

dx_meters_corrected, dy_meters_corrected
RESULT
(-0.08245665084712893, -0.18649515913735973)
Δx≈−0.0825 meters (or -8.25 cm) in the horizontal direction.
Δy≈−0.1865 meters (or -18.65 cm) in the vertical direction.
Speed Estimation:
Let's assume an average shot-to-shot time of 1 second. If you know the exact time or have a specific iPhone model in mind, please let me know, and I can adjust accordingly.
Given this interval, we can compute the speed of the car in both the x and y directions using the formula:
Speed= Distance/Time
# Time interval between the two frames (in seconds)
time_interval = 1.0 # Assuming 1 second for shot-to-shot time

# Compute the speed of the car in both x and y directions
speed_x = dx_meters_corrected / time_interval
speed_y = dy_meters_corrected / time_interval

# Compute the overall speed (magnitude) of the car
overall_speed = np.sqrt(speed_x**2 + speed_y**2)

speed_x, speed_y, overall_speed
RESULT
(-0.08245665084712893, -0.18649515913735973, 0.20391062662498602)

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.