图像白底检测
更新时间:2026-07-29
简介
ImageWhiteBackgroundDetect 是白背景检测算子,基于 PIL / numpy 灰度直方图统计判断图像是否为白底图,输出布尔值。
功能描述
• 将图像转灰度后,分别统计边界带与全图中亮度 >= brightness_threshold 的白像素占比。
• 当边界带白像素占比与全图白像素占比同时超过 white_pixel_ratio 时判为白背景。
• 边界带宽度由 border_ratio 与图像最短边共同决定。
• 纯 CPU 实现,仅依赖 PIL 与 numpy,无需模型权重。
算子参数
输入
| 输入 | 含义 |
|---|---|
| images | 待检测的图像列,取值由 image_src_type 决定(图像 URL / base64 / 二进制) |
输出
| 输出 | 含义 |
|---|---|
| result | bool,True 表示图像为白背景 |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| image_src_type | str | "image_url" | 图像输入类型,可选 image_url / image_binary / image_base64 |
| brightness_threshold | int | 240 | 单像素灰度亮度阈值,>= 视为 "白" |
| border_ratio | float | 0.05 | 边界带占图像最短边的比例 |
| white_pixel_ratio | float | 0.75 | 白像素占比阈值;边界带与全图占比同时超过该值判为 True |
| fail_on_error | bool | False | 出错时是否抛异常;False 时对应行返回 False |
调用示例
Python
1from __future__ import annotations
2
3import os
4
5import daft
6from daft import col
7
8from daft.aihc.common.udf import aihc_udf
9from daft.aihc.functions.image.image_white_background_detect import ImageWhiteBackgroundDetect
10
11IMAGE = "bos://your-bucket/sample.jpg" # MOCK
12
13os.environ.setdefault("BOS_ENDPOINT", "http://bj.bcebos.com")
14os.environ.setdefault("BOS_REGION", "bj")
15
16if __name__ == "__main__":
17 if os.getenv("DAFT_RUNNER", "native") == "ray":
18 import ray
19
20 ray.init(ignore_reinit_error=True)
21 daft.set_runner_ray()
22 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
23
24 samples = {"image": [IMAGE]}
25 ds = daft.from_pydict(samples)
26 ds = ds.with_column(
27 "white_bg",
28 aihc_udf(
29 ImageWhiteBackgroundDetect,
30 construct_args={
31 "image_src_type": "image_url",
32 "brightness_threshold": 240,
33 "border_ratio": 0.05,
34 "white_pixel_ratio": 0.75,
35 },
36 num_cpus=1,
37 concurrency=1,
38 batch_size=1,
39 )(col("image")),
40 )
41 ds.show()
评价此篇文章
