图像黑边检测裁剪处理器
更新时间:2026-04-23
简介
图像黑边检测裁剪处理器
功能描述
-
专业级黑边检测算法:
- 阈值检测(threshold) - 速度最快,适合纯黑边/纯色边
- 边缘检测(edge) - 基于Canny边缘,适合模糊/渐变黑边
- 直方图检测(histogram) - 基于像素分布,适合低对比度黑边
- 自动检测(auto) - 智能选择最优算法(推荐)
-
多格式输入支持:
- URL地址(image_url)
- BOS路径 ("bos://{bucket}/{path}")
- Base64编码(image_base64)
- 二进制流(image_binary)
-
多格式输出支持:
- JPEG/JPG:自动转换RGBA→RGB,保留DPI
- PNG:保留透明通道,无损保存
- WebP:支持透明通道,灵活保存
-
双输出模式:
- Base64编码直出
- 可选本地存储
-
鲁棒性保障:
- 最小黑边尺寸过滤(避免过度裁剪)
- 边界校验(防止裁剪超出图像范围)
- 检测失败自动降级为原图返回
算子参数
输入
| 输入 | 含义 |
|---|---|
| image | 包含输入图像的数组,支持URL/base64/二进制格式 |
| image_name | 可选参数,包含图像标识名的数组,用于生成输出文件名。 |
输出
| 输出 | 含义 |
|---|---|
| base64 | 压缩后图像的base64编码 |
| image_path | 本地存储路径(当配置输出目录时有效) |
参数
| 参数 | 类型 | 含义 | 默认值 |
|---|---|---|---|
| image_suffix | str | 裁剪后图像的保存格式,支持多格式适配。可选值: [".jpg", ".jpeg", ".png", ".webp"] | ".jpg" |
| output_dir | str | 将裁剪后的图像保存到的输出路径,为空则不保存到输出, | "" |
| image_src_type | str | 输入图像的格式类型,支持URL/base64/二进制。可选值: ["image_url", "image_base64", "image_binary"] | "image_url" |
| detect_algorithm | str | 黑边检测算法。支持4种检测算法,auto模式自动选择最优方案。可选值: ["threshold", "edge", "histogram", "auto"] | "auto" |
| black_threshold | int | 灰度阈值(仅threshold/auto算法生效):像素灰度值低于该值视为黑边(0-255),值越小检测越严格。 | 10 |
| edge_sensitivity | float | 边缘检测灵敏度(仅edge/auto算法生效):灵敏度越高,越容易检测到弱边缘(0.1-2.0)。 | 1.0 |
| min_border_size | int | 最小黑边尺寸(像素):小于该尺寸的黑边不裁剪,避免过度裁剪。 | 1 |
| target_dpi | list | 保存图像时的DPI参数,格式为 [width, height]。 | [72, 72] |
| quality | int | 保存质量参数(适配不同格式):JPEG/WebP:1-100(数值越高质量越好)PNG:映射为compress_level(1→9,100→0) | 85 |
调用示例
Plain Text
1from __future__ import annotations
2
3import os
4import daft
5from daft import col
6
7from daft.aihc.common.udf import aihc_udf
8from daft.aihc.functions.image.image_blackborder_crop import ImageBlackBorderCrop
9
10if __name__ == "__main__":
11 if os.getenv("DAFT_RUNNER", "native") == "ray":
12 import ray
13 ray.init(dashboard_host="0.0.0.0", ignore_reinit_error=True)
14 daft.set_runner_ray()
15 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
16
17 samples = {
18 "image": [
19 "file:///local/sample_1.jpg",
20 "file:///mnt/pfs/sample_2.jpg",
21 "file:///mnt/bos/sample_3.jpg",
22 ],
23 "image_name": ["sample_1.jpg", "sample_2.jpg", "sample_3.jpg"],
24 }
25
26 ds = daft.from_pydict(samples)
27 ds = ds.with_column(
28 "image_blackborder_crop",
29 aihc_udf(
30 ImageBlackBorderCrop,
31 construct_args={
32 "image_suffix": ".jpg",
33 "output_dir": "./test_images_output",
34 "image_src_type": "image_url",
35 "detect_algorithm": "auto",
36 "black_threshold": 10,
37 "edge_sensitivity": 1.0,
38 "min_border_size": 1,
39 "target_dpi": [72, 72],
40 },
41 batch_size=1,
42 )(col("image"), col("image_name")),
43 )
44
45 ds.show()
评价此篇文章
