LLDataset / generate_clahe_dataset.py
joel-unist's picture
Add files using upload-large-folder tool
f67b773 verified
import cv2
import os
import shutil
import numpy as np
from tqdm import tqdm
def apply_clahe_to_image(image, clip_limit=2.0, tile_grid_size=(8, 8)):
"""
์ด๋ฏธ์ง€์— CLAHE๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค (LAB ์ƒ‰์ƒ ๊ณต๊ฐ„์˜ L ์ฑ„๋„ ์‚ฌ์šฉ).
์ƒ‰์ƒ(RGB) ์ •๋ณด๋Š” ์œ ์ง€๋ฉ๋‹ˆ๋‹ค.
"""
if image is None:
return None
# 1. BGR -> LAB ๋ณ€ํ™˜
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
# 2. CLAHE ์ƒ์„ฑ ๋ฐ L ์ฑ„๋„์— ์ ์šฉ
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size)
l_clahe = clahe.apply(l)
# 3. ์ฑ„๋„ ๋ณ‘ํ•ฉ ๋ฐ ๋‹ค์‹œ BGR๋กœ ๋ณ€ํ™˜
lab_merge = cv2.merge((l_clahe, a, b))
final_img = cv2.cvtColor(lab_merge, cv2.COLOR_LAB2BGR)
return final_img
def process_dataset(src_root, dst_root):
# ํ™•์žฅ์ž ์ •์˜
img_extensions = ['.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff']
print(f"Dataset Processing: {src_root} -> {dst_root}")
# os.walk๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•˜์œ„ ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์กฐ(train, val ๋“ฑ)๋ฅผ ๊ทธ๋Œ€๋กœ ์ˆœํšŒ
for root, dirs, files in os.walk(src_root):
# ํ˜„์žฌ ๊ฒฝ๋กœ์˜ ์ƒ๋Œ€ ๊ฒฝ๋กœ ๊ณ„์‚ฐ (์˜ˆ: train/images)
rel_path = os.path.relpath(root, src_root)
current_dst_dir = os.path.join(dst_root, rel_path)
# ๋Œ€์ƒ ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ
os.makedirs(current_dst_dir, exist_ok=True)
# ํŒŒ์ผ ์ˆœํšŒ
for file in tqdm(files, desc=f"Processing {rel_path}", leave=False):
src_file_path = os.path.join(root, file)
dst_file_path = os.path.join(current_dst_dir, file)
file_ext = os.path.splitext(file)[1].lower()
if file_ext in img_extensions:
# [์ด๋ฏธ์ง€ ํŒŒ์ผ] CLAHE ์ ์šฉ ํ›„ ์ €์žฅ
img = cv2.imread(src_file_path)
new_img = apply_clahe_to_image(img, clip_limit=2.0, tile_grid_size=(8,8))
if new_img is not None:
cv2.imwrite(dst_file_path, new_img)
else:
print(f"Warning: Could not read image {src_file_path}")
else:
# [๋ผ๋ฒจ(txt), yaml, ๊ธฐํƒ€ ํŒŒ์ผ] ๋‹จ์ˆœ ๋ณต์‚ฌ
shutil.copy2(src_file_path, dst_file_path)
print("\n" + "="*50)
print("โœ… ๋ชจ๋“  ์ฒ˜๋ฆฌ๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")
print(f"์ €์žฅ ์œ„์น˜: {os.path.abspath(dst_root)}")
print("โš ๏ธ ์ฃผ์˜: ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ํด๋” ์•ˆ์˜ 'exdark.yaml' ํŒŒ์ผ ๋‚ด๋ถ€ ๊ฒฝ๋กœ(path)๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”!")
print("="*50)
if __name__ == '__main__':
# ์›๋ณธ ๋ฐ์ดํ„ฐ์…‹ ๊ฒฝ๋กœ
SOURCE_DIR = "./CNTSSS"
# ์ €์žฅํ•  ๋ฐ์ดํ„ฐ์…‹ ๊ฒฝ๋กœ
DEST_DIR = "./CNTSSS_CLAHE"
if not os.path.exists(SOURCE_DIR):
print(f"Error: ์›๋ณธ ๊ฒฝ๋กœ '{SOURCE_DIR}'๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")
else:
process_dataset(SOURCE_DIR, DEST_DIR)