Untitled
unknown
plain_text
a year ago
2.0 kB
4
Indexable
import cv2 import numpy as np import matplotlib.pyplot as plt # Hàm tính diện tích của bounding box def calculate_bbox_area(bbox): xmin, ymin, xmax, ymax = bbox width = xmax - xmin height = ymax - ymin area = width * height return area # Đọc ảnh và label def read_image_and_label(image_path, label_path): image = cv2.imread(image_path) labels = np.loadtxt(label_path, dtype=str, delimiter=' ') return image, labels # Hàm visualize phân bố diện tích bbox def visualize_bbox_areas(bbox_areas): plt.figure(figsize=(8, 6)) plt.hist(bbox_areas, bins=50, edgecolor='black') plt.xlabel('Area of Bounding Box') plt.ylabel('Number of Bounding Boxes') plt.title('Distribution of Bounding Box Areas') plt.grid(True) plt.show() # Đường dẫn đến thư mục chứa ảnh và label image_dir = 'path/to/your/image/directory/' label_dir = 'path/to/your/label/directory/' # List để lưu trữ diện tích của các bounding box bbox_areas = [] # Đọc và xử lý từng ảnh và label for image_name in os.listdir(image_dir): if image_name.endswith('.jpg'): image_path = os.path.join(image_dir, image_name) label_path = os.path.join(label_dir, image_name.replace('.jpg', '.txt')) image, labels = read_image_and_label(image_path, label_path) # Xử lý từng bbox trong labels for label in labels: class_id, xc, yc, w, h = map(float, label) # Chuyển đổi sang tọa độ bbox tương đối xmin = int((xc - w/2) * image.shape[1]) ymin = int((yc - h/2) * image.shape[0]) xmax = int((xc + w/2) * image.shape[1]) ymax = int((yc + h/2) * image.shape[0]) # Tính diện tích bbox bbox_area = calculate_bbox_area((xmin, ymin, xmax, ymax)) bbox_areas.append(bbox_area) # Visualize phân bố diện tích bbox visualize_bbox_areas(bbox_areas)
Editor is loading...
Leave a Comment