Untitled
unknown
plain_text
9 months ago
2.2 kB
4
Indexable
import os import cv2 import numpy as np import matplotlib.pyplot as plt # Hàm tính diện tích của bounding box từ định dạng YOLO def calculate_bbox_area(image_width, image_height, bbox): class_id, x_center, y_center, bbox_width, bbox_height = map(float, bbox) # Chuyển đổi tọa độ trung tâm và kích thước bbox từ tương đối sang tuyệt đối xmin = int((x_center - bbox_width/2) * image_width) ymin = int((y_center - bbox_height/2) * image_height) xmax = int((x_center + bbox_width/2) * image_width) ymax = int((y_center + bbox_height/2) * image_height) # Tính diện tích bbox width = xmax - xmin height = ymax - ymin area = width * height return area # Đọc ảnh và label YOLO def read_image_and_label(image_path, label_path): image = cv2.imread(image_path) image_height, image_width, _ = image.shape labels = np.loadtxt(label_path, dtype=str, delimiter=' ') return image_width, image_height, 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_width, image_height, labels = read_image_and_label(image_path, label_path) # Xử lý từng bbox trong labels for label in labels: bbox_area = calculate_bbox_area(image_width, image_height, label) 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