OpenCV fitLine_0
Find the direction in a streak of points
- points
- line
- nparray
If you have a pile of 2D points that roughly form a line - a motion trail, a road edge, a laser streak, the horizon of a tilted frame - fitLine_0 will find the line that runs through them and hand you its direction. It's OpenCV's fitLine as a ComfyUI node, and it's an analysis tool: you feed it coordinates, it feeds you geometry.
Where does that fit in a workflow? Think measurement, not rendering. You might fit a line to a detected edge to measure its angle, then rotate the image to square it up. Or fit one to a motion streak to estimate velocity direction. It's a math node in the same family as the ellipse fitters in this pack - same auto-generated codebase, same no-frills interface.
How it works
fitLine uses a least-squares fit with a twist: it's an M-estimator, so you can pick a distance function that decides how hard outliers are punished. That's what distType controls. The output is a 4-value line description - a unit direction vector plus a point on the line - rather than a slope/intercept, which OpenCV uses because it handles vertical lines without dividing by zero.
Inputs that matter
Only a few, honestly:
- points (
NPARRAY) - your 2D points, typically N×2(x, y)coordinates. - distType (
INT) - the distance function.0= L2 (plain least squares, fast, outlier-sensitive),1= L1,2= L12,3= FAIR,4= WELSCH,5= HUBER. For most work,0is fine; switch to1or5when stray points are dragging the line. - param (
FLOAT) - the C parameter for the robust distance functions. Leave it0.0and OpenCV picks a sensible default per distance type. - reps (
FLOAT) and aeps (FLOAT) - radial and angular accuracy, both effectively0.01defaults. Leave them alone unless you have a reason.
The optional line (NPARRAY) is an OpenCV out-parameter the pack carries for compatibility; the README's advice is to ignore the dst-style out-params, and this is one of them. Skip it.
Output
nparray - a 1×4 float array: [vx, vy, x0, y0]. (vx, vy) is the unit direction vector, (x0, y0) is a point on the line. Angle in radians is atan2(vy, vx) if you need it.
Note this is not an image. If you wire it into Nparrays2Image, you'll hit the pack's classic 'NoneType' object has no attribute 'shape' error - that message is the README's own warning that not every nparray output is a picture.
Installing
Part of geroldmeisinger/opencv-comfyui, the auto-generated pack of ~635 OpenCV function nodes. ComfyUI Manager: search "opencv-comfyui". Or:
cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
Restart after cloning. requirements.txt pulls opencv-contrib-python, numpy, and torch; pip install opencv-contrib-python if your env is missing it.
Common issues
The usual pack rules: route images through Image2Nparray/Nparrays2Image, keep batch_size==1 (a batch of 2 throws Only images with batch_size==1 are supported!), and give points actual coordinates. The robust-fit knobs (distType, param) are the only real traps - if your line is being dragged by a couple of bad points, that's a distType problem, not a broken node.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| points | NPARRAY | — | |
| distType | INT | — | |
| param | FLOAT | — | |
| reps | FLOAT | — | |
| aeps | FLOAT | — | |
| lineopt | NPARRAY | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |