-
6/29 - (4/10000)개발일지 2024. 6. 29. 02:07
nuscene에 있는 카메라 6개 이미지를 합쳐 봤다.
학습적인 목적보다는 보는 목적때문에 합쳤음. 어떤 이미지가 사용되는지 파악하기 좋음.
import os import cv2 import numpy as np # NuScenes 데이터셋 경로 nusc_data_dir = '/data/sets/nuscenes/samples/' # 각 카메라 폴더 camera_folders = ['CAM_FRONT_LEFT','CAM_FRONT', 'CAM_FRONT_RIGHT', 'CAM_BACK_LEFT','CAM_BACK', 'CAM_BACK_RIGHT'] def extract_number(filename, name ): try: number_str = filename.split(f'__{name}__')[1].split('.')[0] return int(number_str) except IndexError: return None # "__CAM_BACK__" 뒤에 숫자가 없는 경우 처리 # 이미지 리사이즈 크기 (옵션) resize_dim = (640, 480) # 원하는 크기로 설정 # 결과 저장 디렉토리 output_dir = './combined_images' if not os.path.exists(output_dir): os.makedirs(output_dir) # 폴더별 이미지 읽기 및 처리 folder_images = [] # 모든 이미지를 읽어서 좌우 반전 후 조합 combined_images = [] for folder in camera_folders: img_path = os.path.join(nusc_data_dir, folder) images = [] files = os.listdir(os.path.join(nusc_data_dir, folder)) # 파일 이름에서 숫자를 추출하여 정렬하기 sorted_files = sorted(files, key=lambda x: extract_number(x,folder) or float('inf')) for filename in sorted_files: # 파일 이름 순으로 정렬 if filename.endswith('.jpg'): img = cv2.imread(os.path.join(img_path, filename)) if img is not None: img = cv2.resize(img, resize_dim) # 이미지 리사이징 # CAM_BACK 카메라만 좌우 반전 if 'CAM_BACK' in folder: img = cv2.flip(img, 1) images.append(img) else: print(f"Error reading image from {filename}") folder_images.append(images) # # 각 폴더의 이미지를 위아래로 합치기 combined_images = [] for imgs in zip(*folder_images): # 좌우로 3개씩 묶어서 합치기 combined_row1 = np.hstack(imgs[:3]) combined_row2 = np.hstack(imgs[3:]) combined_image = np.vstack([combined_row1, combined_row2]) combined_images.append(combined_image) # 합쳐진 이미지를 파일로 저장하거나 화면에 표시 for idx, img in enumerate(combined_images): output_path = os.path.join(output_dir, f"combined_image_{idx}.jpg") cv2.imwrite(output_path, img) print(f"Combined image saved to {output_path}") print("Processing complete.")
위 코드를 실행하면 아래 같은 이미지가 저장되어 있는것을 볼 수 있을것이다.
위치를 맞추기 위해서 좌우 반전을 했다.
앞/뒤로 긴 물체인 경우 카메라가 6개 밖에 없지만 연속적으로 보임.
반응형'개발일지' 카테고리의 다른 글
7/9 - (9/10K) (0) 2024.07.10 7/1 - (7/10000) (0) 2024.07.01 6/30 - (5/10000) (1) 2024.06.30 6/28 - (2/10000) (0) 2024.06.28 만시간의 법칙 (0) 2024.06.27