Download code/color_aug_class.py from qinxunhui/deepellipse-circleid: direct link, hf CLI and curl.
- Browser
- Download file 11.8 kB
-
https://huggingface.co/qinxunhui/deepellipse-circleid/resolve/main/code/color_aug_class.py
- Command line
-
hf download hf://qinxunhui/deepellipse-circleid/code/color_aug_class.py
-
curl -L -o color_aug_class.py https://huggingface.co/qinxunhui/deepellipse-circleid/resolve/main/code/color_aug_class.py
11.8 kB
| import cv2 | |
| import numpy as np | |
| import os | |
| import random | |
| import matplotlib.pyplot as plt | |
| import pickle | |
| import json | |
| from tqdm import tqdm | |
| # ================= 辅助函数 (二值化与骨架化) ================= | |
| def binarize_image(image: np.ndarray, method: str = 'adaptive', k=1) -> np.ndarray: | |
| """ | |
| k=2,默认 | |
| k=0,含有更多边缘 | |
| """ | |
| if len(image.shape) == 3: | |
| gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
| else: | |
| gray = image.copy() | |
| if method == 'adaptive': | |
| # 优化参数以保留更多细节 | |
| return cv2.adaptiveThreshold( | |
| gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, | |
| cv2.THRESH_BINARY_INV, 15, k | |
| ) | |
| elif method == 'otsu': | |
| _, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) | |
| return binary | |
| else: | |
| _, binary = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV) | |
| return binary | |
| def create_hard_mask(img): | |
| """生成硬二值Mask (0或255),用于确定变换区域""" | |
| if len(img.shape) == 3: | |
| gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
| else: | |
| gray = img | |
| blur = cv2.GaussianBlur(gray, (5, 5), 0) | |
| ret, mask = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) | |
| kernel = np.ones((3, 3), np.uint8) | |
| mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1) | |
| mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=2) | |
| if cv2.countNonZero(mask) / mask.size < 0.01: | |
| _, mask = cv2.threshold(blur, 200, 255, cv2.THRESH_BINARY_INV) | |
| mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=2) | |
| # kernel = np.ones((3, 3), np.uint8) | |
| # mask = cv2.erode(mask, kernel, iterations=2) | |
| return mask | |
| class ColorStatsDatabase: | |
| """ | |
| 负责预计算、保存和加载参考图像库的统计信息。 | |
| 针对白底黑字场景,仅计算'前景区域'的均值和方差。 | |
| """ | |
| def __init__(self, cache_file='test_color_sta.json'): | |
| # 缓存文件保存在参考文件夹的父目录或当前目录,避免污染图片文件夹 | |
| self.cache_file = cache_file | |
| self.stats_data = {} | |
| self.file_list = [] | |
| if os.path.exists(self.cache_file): | |
| self.load() | |
| else: | |
| raise ValueError("输入正确的资源文件路径") | |
| def compute_and_save(self, folder_path): | |
| self.folder_path = folder_path | |
| """遍历文件夹,计算统计量并保存到磁盘""" | |
| print(f"正在扫描并计算参考库统计量: {self.folder_path} ...") | |
| valid_ext = ('.jpg', '.jpeg', '.png') | |
| files = [f for f in os.listdir(self.folder_path) | |
| if f.lower().endswith(valid_ext) and f.startswith('v2_')] | |
| if not files: | |
| raise ValueError(f"文件夹中未找到图像: {self.folder_path}") | |
| self.stats_data = {} | |
| for filename in tqdm(files, desc="计算统计量"): | |
| filepath = os.path.join(self.folder_path, filename) | |
| img = cv2.imread(filepath) | |
| if img is None: | |
| continue | |
| mask = create_hard_mask(img) | |
| mask_bool = mask > 0 | |
| if np.sum(mask_bool) < 10: | |
| continue # 跳过无前景的图 | |
| means = [] | |
| stds = [] | |
| # 分别计算 B, G, R 通道的前景统计量 | |
| for i in range(3): | |
| channel = img[:, :, i] | |
| pixels = channel[mask_bool].astype(np.float64) | |
| means.append(float(np.mean(pixels))) | |
| stds.append(float(np.std(pixels))) | |
| self.stats_data[filename] = { | |
| 'mean': means, | |
| 'std': stds | |
| } | |
| with open(self.cache_file, 'w') as f: | |
| json.dump(self.stats_data, f, indent=2) | |
| print(f"统计量已保存至: {self.cache_file}") | |
| print(f"共处理有效图像: {len(self.stats_data)} 张") | |
| self.file_list = list(self.stats_data.keys()) | |
| def load(self): | |
| """从磁盘加载统计量""" | |
| if not os.path.exists(self.cache_file): | |
| print(f"缓存文件不存在: {self.cache_file},将重新计算...") | |
| self.compute_and_save() | |
| else: | |
| print(f"正在加载缓存统计量: {self.cache_file}") | |
| with open(self.cache_file, 'r') as f: | |
| self.stats_data = json.load(f) | |
| self.file_list = list(self.stats_data.keys()) | |
| print(f"加载完成,共 {len(self.stats_data)} 张图像统计量。") | |
| def get_random_stats(self): | |
| """随机返回一组统计量 (filename, mean, std)""" | |
| if not self.file_list: | |
| raise ValueError("数据库为空") | |
| fname = random.choice(self.file_list) | |
| data = self.stats_data[fname] | |
| return fname, data['mean'], data['std'] | |
| class CircleColorAugmentor: | |
| """ | |
| 颜色增强类:利用预计算的统计量对源图像进行硬变换(无边缘融合)。 | |
| """ | |
| def __init__(self, ref_stats_db: ColorStatsDatabase, match_std=True): | |
| self.ref_db = ref_stats_db | |
| self.match_std = match_std | |
| def augment(self, src_img, b_debug=False): | |
| """ | |
| 执行一次增强: | |
| 1. 读取源图 | |
| 2. 随机选择一组参考统计量 | |
| 3. 生成源图硬Mask | |
| 4. 计算源图前景统计量 | |
| 5. 应用线性变换并直接替换前景像素 | |
| """ | |
| # src_img = cv2.imread(src_img_path) | |
| # channels = cv2.split(src_img) | |
| # kernel = np.ones((3, 3), np.uint8) | |
| # new_ch = [] | |
| # for ch in channels: | |
| # mod = cv2.erode(ch, kernel, iterations=2) | |
| # new_ch.append(mod) | |
| # src_img = cv2.merge(new_ch) | |
| # 1. 获取随机参考统计量 (从内存缓存读取,极速) | |
| ref_fname, ref_means, ref_stds = self.ref_db.get_random_stats() | |
| # 2. 生成源图的硬 Mask (必须实时生成) | |
| hard_mask = create_hard_mask(src_img) | |
| mask_bool = hard_mask > 0 | |
| if np.sum(mask_bool) < 10: | |
| # 没检测到前景,直接返回原图和空mask | |
| if b_debug is True: | |
| return src_img, src_img.copy(), np.zeros_like(hard_mask), "No_Foreground" | |
| else: | |
| return src_img | |
| # 3. 计算源图当前前景的统计量 | |
| src_means = [] | |
| src_stds = [] | |
| for i in range(3): | |
| channel = src_img[:, :, i] | |
| pixels = channel[mask_bool].astype(np.float64) | |
| src_means.append(np.mean(pixels)) | |
| src_stds.append(np.std(pixels)) | |
| # 4. 应用变换 (直接替换,无融合) | |
| result_img = src_img.copy().astype(np.float64) | |
| for i in range(3): | |
| s_chan = src_img[:, :, i].astype(np.float64) | |
| m_s, s_s = src_means[i], src_stds[i] | |
| m_r, s_r = ref_means[i], ref_stds[i] | |
| # 提取前景像素 | |
| src_vals = s_chan[mask_bool] | |
| # 计算新值 | |
| if s_s < 1e-5: | |
| new_vals = np.full_like(src_vals, m_r) | |
| else: | |
| if self.match_std: | |
| # 匹配均值和方差 | |
| new_vals = (src_vals - m_s) * (s_r / s_s) + m_r | |
| else: | |
| # 仅匹配均值 | |
| new_vals = src_vals - m_s + m_r | |
| # 截断 | |
| new_vals = np.clip(new_vals, 0, 255) | |
| # 【关键】直接赋值给结果图像的 Mask 区域,背景保持不变 | |
| result_img[:, :, i][mask_bool] = new_vals | |
| result_img = np.clip(result_img, 0, 255).astype(np.uint8) | |
| # channels = cv2.split(result_img) | |
| # new_ch = [] | |
| # for ch in channels: | |
| # mod = cv2.dilate(ch, kernel, iterations=2) | |
| # new_ch.append(mod) | |
| # result_img = cv2.merge(new_ch) | |
| # result_img = cv2.GaussianBlur(result_img, (5, 5), 0) | |
| if b_debug is True: | |
| return src_img, result_img, hard_mask, ref_fname | |
| else: | |
| return result_img | |
| def visualize_results(src_img, tgt_img, hard_mask, ref_path, save_path): | |
| """生成对比图:源图 | 结果图 | 硬Mask | 参考信息""" | |
| src_rgb = cv2.cvtColor(src_img, cv2.COLOR_BGR2RGB) | |
| tgt_rgb = cv2.cvtColor(tgt_img, cv2.COLOR_BGR2RGB) | |
| # 硬Mask 可视化 | |
| mask_vis = hard_mask.astype(np.uint8) | |
| mask_rgb = cv2.cvtColor(mask_vis, cv2.COLOR_GRAY2RGB) | |
| fig, axes = plt.subplots(1, 4, figsize=(20, 5)) | |
| axes[0].imshow(src_rgb) | |
| axes[0].set_title("Source Image", fontsize=14, fontweight='bold') | |
| axes[0].axis('off') | |
| axes[1].imshow(tgt_rgb) | |
| axes[1].set_title(f"Target (Hard Transform)", fontsize=14, fontweight='bold') | |
| axes[1].axis('off') | |
| axes[2].imshow(mask_rgb) | |
| axes[2].set_title("Foreground Mask (Binary)", fontsize=14, fontweight='bold') | |
| axes[2].axis('off') | |
| # 第四列显示参考信息 | |
| ref_rgb = cv2.imread(ref_path) | |
| axes[3].imshow(ref_rgb) | |
| axes[3].set_title(f"Reference") | |
| axes[3].axis('off') | |
| plt.tight_layout() | |
| plt.savefig(save_path, dpi=150, bbox_inches='tight') | |
| plt.close(fig) | |
| def debug(): | |
| # ================= 配置区域 ================= | |
| path_src_folder = "/path/to/circle_id/images/ours_images" | |
| path_ref_folder = "/path/to/circle_id/images/images/" | |
| path_out_folder = "/path/to/circle_id/outputs/color_aug_hard_class_new" | |
| cache_file = os.path.join(os.path.dirname(__file__), "test_color_sta.json") | |
| ref_db = ColorStatsDatabase(cache_file) | |
| # test_color_sta.compute_and_save('/path/to/circle_id/images') | |
| MATCH_STANDARD_DEVIATION = False # 是否匹配方差 | |
| NUM_SAMPLES = 100 # 生成测试图数量 | |
| # =========================================== | |
| os.makedirs(path_out_folder, exist_ok=True) | |
| # 2. 初始化增强器 | |
| augmentor = CircleColorAugmentor(ref_db, match_std=MATCH_STANDARD_DEVIATION) | |
| # 3. 获取源图像列表 | |
| valid_ext = ('.jpg', '.jpeg', '.png') | |
| src_files = [f for f in os.listdir(path_src_folder) if f.lower().endswith(valid_ext)] | |
| if not src_files: | |
| print("源文件夹为空") | |
| return | |
| print(f"\n开始生成 {NUM_SAMPLES} 张增强图像 (硬变换模式)...\n") | |
| for idx in range(NUM_SAMPLES): | |
| src_fname = random.choice(src_files) | |
| src_path = os.path.join(path_src_folder, src_fname) | |
| try: | |
| src_img, tgt_img, hard_mask, ref_used = augmentor.augment(src_path, b_debug=True) | |
| if ref_used == "No_Foreground": | |
| print(f"[{idx + 1}/{NUM_SAMPLES}] 跳过 (未检测到前景): {src_fname}") | |
| continue | |
| # 保存结果 | |
| out_name = f"aug_{idx:03d}_{os.path.splitext(src_fname)[0]}_ref_{os.path.splitext(ref_used)[0]}.jpg" | |
| save_path = os.path.join(path_out_folder, out_name) | |
| ref_used = path_ref_folder + ref_used | |
| visualize_results(src_img, tgt_img, hard_mask, ref_used, save_path) | |
| print(f"[{idx + 1}/{NUM_SAMPLES}] 完成: {out_name}") | |
| except Exception as e: | |
| print(f"[{idx + 1}/{NUM_SAMPLES}] 失败: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| print("\n所有任务完成!结果已保存。") | |
| def main(): | |
| cache_file = os.path.join(os.path.dirname(__file__), "test_color_sta.json") | |
| ref_db = ColorStatsDatabase(cache_file) | |
| augmentor = CircleColorAugmentor(ref_db, match_std=True) | |
| src_path = "/path/to/circle_id/images/ours_images/W103_17_circle_010.png" | |
| tgt_img = augmentor.augment(src_path, b_debug=False) | |
| cv2.imwrite("/path/to/circle_id/outputs/tgt.png", tgt_img) | |
| if __name__ == "__main__": | |
| # main() | |
| debug() | |