import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
img1_path = '/home/marco/building_demo_test_set_1/DJI_20230927080749_0001_V.JPG'
img2_path = '/home/marco/building_demo_test_set_1/DJI_20230927080751_0002_V.JPG'
label1_path = '/home/marco/building_demo_test_set_1/labels/DJI_20230927080749_0001_V.txt'
label2_path = '/home/marco/building_demo_test_set_1/labels/DJI_20230927080751_0002_V.txt'
# Load images
img1 = cv.imread(img1_path)
img2 = cv.imread(img2_path)
if img1 is None or img2 is None:
print("Error loading images")
exit()
# Load labels
def load_labels(label_path):
with open(label_path, 'r') as file:
labels = file.readlines()
return labels
labels1 = load_labels(label1_path)
labels2 = load_labels(label2_path)
# Display the images with labels
def display_image_with_labels(img, labels, title):
img_copy = img.copy()
img_height, img_width = img_copy.shape[:2]
for label in labels:
data = list(map(float, label.strip().split()))
class_id, x_center, y_center, width, height = data
x_center, y_center = x_center * img_width, y_center * img_height
width, height = width * img_width, height * img_height
x1, y1 = int(x_center - width / 2), int(y_center - height / 2)
x2, y2 = int(x_center + width / 2), int(y_center + height / 2)
cv.rectangle(img_copy, (x1, y1), (x2, y2), (0, 255, 0), 2)
plt.imshow(cv.cvtColor(img_copy, cv.COLOR_BGR2RGB))
plt.title(title)
plt.axis('off')
# Detect ORB keypoints and descriptors
orb = cv.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)
if des1 is None or des2 is None:
print("Error detecting keypoints/descriptors")
exit()
# Match descriptors
bf = cv.BFMatcher(cv.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key=lambda x: x.distance)
if len(matches) == 0:
print("No matches found")
exit()
# Extract matched keypoints
src_pts = np.float32([kp1[m.queryIdx].pt for m in matches]).reshape(-1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in matches]).reshape(-1, 2)
# Find homography matrix
H, mask = cv.findHomography(src_pts, dst_pts, cv.RANSAC, 5.0)
if H is None:
print("Error computing homography")
exit()
# Transform bounding boxes from img1 to img2
def transform_bounding_boxes(labels, H, img_shape):
img_height, img_width = img_shape[:2]
transformed_labels = []
for label in labels:
data = list(map(float, label.strip().split()))
class_id, x_center, y_center, width, height = data
x_center, y_center = x_center * img_width, y_center * img_height
width, height = width * img_width, height * img_height
x1, y1 = x_center - width / 2, y_center - height / 2
x2, y2 = x_center + width / 2, y_center + height / 2
pts = np.float32([[x1, y1], [x2, y1], [x2, y2], [x1, y2]]).reshape(-1, 1, 2)
transformed_pts = cv.perspectiveTransform(pts, H)
tx1, ty1 = np.min(transformed_pts[:, 0, :], axis=0)
tx2, ty2 = np.max(transformed_pts[:, 0, :], axis=0)
t_width = tx2 - tx1
t_height = ty2 - ty1
t_x_center = tx1 + t_width / 2
t_y_center = ty1 + t_height / 2
# Normalize the transformed coordinates
t_x_center /= img_width
t_y_center /= img_height
t_width /= img_width
t_height /= img_height
transformed_label = f"{class_id} {t_x_center} {t_y_center} {t_width} {t_height}n"
transformed_labels.append(transformed_label)
return transformed_labels
# Transform bounding boxes from img1 to img2
transformed_labels1_to_2 = transform_bounding_boxes(labels1, H, img2.shape)
# Combine transformed labels with original labels of img2
combined_labels2 = labels2 + transformed_labels1_to_2
# Display the images with original and transformed labels
plt.figure(figsize=(20, 10))
plt.subplot(1, 2, 1)
display_image_with_labels(img1, labels1, 'Image 1 with Labels')
plt.subplot(1, 2, 2)
display_image_with_labels(img2, combined_labels2, 'Image 2 with Combined Labels')
plt.show()
I tried running this code in VS Code, but I cannot have the images shown. Why is this the case?
I intended to show the images themselves with the YOLOv7 label.txt so I can visualize them. Somehow they don’t.
The logic of the code is like this:
Let’s say we have these two images with their bounding box, some bounding boxes on a feature appear in image 1 while some bounding boxes of the same feature do not appear in image 2. I want both images to have the bounding boxes (say, image 2 should have the missing bounding boxes) in a feature-matching fashion.