ComfyUI Node

Line Detector

Counting the lines in a diagram — and drawing each one in its own color so you can check its work

By mikemojen·Created 8 months ago·Updated 3 months ago· 0
Line Detector
  • image
  • line_count
  • line_lengths
  • colored_lines_image
threshold128
invertfalse
min_line_length10
line_thickness2

Line Detector is the headline node of this pack - the thing the README's seven-step algorithm is really about. Feed it an image of straight lines (a wiring diagram, a schematic, a plan view) and it counts the lines, measures each one's length in pixels, and - this is the part that makes it trustworthy - draws every detected line in a different color so you can see exactly what it thinks it found. That visualization is the difference between a number you blindly trust and a number you can actually verify.

How it works

The pipeline is textbook computer vision, executed in seven steps: convert to grayscale and binarize at threshold (default 128), thin everything to 1-pixel width via skeletonization, build a graph of junction points and endpoints, classify each junction by how many segments meet there, and then resolve crossings. The clever bit is the 4-way junction handling: when two lines cross, the algorithm pairs up collinear segments rather than just picking connections arbitrarily - so an X-shaped crossing is read as two straight lines, not four stubby ends. Then it traces each line through the graph and measures it, filtering out anything shorter than min_line_length (default 10 px).

invert (default off) handles the "white lines on dark background" case, and line_thickness (1–10, default 2) only affects the thickness of the colored visualization, not the measurement.

What comes out

  • line_count (INT) - how many lines it found.
  • line_lengths (STRING) - a comma-separated list of lengths in pixels.
  • colored_lines_image (IMAGE) - each line drawn in its own color from a 20-color palette.

The lengths are real measured pixel lengths along the skeleton, so they're useful raw numbers for scaling calculations - if your diagram has a known dimension, you can compute pixels-per-unit and turn the whole list into physical lengths. That's the workflow this pack is built for.

Where it fits

Load Image → Extract Black → LineDetector → Show Text, and the colored visualization gives you the confidence check. If the image has dashed lines, run DashedToSolidLine first - this detector assumes solid strokes. If it has curved lines, this isn't your node; it's explicitly for straight lines, and curves will come out fragmented or missed.

Install

Part of ComfyUI-HappNodeSet (mikemojen). ComfyUI Manager: search HappNodeSet. Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/mikemojen/ComfyUI-HappNodeSet.git
pip install -r ComfyUI-HappNodeSet/requirements.txt

Restart ComfyUI. Deps are the pack standard: numpy, opencv-python, scipy, scikit-image, svgwrite, Pillow, torch.

Common issues

Threshold is the usual offender: at 128, faint or thin lines vanish, so drop it for light pencil work. The 4-way junction resolution is the other thing to watch - at junctions where lines meet at nearly the same angle (an arrowhead, a Y), the collinearity pairing can merge what should be separate lines; that's when you eyeball the colored output and adjust min_line_length to shed spurious short segments. And remember the README's own advice: it wants high-contrast, relatively straight lines. Garbage in, garbage out - but at least here you can see the garbage, in twenty distinct colors, before you trust the count.

Categoryimage/analysis

Inputs (5)

NameTypeDefaultDescription
imageIMAGE
thresholdINT1280–255
invertBOOLEANfalse
min_line_lengthINT101–1000
line_thicknessINT21–10

Outputs (3)

NameTypeDescription
line_countINT
line_lengthsSTRING
colored_lines_imageIMAGE