局部自适应阈值二值化
FlowCV AdaptiveThreshold for uneven light
- 图像输入
- 图像输出
Try a fixed threshold on a photo with a hard shadow across it and you'll see the problem immediately: the shadowed half goes solid black, the bright half goes solid white, and everything you actually wanted is gone. FlowCV's 局部自适应阈值二值化 node (class FCV_AdaptiveThreshold) is the fix for that. Instead of one global cutoff, it computes a threshold per pixel from the local neighborhood, so a document, label, or product photo with uneven lighting binarizes cleanly.
It's the second rung of the pack's binarization ladder: plain FCV_Threshold for flat, well-lit images, this node when lighting is uneven, and FCV_OTSU when you don't want to choose a threshold at all.
How it works
Under the hood it's OpenCV's cv2.adaptiveThreshold(). The image is converted to grayscale, then for every pixel a threshold is derived from the mean (or weighted Gaussian mean) of the surrounding block - hence the name "local adaptive." The output is the same binary black-and-white you get from a normal threshold, but the cutoff moves with the local brightness instead of sitting fixed at 127. That's the entire superpower, and it's why shadowed scans come out legible.
Two parameters control the mechanism:
- 邻域大小 (block size, default 11) - the odd-numbered window used to compute each local threshold. Bigger = threshold follows slower, smoother lighting changes. This must be odd; the node silently bumps even values up by one.
- 常数C (constant C, default 2) - a number subtracted from the local mean before comparing. Raise it to make fewer pixels pass (thinner foreground), lower it (or go negative, down to -50) to let more through. This is the knob you'll actually tune.
The 自适应方法 dropdown picks between 均值 (mean, ADAPTIVE_THRESH_MEAN_C) and 高斯 (Gaussian, ADAPTIVE_THRESH_GAUSSIAN_C) weighting - Gaussian is smoother on textured surfaces, mean is snappier. And 阈值类型 lets you flip to 反向二值化 if your foreground comes out inverted.
Wiring it up
Input is a CVIMAGE, output is a CVIMAGE - that's the pack's currency (raw OpenCV BGR numpy arrays). Route the output through FCV_CVToIMAGE to preview it or hand it to the rest of ComfyUI, or feed it straight into FCV_FindRectangles to hunt for contours in the binarized result. It pairs naturally with FCV_Median or FCV_Gaussian first if the source is noisy.
Installing
Same as every FlowCV node: it ships in the pack, so install once via ComfyUI Manager (search "FlowCV") or:
cd ComfyUI/custom_nodes
git clone https://github.com/Koren-cy/FlowCV
Restart, done. Dependencies are just opencv-python, numpy, and pyserial - no models. Worth knowing: the README states the project has migrated to ComfyUI_For_Academic, so this repo is in maintenance limbo; fine to use, unlikely to gain features.
The gotchas
Adaptive thresholding is noticeably slower than a fixed threshold on big images, because it's computing a neighborhood statistic per pixel - keep 邻域大小 modest on large inputs. And remember the pack's silent-failure habit: any exception prints a Chinese message to the console and returns your input unchanged, so if the output looks unprocessed, check the terminal.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| 图像输入 | CVIMAGE | 输入的openCV格式图像 | |
| 最大值 | INT | 2551–255 | 分配给满足条件的像素值的最大值 |
| 自适应方法 | COMBO | 均值 | 自适应阈值算法:均值或高斯 |
| 阈值类型 | COMBO | 二值化 | 阈值类型:二值化或反向二值化 |
| 邻域大小 | INT | 113–255 | 用于计算阈值的邻域大小,必须为奇数 |
| 常数C | FLOAT | 2.0-50–50 | 从均值或加权均值中减去的常数 |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| 图像输出 | CVIMAGE | 局部自适应阈值二值化处理后的图像 |