OpenCV Images not being Shown in Output

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật