Nodes/opencv-comfyui/OpenCV HoughLines_0
ComfyUI Node

OpenCV HoughLines_0

Finding straight lines in a photo with a 60-year-old voting trick

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV HoughLines_0
  • image
  • lines
  • nparray
rho
theta
threshold
srn
stn
min_theta
max_theta
use_edgeval

HoughLines_0 finds straight lines in an image using the classic Hough transform - edge pixels vote for the lines that could pass through them, and the winners come back as [rho, theta] pairs. If you need to detect lanes, edges of a document, perspective lines, or the horizon, this is the node. It returns infinite lines (defined by distance-from-origin and angle), which is the thing that separates it from its more practical cousin HoughLinesP_0.

How it works

Every line in a plane can be written as rho = x·cos(theta) + y·sin(theta) - one distance, one angle. The Hough transform takes each edge point and adds a vote to every (rho, theta) combination it's compatible with. Points sitting on the same straight line all agree on one (rho, theta), so that cell in the accumulator wins. threshold is how many votes a cell needs to count as a line - raise it to demand more supporting pixels.

The pipeline around it

HoughLines doesn't detect edges itself. The textbook chain is: Image2NparraycvtColor (code 6, BGR2GRAY) → Canny_0 for edges → HoughLines_0. Feeding it a raw photo wastes votes on every gradient in the image; feeding it an edge map is where it sings. The image input must be 8-bit grayscale - hit it with a 3-channel array and you get the pack's familiar img.type() == CV_8UC1 assertion error.

The inputs that matter

  • image (NPARRAY) - grayscale, ideally a Canny edge map.
  • rho (FLOAT) - distance resolution in pixels. 1.0 is the standard choice.
  • theta (FLOAT) - angle resolution in radians. 0.0174 is π/180, i.e. one degree.
  • threshold (INT) - minimum accumulator votes per line. The main sensitivity knob.
  • srn, stn (FLOAT) - multi-scale Hough parameters; leave 0 unless you know why you need them.
  • min_theta, max_theta (FLOAT) - angle range to search, in radians. Restricting this to, say, [0, π/2] filters out the angles you don't care about.
  • use_edgeval (BOOLEAN) - newer OpenCV option that weights votes by edge strength. False default is fine to start.

Output is nparray: shape (N, 1, 2), each row [rho, theta]. The optional lines input is the generator's out-parameter; leave it disconnected.

What you get is not a drawing

The output is raw geometry, so to actually see the lines you must draw them yourself. There's no convenience node for it - the author lists "convert Hough lines to image" as an unfinished TODO. You'd take the [rho, theta] pairs, compute endpoints, and draw with line_0. It's doable but it's exactly the "expect dragons" the README warns about.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

or "OpenCV" via ComfyUI Manager, plus pip install opencv-contrib-python. No model files.

Gotchas

If you want finite segments (line segments with start/end points), you want HoughLinesP_0 instead - it has minLineLength and maxLineGap knobs and returns [x1, y1, x2, y2]. For detecting documents and lanes, P is usually the better tool. HoughLines_0's infinite lines are more useful for estimating vanishing points and perspective. And if lines are missing, it's almost always threshold - lower it.

Categoryimage/OpenCV

Inputs (10)

NameTypeDefaultDescription
imageNPARRAY
rhoFLOAT
thetaFLOAT
thresholdINT
srnFLOAT
stnFLOAT
min_thetaFLOAT
max_thetaFLOAT
use_edgevalBOOLEAN
linesoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY