形态学操作
FlowCV's morphology toolbox
- 图像输入
- 图像输出
Morphology is what you do to a binary image after thresholding, and it's the most underrated cleanup step in the whole pipeline. FCV_ (menu label 形态学操作, "morphological operations") is FlowCV's Swiss Army knife for it: seven operations in one node, plus control over the kernel shape and how hard it hits. If you've ever thresholded something and gotten a mask full of speckles and broken outlines, this node is the eraser and the glue.
The pack's category for it - openCV/形态学操作 - is a hint that the author treats it as the companion to the binarization nodes. The typical flow: threshold to black-and-white, then use this node to kill the noise and close the gaps before you feed the mask to FCV_FindRectangles or a diffusion workflow.
The operations
A dropdown named 操作类型 (operation type) picks the behavior, and the tooltips are honest descriptions worth trusting:
- 腐蚀 (erode) - shrinks bright regions, removes small specks, breaks apart barely-connected objects.
- 膨胀 (dilate) - grows bright regions, fills small holes, reconnects broken bits.
- 开运算 (opening) - erode then dilate. Removes small objects while keeping big ones roughly their original size. The default, and the one you'll use most for noise cleanup.
- 闭运算 (closing) - dilate then erode. Fills small holes inside objects. The other workhorse.
- 形态学梯度 (morphological gradient) - dilate minus erode, which extracts the outline of objects.
- 顶帽 (top hat) - original minus opening; pulls out small bright details.
- 黑帽 (black hat) - closing minus original; pulls out small dark details.
Under the hood each one is a single OpenCV call - cv2.erode, cv2.dilate, or cv2.morphologyEx with MORPH_OPEN / MORPH_CLOSE / MORPH_GRADIENT / MORPH_TOPHAT / MORPH_BLACKHAT.
The knobs that matter
- 核形状 (kernel shape) - 矩形 is the default and the strongest; 椭圆 gives rounder, gentler results on circular objects; 十字 (cross) affects horizontal/vertical structures, useful when you want to preserve thin lines along one axis.
- 核大小 (kernel size, default 5) - the size of the structuring element. Must be odd; the node silently adds one if you feed it an even number.
- 迭代次数 (iterations, default 1) - how many passes. This is the "strength" dial; crank it instead of making the kernel huge, since bigger kernels get expensive fast.
Wiring and output
Input CVIMAGE in, processed CVIMAGE out - the pack's standard BGR numpy format. Since morphology assumes a binary-ish input for best results, run a threshold (fixed, adaptive, or OTSU) first, then route this output to FCV_CVToIMAGE to view it or on to the next processing step.
Installing
Bundled in FlowCV, so one install covers the whole pack. ComfyUI Manager, search "FlowCV"; or:
cd ComfyUI/custom_nodes
git clone https://github.com/Koren-cy/FlowCV
Restart ComfyUI. Dependencies are light - opencv-python, numpy, pyserial, no models. Note: the README says the project has migrated to ComfyUI_For_Academic, so this repo is in archive mode - it works, it just isn't being developed.
Gotchas
Morphology works on the whole image, so a bright object adjacent to another bright object will bleed when you dilate aggressively - that's normal, that's the operation. And remember the pack-wide silent-failure habit: on any exception the node prints a Chinese error to the console and returns your input unchanged. If the output looks untouched, look at the terminal.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| 图像输入 | CVIMAGE | 输入的openCV格式图像 | |
| 操作类型 | COMBO | 开运算 | 选择要执行的形态学操作类型 腐蚀:移除图像中的小物体,平滑边界,断开连接的物体 膨胀:填充图像中的小孔,连接断开的物体,增强物体区域 开运算:先腐蚀再膨胀,移除小物体,保留大物体,平滑边界 闭运算:先膨胀再腐蚀,填充小孔,保留大物体,平滑边界 形态学梯度:膨胀减去腐蚀,提取物体轮廓,保留物体结构 顶帽:原始图像减去开运算,突出小物体,增强细节 黑帽:闭运算减去原始图像,突出暗细节,增强暗区域 |
| 核形状 | COMBO | 矩形 | 形态学操作的结构元素形状 椭圆核:平滑边缘,圆形对象,平滑效果 矩形核:保留物体边界,线性结构,强烈效果 十字核:突出物体内部细节,水平/垂直处理 |
| 核大小 | INT | 53–27 | 结构元素的大小,必须为奇数 |
| 迭代次数 | INT | 11–30 | 形态学操作的迭代次数 |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| 图像输出 | CVIMAGE | 形态学操作处理后的图像 |