Nodes/ComfyUI CV/cv2.HoughLinesP
ComfyUI Node

cv2.HoughLinesP

The Hough transform you actually want for drawing and counting

By bmad4ever·Created 3 months ago·Updated 14 days ago· 1
cv2.HoughLinesP
  • image
  • nparray
◄rho0.0000►
◄theta0.0000►
◄threshold0►
◄minLineLength0.0000►
◄maxLineGap0.0000►

Probabilistic Hough - the same line finder as cv2.HoughLines, except it returns segments with endpoints instead of infinite polar lines. Each row is (x1, y1, x2, y2). That one design decision makes it the version almost everybody should be using: it drops straight into CV Draw Segments, into the curated CV Detect Lines (Hough) node, and into anything that wants geometry rather than angles.

It is also the one with controls that map to how you actually think: minimum length, and how big a gap between collinear runs is allowed before they stop being one line. Find long railings and ignore texture, or find any scrap of straight edge and accept that a brick wall is one giant line family.

How it works

Instead of filling a full (rho, theta) accumulator and reading peaks, the probabilistic variant samples edge points, and when a candidate line gathers enough votes it walks along it, extending as far as the edge points allow. That walk is what produces endpoints, and why minLineLength / maxLineGap are meaningful: they are applied as the segment is grown and trimmed.

The cost is a non-deterministic flavour: which points get sampled is random, so two runs on the same edge map can return slightly different segment sets. You will not usually notice, but do not build a test suite that asserts exact segment counts.

Inputs

  • image - 8-bit single-channel binary source. Canny first, or a threshold. The wrapper grayscales a colour IMAGE link for you, but a gray photo is still the wrong input; the tooltip says the image may be modified by the function, and the pack protects you by passing cv2 a private copy, so your upstream array is never damaged.
  • rho - accumulator distance resolution in pixels. Usually 1. The widget default is 0; set it.
  • theta - angle resolution in radians, usually np.pi/180 (1°).
  • threshold - minimum votes to accept a line. Start at 50 and move.
  • minLineLength - reject segments shorter than this. This is the parameter that removes the noise.
  • maxLineGap - maximum gap between points on the same line before the segment is broken. Bigger values merge broken edges into one long segment, which is often what you want on textured or antialiased edges.

Output is a single nparray of endpoint quadruples - and the row layout is a small moving target: OpenCV 4.x hands back (N, 1, 4), 5.x returns (N, 4). The pack's own workflows accept both, and CV Draw Segments documents that it takes "Nx1x4 or Nx4" and that cv2_HoughLinesP plugs in directly. If you are writing your own math downstream, reshape with CV Reshape Array rather than assuming.

Downstream, in order of usefulness: CV Draw Segments (endpoints plus count, so you can branch on "did we find anything"), counting via the count output, cv2.sortIdx on length if you want to keep the longest, CV Array To Text to dump the coordinates, and the same array into CV Filter Points By Distance if you only care about the endpoints as a point cloud. The pack also has parallel line detectors worth comparing on your own image - LSD (08_line_segments_lsd.json) and the Fast Line Detector - and both were deliberately given the same output layout so CV Draw Segments works with either.

If you want one node that does the whole job, CV Detect Lines (Hough) wraps Canny + this function + drawing, exposes canny_low, canny_high, threshold, min_length, max_gap, thickness, and returns both an overlay and a lines_only image. That is the sane starting point; use the raw nodes when you need the coordinates as data.

Install

Manager → search comfyui_cv (bmad4ever), or:

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"

Python ≥ 3.12 and a recent ComfyUI on the V3 node API. No model downloads anywhere in this pack.

When it goes wrong

  • Zero-length parameter defaults. rho, theta and threshold all start at 0 in the widget, and 0 is not a valid resolution or vote count. Set all three.
  • Nothing is found. Usually the input is not an edge map, or threshold is high relative to the edge density. Look at the Canny output before blaming the Hough settings - that is where the actual tuning lives.
  • A thousand tiny segments. Lower threshold plus texture. Raise minLineLength, raise maxLineGap, or pre-blur (the playground workflow uses cv2.medianBlur before Canny for exactly this).
  • One line where the architecture has a joint. maxLineGap too generous; it will happily bridge a corner into a straight run.
  • Frame 0 of a batch only. Not in the pack's per-frame loop list - split a batch first if you need per-frame segments.
  • Wheel hygiene. Never let a second pack install a non-contrib OpenCV wheel over this one; contrib modules empty out silently. tools/repair_opencv_contrib.py --check, then --apply.
Categoryimage/CV/low-level/cv2 H

Inputs (6)

NameTypeDefaultDescription
imageNPARRAY,IMAGE,MASK8-bit, single-channel binary source image. The image may be modified by the function. Accepts a ComfyUI IMAGE/MASK directly (frame 0 of a batch) or an NPARRAY. Arithmetic ops (add, multiply, etc.) process the full IMAGE batch when both inputs have the same batch size.
rhoFLOAT0.0000-1e+38–1e+38Distance resolution of the accumulator in pixels.
thetaFLOAT0.0000-1e+38–1e+38Angle resolution of the accumulator in radians.
thresholdINT0-2147483648–2147483647%Accumulator threshold parameter. Only those lines are returned that get enough votes ( $>\texttt{threshold}$ ).
minLineLengthoptFLOAT0.0000-1e+38–1e+38Minimum line length. Line segments shorter than that are rejected. Preset to the OpenCV default (0.0).
maxLineGapoptFLOAT0.0000-1e+38–1e+38Maximum allowed gap between points on the same line to link them. Preset to the OpenCV default (0.0).

Outputs (1)

NameTypeDescription
nparrayNPARRAY—