Nodes/FlowCV/寻找矩形轮廓
ComfyUI Node

寻找矩形轮廓

Geometry-checked contour detection

By Bit-Walker·Created about a year ago·Updated 7 months ago· 3
寻找矩形轮廓
  • 图像输入
  • 图像输出
  • 矩形数量
  • 矩形坐标
最小面积100
最大面积50000
近似精度0.030
最大长宽比3.0
角度容差15
凸性检测
对角线容差0.05
边缘平行度10
轮廓完整性0.90
绘制结果

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:

  1. Grayscale the input, then cv2.findContours() finds every closed contour.
  2. Area filter - 最小面积 (default 100) and 最大面积 (default 50000) throw out specks and the whole-frame blobs.
  3. 近似精度 (approximation, default 0.03) drives approxPolyDP to reduce each contour to a polygon; the contour survives this stage only if it approximates to exactly 4 points.
  4. Then a gauntlet of geometric checks - the _is_rectangle validator: 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.

CategoryopenCV/寻找轮廓

Inputs (11)

NameTypeDefaultDescription
图像输入CVIMAGE输入的openCV格式图像,建议使用二值化或边缘检测后的图像
最小面积INT100100–100000矩形的最小面积阈值,过滤掉太小的矩形
最大面积INT500001000–500000矩形的最大面积阈值,过滤掉太大的矩形
近似精度FLOAT0.0300.005–0.1多边形近似的精度系数,值越小越精确
最大长宽比FLOAT3.01–10允许的最大长宽比,用于过滤过于狭长的矩形
角度容差FLOAT155–45矩形角度的容差范围(度),用于验证是否为规整矩形
凸性检测COMBO是否检测轮廓的凸性,矩形应该是凸多边形
对角线容差FLOAT0.050.01–0.2对角线长度差异的容差比例,矩形的两条对角线应该相等
边缘平行度FLOAT105–30对边平行度的角度容差(度),矩形的对边应该平行
轮廓完整性FLOAT0.900.7–1轮廓完整性阈值,检测轮廓是否足够完整
绘制结果COMBO是否在输出图像上绘制检测到的矩形

Outputs (3)

NameTypeDescription
图像输出CVIMAGE绘制了矩形的图像
矩形数量INT检测到的矩形数量
矩形坐标LIST矩形的四个顶点坐标列表