寻找矩形轮廓
Geometry-checked contour detection
- 图像输入
- 图像输出
- 矩形数量
- 矩形坐标
Point this at a barcode, a document, a card, or a sheet of paper and it hands you back the rectangles - with coordinates. FlowCV's 寻找矩形轮廓 node (class FCV_FindRectangles) is the pack's geometry detective: it hunts contours, checks each one against a pile of rectangle criteria, and only lets through shapes that genuinely are rectangles. You reach for it when you want to locate objects in a frame - a document corner for perspective work, a barcode for a crop, a card on a table - without training any detection model.
The author leans on the tooltip for the key setup hint: feed it a binarized or edge-detected image. Garbage in, no rectangles out.
How it works
The pipeline is textbook OpenCV contour analysis, and the node is unusually strict about what counts:
- Grayscale the input, then
cv2.findContours()finds every closed contour. - Area filter - 最小面积 (default 100) and 最大面积 (default 50000) throw out specks and the whole-frame blobs.
- 近似精度 (approximation, default 0.03) drives
approxPolyDPto reduce each contour to a polygon; the contour survives this stage only if it approximates to exactly 4 points. - Then a gauntlet of geometric checks - the
_is_rectanglevalidator: convexity (凸性检测), opposite sides roughly equal, angles within 角度容差 of 90°, diagonals within 对角线容差 of equal, opposite edges within 边缘平行度 of parallel, aspect ratio under 最大长宽比, and 轮廓完整性 (how much of the raw contour the 4-point approximation actually covers).
That last set is what makes this node trustworthy: a squashed pentagon or a wobbly blob won't pass even if it has four corners. The tradeoff is tuning - with the defaults you may get zero hits on imperfect rectangles, which is the correct behavior when the criteria are strict.
Outputs
- 图像输出 - the input with detected rectangles drawn in green (plus blue corner dots) when 绘制结果 is set to 是 (the default).
- 矩形数量 - an INT count of matches.
- 矩形坐标 - a LIST of the four vertex coordinates for each rectangle, ready for other nodes to consume (cropping regions, etc.).
Wiring it up
CVIMAGE in, CVIMAGE + INT + LIST out. The canonical chain is FCV_Threshold (or FCV_OTSU) → FCV_ morphology to clean up → this node. Convert the drawn result with FCV_CVToIMAGE to view it.
Installing
Bundled in FlowCV. ComfyUI Manager, search "FlowCV"; or:
cd ComfyUI/custom_nodes
git clone https://github.com/Koren-cy/FlowCV
Restart ComfyUI. Dependencies: opencv-python, numpy, pyserial; no models. README note: migrated to ComfyUI_For_Academic, repo archived but working.
Gotchas
The strict checks are the whole point - and the whole frustration. If you feed it raw photos you'll get nothing; binarize first. The 近似精度 default (0.03) is a good starting point but noisy contours may need loosening to 0.05. And remember the pack's silent-failure habit: on an exception it prints a Chinese error to the console and returns (input, 0, []) - zero rectangles and the original image. Check the terminal before you blame your rectangles.
Inputs (11)
| Name | Type | Default | Description |
|---|---|---|---|
| 图像输入 | CVIMAGE | 输入的openCV格式图像,建议使用二值化或边缘检测后的图像 | |
| 最小面积 | INT | 100100–100000 | 矩形的最小面积阈值,过滤掉太小的矩形 |
| 最大面积 | INT | 500001000–500000 | 矩形的最大面积阈值,过滤掉太大的矩形 |
| 近似精度 | FLOAT | 0.0300.005–0.1 | 多边形近似的精度系数,值越小越精确 |
| 最大长宽比 | FLOAT | 3.01–10 | 允许的最大长宽比,用于过滤过于狭长的矩形 |
| 角度容差 | FLOAT | 155–45 | 矩形角度的容差范围(度),用于验证是否为规整矩形 |
| 凸性检测 | COMBO | 是 | 是否检测轮廓的凸性,矩形应该是凸多边形 |
| 对角线容差 | FLOAT | 0.050.01–0.2 | 对角线长度差异的容差比例,矩形的两条对角线应该相等 |
| 边缘平行度 | FLOAT | 105–30 | 对边平行度的角度容差(度),矩形的对边应该平行 |
| 轮廓完整性 | FLOAT | 0.900.7–1 | 轮廓完整性阈值,检测轮廓是否足够完整 |
| 绘制结果 | COMBO | 是 | 是否在输出图像上绘制检测到的矩形 |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| 图像输出 | CVIMAGE | 绘制了矩形的图像 |
| 矩形数量 | INT | 检测到的矩形数量 |
| 矩形坐标 | LIST | 矩形的四个顶点坐标列表 |