| { |
| "cells": [ |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "import os\n", |
| "import numpy as np\n", |
| "from glob import glob\n", |
| "from tqdm import tqdm\n", |
| "from PIL import Image\n", |
| "\n", |
| "def remove_black_border(image):\n", |
| " \"\"\"去除X光片黑边\"\"\"\n", |
| " gray = np.array(image.convert(\"L\")) # 转为灰度\n", |
| " mask = gray > 10 # 过滤黑色背景,避免完全黑的像素影响裁剪\n", |
| " coords = np.argwhere(mask)\n", |
| " if coords.shape[0] == 0:\n", |
| " return image # 如果没有找到非黑区域,返回原图\n", |
| " y0, x0 = coords.min(axis=0)\n", |
| " y1, x1 = coords.max(axis=0) + 1\n", |
| " return image.crop((x0, y0, x1, y1))\n", |
| "\n", |
| "def process_images(input_dir, output_dir, target_size=(512, 512)):\n", |
| " \"\"\"批量处理X光片:去黑边 + 调整大小\"\"\"\n", |
| " os.makedirs(output_dir, exist_ok=True)\n", |
| " image_paths = glob(os.path.join(input_dir, \"**/*.png\"), recursive=True)\n", |
| "\n", |
| " for img_path in tqdm(image_paths, desc=\"Processing images\"):\n", |
| " try:\n", |
| " img = Image.open(img_path).convert(\"RGB\")\n", |
| " except Exception as e:\n", |
| " print(f\"Error loading {img_path}: {e}\")\n", |
| " continue\n", |
| " \n", |
| " cropped = remove_black_border(img)\n", |
| " resized = cropped.resize(target_size, Image.BILINEAR)\n", |
| "\n", |
| " # 生成输出路径\n", |
| " rel_path = os.path.relpath(img_path, input_dir)\n", |
| " save_path = os.path.join(output_dir, rel_path)\n", |
| " os.makedirs(os.path.dirname(save_path), exist_ok=True)\n", |
| " \n", |
| " resized.save(save_path)\n", |
| "\n", |
| "# 使用示例\n", |
| "input_dir = \"/data16T/chestx-ray/text2layout-single/\"\n", |
| "output_dir = \"/data16T/chestx-ray/text2layout-single-processed/\"\n", |
| "process_images(input_dir, output_dir)\n" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 1, |
| "metadata": {}, |
| "outputs": [ |
| { |
| "name": "stdout", |
| "output_type": "stream", |
| "text": [ |
| "Processed and saved: /data16T/chestx-ray/text2layout-ptx/0.0.07.109556.72.6.9.7.18989625890.5633519597565.8.png\n" |
| ] |
| } |
| ], |
| "source": [ |
| "from PIL import Image\n", |
| "import numpy as np\n", |
| "\n", |
| "def remove_black_border(image):\n", |
| " \"\"\"去除X光片黑边\"\"\"\n", |
| " gray = np.array(image.convert(\"L\")) # 转换为灰度图\n", |
| " mask = gray > 10 # 过滤接近黑色的像素,避免裁剪全黑区域\n", |
| " coords = np.argwhere(mask)\n", |
| " \n", |
| " if coords.shape[0] == 0:\n", |
| " return image # 如果没有有效区域,返回原图\n", |
| " \n", |
| " y0, x0 = coords.min(axis=0)\n", |
| " y1, x1 = coords.max(axis=0) + 1\n", |
| " return image.crop((x0, y0, x1, y1))\n", |
| "\n", |
| "def process_xray(image_path, output_path, target_size=(512, 512)):\n", |
| " \"\"\"对单张X光片去黑边 + 调整大小\"\"\"\n", |
| " img = Image.open(image_path).convert(\"RGB\")\n", |
| " cropped = remove_black_border(img)\n", |
| " resized = cropped.resize(target_size, Image.BILINEAR)\n", |
| " resized.save(output_path)\n", |
| " print(f\"Processed and saved: {output_path}\")\n", |
| "\n", |
| "# 示例:对 `image.png` 进行处理并保存\n", |
| "input_image = \"/data16T/chestx-ray/text2layout-ptx/train/0.0.07.109556.72.6.9.7.18989625890.5633519597565.8/0.0.07.109556.72.6.9.7.18989625890.5633519597565.8.png\"\n", |
| "output_image = \"/data16T/chestx-ray/text2layout-ptx/0.0.07.109556.72.6.9.7.18989625890.5633519597565.8.png\"\n", |
| "process_xray(input_image, output_image)\n" |
| ] |
| } |
| ], |
| "metadata": { |
| "kernelspec": { |
| "display_name": "biomedclip", |
| "language": "python", |
| "name": "python3" |
| }, |
| "language_info": { |
| "codemirror_mode": { |
| "name": "ipython", |
| "version": 3 |
| }, |
| "file_extension": ".py", |
| "mimetype": "text/x-python", |
| "name": "python", |
| "nbconvert_exporter": "python", |
| "pygments_lexer": "ipython3", |
| "version": "3.10.15" |
| } |
| }, |
| "nbformat": 4, |
| "nbformat_minor": 2 |
| } |
|
|