Nodes/ComfyUI CV/cv2.HoughLines
ComfyUI Node

cv2.HoughLines

Cv2.HoughLines (and why you probably want HoughLinesP)

By bmad4ever·Created 3 months ago·Updated 14 days ago· 1
cv2.HoughLines
  • image
  • nparray
◄rho0.0000►
◄theta0.0000►
◄threshold0►
◄srn0.0000►
◄stn0.0000►
◄min_theta0.0000►
◄max_theta3.1416►
◄use_edgevalfalse►

cv2.HoughLines finds straight lines in an edge image. It does not give you endpoints, and that is the whole story of this node: each line comes back as a distance and an angle - (rho, theta) in polar form - describing an infinite line through the image. Radians, not degrees, and the parameter space is the image origin in the top-left corner.

So: reach for it when the orientation is the answer. Deskewing a scanned page, measuring the horizon, finding the dominant angle of a facade, checking whether a generated room's wall is off by two degrees. Reach for cv2.HoughLinesP when you want to draw or count segments, because it returns endpoints and its output plugs straight into the pack's drawing nodes. The edge maps this thing eats are commonly Canny or lineart passes - the same kind of structure ControlNet's lineart and canny preprocessors key on (controlnet.md).

How it works

Every white (edge) pixel votes. For each candidate angle the pixel contributes to a sinusoid in the (rho, theta) accumulator; where many sinusoids cross, a line exists. rho is the accumulator's distance resolution in pixels (usually 1) and theta its angular resolution in radians (usually np.pi/180, one degree). threshold is the vote count a line needs.

The classic Hough transform then returns, per accumulator peak, one infinitely long line. It is not segment-aware: three collinear segments on the same edge produce one line, and a slightly bent edge can produce several.

Inputs

  • image - 8-bit single-channel binary source. The tooltip is explicit that this is an edge map, not a photo: run cv2.Canny (or a threshold) first. A colour IMAGE link is auto-converted to gray, which is convenient but not sufficient - gray photos have no line structure to vote on.
  • rho - 1 is the normal value. The widget starts at 0, which is not usable; set it.
  • theta - 0.01745 (1 degree). Smaller gives finer angles and slower accumulation.
  • threshold - starts at 0, meaning every line down to one vote; set something in the 50–200 range for a 1024px frame.
  • srn, stn - the multi-scale Hough divisors (0 = plain single-scale). Only meaningful with HOUGH_MULTI_SCALE, which you select on the method dropdown of the sibling nodes; here they are just knobs that do nothing at their defaults.
  • min_theta / max_theta - restrict the angle search, a genuinely useful trick: set min_theta near π/2 to find only near-horizontal lines and skip half the accumulator.
  • use_edgeval - weighted Hough, off by default. Turn it on when weak edges are dragging lines off true.

Output is one nparray: for each line, rho and theta in radians, ordered by vote strength.

Reading the output, and drawing it

CV Draw Segments will not eat this. It wants (x1, y1, x2, y2) endpoint quadruples - what HoughLinesP produces - and there is no curated node in the pack that converts polar lines to segments for you. CV Draw Rays draws from a shared origin, which is a different construction. To look at polar lines you either convert them yourself (x0 = rho·cos(theta), y0 = rho·sin(theta), then step ±L along the perpendicular), or you stop fighting it and use HoughLinesP plus CV Draw Segments, or the curated one-node CV Detect Lines (Hough), which runs Canny then probabilistic Hough and returns an overlay plus a lines-only image.

Where the polar form is actually pleasant: CV Array To Text to print the angles, Preview CV Array to histogram them, and the pack's angle/rotation nodes to build a deskew transform from a measured theta. CV Roll (FFT Shift) and the rotation/transform nodes are the natural next step. The pack's 07_hough_playground.json is the workflow to open first - it wires Hough variants, Canny and the drawing nodes together so you can watch parameters move the result.

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, a recent ComfyUI on the V3 node API, one pinned dependency, no models.

When it goes wrong

  • rho or theta left at 0. The widget's zero default is passed straight through to OpenCV, and a zero resolution is not "use the default" - type the real values.
  • Hundreds of near-identical lines. Standard Hough behaviour on thick edges: both sides of every edge vote. Raise threshold, or use HoughLinesP with minLineLength, which is far easier to tame.
  • Angles that look rotated. Theta is measured from the vertical axis in the top-left origin, so 0 is a vertical line in image terms - the tooltip says so and it still surprises people.
  • Frame 0 only. This function is not in the pack's per-frame loop list; a batch gives you the first frame. Split first if you need per-frame lines.
  • Empty output is normal. A soft or noisy edge map can produce nothing at your threshold. Branch on it rather than assuming lines exist.
Categoryimage/CV/low-level/cv2 H

Inputs (9)

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}$ ).
srnoptFLOAT0.0000-1e+38–1e+38For the multi-scale Hough transform, it is a divisor for the distance resolution rho. The coarse accumulator distance resolution is rho and the accurate accumulator resolution is rho/srn. If both srn=0 and stn=0, the classical Hough transform is used. Otherwise, both these parameters should be positive. Preset to the OpenCV default (0.0).
stnoptFLOAT0.0000-1e+38–1e+38For the multi-scale Hough transform, it is a divisor for the distance resolution theta. Preset to the OpenCV default (0.0).
min_thetaoptFLOAT0.0000-1e+38–1e+38For standard and multi-scale Hough transform, minimum angle to check for lines. Must fall between 0 and max_theta. Preset to the OpenCV default (0.0).
max_thetaoptFLOAT3.1416-1e+38–1e+38For standard and multi-scale Hough transform, an upper bound for the angle. Must fall between min_theta and CV_PI. The actual maximum angle in the accumulator may be slightly less than max_theta, depending on the parameters min_theta and theta. Preset to the OpenCV default (3.141592653589793).
use_edgevaloptBOOLEANfalseTrue if you want to use weighted Hough transform. Preset to the OpenCV default (False).

Outputs (1)

NameTypeDescription
nparrayNPARRAY—