CommonZhuan Preprocessing Scripts 1.0
This commit is contained in:
1
PreProcessing_MatrixZhuan/.gitignore
vendored
1
PreProcessing_MatrixZhuan/.gitignore
vendored
@@ -7,6 +7,7 @@ processed_characters/
|
||||
svg_characters/
|
||||
out_cleaned/
|
||||
out_matrix/
|
||||
out_svg/
|
||||
|
||||
# Local caches
|
||||
.matplotlib-cache/
|
||||
|
||||
@@ -7,16 +7,17 @@
|
||||
依赖见 `scripts/requirements.txt`。如果环境里还没有安装,可以先运行:
|
||||
|
||||
```bash
|
||||
.venv/bin/pip install -r PreProcessing/scripts/requirements.txt
|
||||
.venv/bin/pip install -r PreProcessing_MatrixZhuan/scripts/requirements.txt
|
||||
```
|
||||
|
||||
从项目根目录运行:
|
||||
|
||||
```bash
|
||||
.venv/bin/python PreProcessing/scripts/run_preprocessing.py \
|
||||
--input-dir PreProcessing/original_characters \
|
||||
--cleaned-dir PreProcessing/out_cleaned \
|
||||
--matrix-dir PreProcessing/out_matrix
|
||||
.venv/bin/python PreProcessing_MatrixZhuan/scripts/run_preprocessing.py \
|
||||
--input-dir PreProcessing_MatrixZhuan/original_characters \
|
||||
--cleaned-dir PreProcessing_MatrixZhuan/out_cleaned \
|
||||
--matrix-dir PreProcessing_MatrixZhuan/out_matrix \
|
||||
--svg-dir PreProcessing_MatrixZhuan/out_svg
|
||||
```
|
||||
|
||||
参数含义:
|
||||
@@ -24,11 +25,13 @@
|
||||
- `--input-dir`:原始图像输入文件夹
|
||||
- `--cleaned-dir`:cleaned 二值图输出文件夹
|
||||
- `--matrix-dir`:17x17 矩阵文本输出文件夹
|
||||
- `--svg-dir`:矢量图 SVG 输出文件夹
|
||||
|
||||
输出命名会和输入文件名对齐:
|
||||
|
||||
- cleaned 图像:`原文件名_cleaned.png`
|
||||
- 矩阵文本:`原文件名_matrix.txt`
|
||||
- 矢量图:`原文件名.svg`
|
||||
|
||||
矩阵文本是纯 17 行,每行 17 个字符,只包含 `0` 和 `1`。
|
||||
|
||||
@@ -46,4 +49,4 @@
|
||||
|
||||
## 目录说明
|
||||
|
||||
`comparisons/`、`manual_diffs/`、`manual_matrices/`、`processed_characters/`、`svg_characters/` 等测试和比较用目录已经在 `PreProcessing/.gitignore` 中忽略。
|
||||
`comparisons/`、`manual_diffs/`、`manual_matrices/`、`processed_characters/`、`svg_characters/` 等测试和比较用目录已经在 `PreProcessing_MatrixZhuan/.gitignore` 中忽略。
|
||||
|
||||
@@ -13,6 +13,7 @@ from process_characters import (
|
||||
matrix_map,
|
||||
mask_to_display,
|
||||
read_image,
|
||||
small_matrix_to_svg,
|
||||
write_image,
|
||||
)
|
||||
|
||||
@@ -35,6 +36,7 @@ def process_image(
|
||||
image_path: Path,
|
||||
cleaned_dir: Path,
|
||||
matrix_dir: Path,
|
||||
svg_dir: Path,
|
||||
grid_size: int = 17,
|
||||
cell_samples: int = 32,
|
||||
open_kernel_size: int = 1,
|
||||
@@ -67,12 +69,24 @@ def process_image(
|
||||
matrix_to_text(mapping.matrix),
|
||||
encoding="utf-8",
|
||||
)
|
||||
height, width = gray.shape
|
||||
svg = small_matrix_to_svg(
|
||||
mapping.matrix,
|
||||
width,
|
||||
height,
|
||||
stem,
|
||||
mapping.x_edges,
|
||||
mapping.y_edges,
|
||||
mapping.triangles,
|
||||
)
|
||||
(svg_dir / f"{stem}.svg").write_text(svg, encoding="utf-8")
|
||||
|
||||
|
||||
def process_directory(
|
||||
input_dir: Path,
|
||||
cleaned_dir: Path,
|
||||
matrix_dir: Path,
|
||||
svg_dir: Path,
|
||||
grid_size: int = 17,
|
||||
cell_samples: int = 32,
|
||||
open_kernel_size: int = 1,
|
||||
@@ -83,6 +97,7 @@ def process_directory(
|
||||
) -> int:
|
||||
cleaned_dir.mkdir(parents=True, exist_ok=True)
|
||||
matrix_dir.mkdir(parents=True, exist_ok=True)
|
||||
svg_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
image_paths = iter_image_paths(input_dir)
|
||||
if not image_paths:
|
||||
@@ -93,6 +108,7 @@ def process_directory(
|
||||
image_path=image_path,
|
||||
cleaned_dir=cleaned_dir,
|
||||
matrix_dir=matrix_dir,
|
||||
svg_dir=svg_dir,
|
||||
grid_size=grid_size,
|
||||
cell_samples=cell_samples,
|
||||
open_kernel_size=open_kernel_size,
|
||||
@@ -113,6 +129,7 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
parser.add_argument("--input-dir", type=Path, required=True)
|
||||
parser.add_argument("--cleaned-dir", type=Path, required=True)
|
||||
parser.add_argument("--matrix-dir", type=Path, required=True)
|
||||
parser.add_argument("--svg-dir", type=Path, required=True)
|
||||
parser.add_argument("--grid-size", type=int, default=17)
|
||||
parser.add_argument("--cell-samples", type=int, default=32)
|
||||
parser.add_argument("--open-kernel-size", type=int, default=1)
|
||||
@@ -129,6 +146,7 @@ def main() -> None:
|
||||
input_dir=args.input_dir,
|
||||
cleaned_dir=args.cleaned_dir,
|
||||
matrix_dir=args.matrix_dir,
|
||||
svg_dir=args.svg_dir,
|
||||
grid_size=args.grid_size,
|
||||
cell_samples=args.cell_samples,
|
||||
open_kernel_size=args.open_kernel_size,
|
||||
|
||||
Reference in New Issue
Block a user