ApplicationsExplainer

Line crossing object counting: how crossing detection works

How line-crossing object counting works: centroid tracking, signed distance and hysteresis, camera sizing arithmetic, line placement rules and miscount fixes.

LumaSpec Vision11 min read Share on LinkedIn

On this page 9 sections
  1. 01What a crossing counter counts
  2. 02Detect, track, then test the line
  3. 03The crossing test: signed distance and hysteresis
  4. 04Sizing the camera: pixels per object, pixels per frame, exposure
  5. 05Placing the line and aiming the camera
  6. 06Where counts go wrong, and the fix
  7. 07Hardware for a counting station
  8. 08Checks before you trust the number
  9. 09Questions

A line-crossing counter tallies each object once, at the moment its tracked centre crosses a line you draw in the image. Detect, track, test the line: that three-stage pipeline sits behind conveyor out-feed tallies, door and aisle traffic counts, vehicle counts and reject confirmation, and it runs on a vision sensor with no PC attached. This guide explains the part that decides whether the number is trustworthy: the signed-distance test, its hysteresis band and the one-shot latch behind it. It also works the sizing arithmetic — pixels per object, pixels per frame, exposure — and lists the failure modes behind double and missed counts.

What a crossing counter counts

A single-frame count answers a different question: how many objects are in the image right now. You segment one frame and count the blobs, as in counting rod-shaped objects in a single frame. That method breaks on flow: a part present across several frames is counted once when it should be many, or once per frame it appears in. Throughput questions need time, not a snapshot.

A crossing counter tracks every object and counts an event instead. Each object carries a track ID from first detection to exit; the counter increments when that track's centroid passes the line, exactly once per ID. What the object does afterwards — slow down, pause, shuffle — changes nothing, because the latch has already fired.

Counters are partitioned per line and per class, so one camera can watch a main line and a reject line and file boxes and rejects into separate tallies. Typical uses: out-feed counts on conveyors, parcels confirmed into a chute, people through a doorway, vehicles in a lane, parts past a diverter.

Detect, track, then test the line

Everything hangs off three stages, and the middle one is where counters usually go wrong.

  1. Detect. Every frame returns objects: boxes from a trained detector or blobs from thresholding, each with a class and a centroid. Getting stable centroids from contours is its own small craft — see OpenCV contour coordinates to x and y arrays.
  2. Track. Associate detections across frames so each object keeps one ID. Nearest-centroid matching holds while objects move less than about half their own pixel width per frame; dense scenes need an assignment step that weighs all pairs, not just the nearest neighbour. The output is a stream of ID, centroid and class per frame — segmentation and tracking from a machine-vision cell shows what that stream looks like in practice.
  3. Test. For each track and each line, compute the signed distance from centroid to line and run the state machine in the next section.

One gate sits before the test: a track must be seen in 3 consecutive frames before it may arm. Detector flicker throws one-frame phantoms; without the gate, glare and vibration write phantom counts all shift long.

The crossing test: signed distance and hysteresis

Draw the line, fix the travel direction, and the geometry reduces to one number per track per frame: the signed distance from centroid to line, in pixels.

s = x_centroid - x_line (pixels; use y for a horizontal line) armed when s > +h fired when an armed track reaches s < -h -> increment once, latch

Each track walks four states per line: new, confirmed after 3 detections, armed once its centroid sits more than h pixels on the origin side, fired when it reaches more than h pixels on the far side. The increment lives in the fired transition only.

The band h exists because a centroid jitters 1-2 px between frames from segmentation noise. A bare sign test at s = 0 toggles with that noise, and an object pausing on the line is counted by every toggle. A band of 3 px on each side covers the jitter for a cleanly lit 20 px object; move to 5 px when silhouettes flicker — shiny parts, thin edges, distant targets. The band only has to beat the jitter, so it tracks your segmentation quality, not the object size.

The count is an edge event, not a level. It is the same discipline as a rising-edge counter written in structured text on a PLC: increment on the transition, never while the condition holds.

Test the centroid, not the bounding-box edge. The box re-fits every frame and can wash back and forth across the line; the centroid of a passing object advances monotonically. When objects can legitimately recross — a person stepping back through a doorway — re-arm only after the centroid returns past the far band, and keep an IN and an OUT counter rather than one net number. And because a centroid that lands inside one pixel sits on either side of your line in arithmetic terms, the half-pixel convention in blob centroid coordinates decides that edge case: pick one convention and keep it.

Sizing the camera: pixels per object, pixels per frame, exposure

Three pixel requirements size the station, and all three hang off one scale factor: p, the millimetres per pixel at the line.

p = w_min / n_px mm per pixel, from smallest object w_min and target pixels n_px f = v / (fps × p) pixels of travel per frame at speed v t_exp ≤ p / v exposure that keeps motion blur under one pixel

Size for 15-20 px across the smallest object: below that, detectors stop holding stable IDs and jitter approaches the object's own width. Keep travel under about a third of the object width per frame, so the tracker sees it at least 3 times before the line. Cap exposure at p / v so blur stays inside one pixel and the centroid stays honest.

That 480 mm field comes from focal length and working distance in the usual way:

How sensor width, working distance and field of view set the focal length Rays from the edges of the sensor pass through the lens and spread to the edges of the field of view. The ratio of sensor width to field of view is the magnification m.CameraField of view (FOV)Working distance (WD)≈ fsensor width slensm = s / FOVf = WD × m / (1 + m)
How sensor width, working distance and field of view set the focal length. Rays from the edges of the sensor pass through the lens and spread to the edges of the field of view. The ratio of sensor width to field of view is the magnification m.

Check your own pixel budget before shortlisting hardware:

Camera resolution for the smallest feature
Units
Pixels needed
–
Sensor size
– MP
Smallest sensor that fits
–
Pixel on the part
– mm
Pixels across the feature, that sensor
–

Pixels needed = field / feature × pixels per feature on each axis (field incl. margin). The lens must resolve the pixels too: Nyquist = 1000 / (2 × pixel µm) lp/mm.

Find cameras with enough pixels Open the full calculator

Placing the line and aiming the camera

Put the line in the middle third of the field of view. The track needs history before it can arm: confirmation costs 3 frames × 6.7 px ≈ 20 px ≈ 50 mm of travel at the belt example. A centred line leaves about 96 px — roughly 14 frames at 30 fps — of confirmed track before the crossing, so even a late first detection counts.

Aim square to the travel direction and shoot from above where you can. Overhead views stop objects occluding each other at the line and keep scale uniform across it. A tilted camera makes mm per pixel vary with depth: your p, and with it the frame-rate and exposure numbers, is then exact at one distance only.

Calibrate p against something known in the scene — a ruler laid across the belt, a part of known width. The inverse problem, reading a distance from a known pixel width, is worked in camera distance from a tape line's pixel width; same geometry, solved backwards.

Keep lines out of the outer few percent of the frame, where lens distortion moves centroids most.

Where counts go wrong, and the fix

Nearly every miscount is one of six failures:

Symptom Usual cause Fix
Same object counted twice Track loses its ID at the line and the replacement ID fires too One-shot latch per ID, minimum track age, wider hysteresis
Doubles on a multi-camera belt Adjacent fields of view overlap, so two cameras see one part Deduplicate at the hand-off — see duplicate detection with overlapping conveyor images
Objects missed Touching parts merge into one blob that crosses once Overhead spacing, size-based split, watershed splitting of touching objects
Fires early or late Line inside the entry margin, or jitter toggling a bare sign test Middle-third placement plus the hysteresis band
Tallies drift through the shift Lighting changes what the detector sees Fixed exposure and gain; shield or compensate for ambient light variation
Phantom counts One-frame detections from glare or vibration 3-frame confirmation before a track may arm

Shutter type is the last hardware-level cause. A rolling shutter reads rows in sequence, so a fast object leans within its frame; the skew stays inside the band for slow traffic but, at tens of pixels per frame, distorts the detection itself. Fast lines get a global shutter; slow lines run either type.

Hardware for a counting station

A single line and one or two classes need no PC. The MV-SCB007EC-12S-WBN combines a 1/2.9" global-shutter sensor, a fixed 12 mm lens and Fast Ethernet: the hardware list is one cable.

Smart cameras & vision sensorsHikrobot MV-SCB007EC-12S-WBN SCB vision sensor, 1.6 MP, Color, 12 mm lens, Manual focusMV-SCB007EC-12S-WBN
Interface
Fast Ethernet
Resolution
Up to 3 MP
Sensor format
1/2.9"
Shutter
Global
$1,989.00 · quotedView and quote

When classes separate by colour rather than shape, or you want several lines and the lighting built in, use a smart camera with a global shutter. The MV-SC6050C-08M-XBN pairs a 5 MP colour global-shutter sensor with an integrated multi-colour light source on GigE.

Smart cameras & vision sensorsHikrobot MV-SC6050C-08M-XBN SC6000 Series Smart Camera, 5 MP, Color, 8 mm focal length, M12-mount, Multi-color light sourceMV-SC6050C-08M-XBN
Interface
GigE
Resolution
5 MP
Sensor format
1/1.45"
Shutter
Global
$3,449.00 · quotedView and quote

Multi-camera tallies, footage retention and heavier models belong on a PC. The area scan cameras category covers the sensor side — the MV-CU020-90GC brings a 2.5 MP colour global-shutter sensor in 1/1.7" format to a C-mount on GigE — while the vision controllers category covers the compute side. The MV-VC2140-128G40-1T contributes an Intel N-series processor, 8 GB memory, a 128 GB SSD plus 1 TB HDD, three to four LAN ports and four built-in light channels.

Area scan camerasHikrobot MV-CU020-90GC 2.5 MP Area Scan Camera, GigE, GMAX4002, ColorMV-CU020-90GC
Sensor format
1/1.7"
Lens mount
C-mount
Interface
GigE
Resolution
Up to 3 MP
$309.00View partBuild a system with it
Vision controllersHikrobot MV-VC2140-128G40-1T VC2100 Vision Controller, N97, 8GB+128G SSD+ 1THDDMV-VC2140-128G40-1T
LAN ports
3-4 LAN ports
USB 3 ports
2 USB 3 ports
Light outputs
4 built-in channels
Memory
8 GB
$1,679.00View partBuild a system with it

For the algorithm-in-camera route, browse smart cameras; to run counting alongside locating and code reading in one controller, start at the camera step of the system configurator.

Checks before you trust the number

  • Record the worst five minutes — fastest belt, densest spacing, worst light — and replay them through the counter against a manual tally. They must agree exactly; every disagreement is a bug with an address.
  • Repeat the replay per product and per light level, and score it per condition: per-condition generalisation checks for detection models show which condition breaks before production does.
  • Watch the last-crossing timestamps, not just the total: strictly increasing, one timestamp per increment, and an interval distribution that matches your part flow.
  • Reset counters at batch and shift boundaries — the per-batch counter reset pattern keeps every total attributable to a window — and log each reset.
  • After any change of lens, zoom or camera height, re-derive p, exposure and pixels per frame; the line position is the only part of the set-up you may carry over unchanged.

From here it is four lines of arithmetic: belt speed and smallest part give p, f and t_exp, and the camera finder turns them into a shortlist. The applications topic hub collects the neighbouring set-ups — conveyors, tracking, sizing — when counting is one station among several.

Questions

How many pixels across does an object need for reliable crossing counts?

Plan on 15-20 pixels across the smallest object and treat 10 pixels as the floor. Below roughly 10 pixels, frame-to-frame centroid jitter from segmentation noise approaches the object's own width, IDs flicker and the crossing test fires on noise. The worked belt example sizes the field of view for 20 pixels on a 50 mm box, which sets 2.5 mm per pixel and leaves exposure and frame rate easy to meet.

What hysteresis band should I set on the line?

Start at 3 pixels on each side and widen to 5 pixels when silhouettes flicker. The band's only job is to exceed the centroid's frame-to-frame jitter, which runs 1-2 pixels for a cleanly lit 20-pixel object. Make it asymmetric if noise differs by side: arm 3 pixels before the line, fire 5 after it. Re-check the band after any lens or height change, because jitter and pixels per frame scale with the new geometry.

Can one camera count several lines and classes at once?

Yes. Each line-and-class pair keeps an independent counter, and one track can fire different counters as it passes different lines. Give every counter its own enable flag and its own reset, so clearing a shift total on one line leaves the others intact. Vote the class over the track's recent frames before firing: a detector that flips a part between classes at the line would otherwise split one object across two tallies.

What frame rate does my belt speed need?

Convert everything to pixels first: keep travel under about a third of the object's pixel width per frame so the tracker sees it at least three times before the line. At 0.5 m/s and 2.5 mm per pixel, a 20-pixel object allows about 6.7 pixels per frame, which 30 fps meets exactly and 60 fps halves. Faster only pays when objects crowd each other and the tracker needs more frames to hold IDs.

Why does my counter register the same object twice?

Almost always tracking, not detection. The tracker drops the ID at the line — blur, overlap or a lighting flicker — issues a new ID, and both tracks fire the latch. Fix it with a one-shot latch per ID, a minimum track age before firing, the hysteresis band, and an overhead angle that stops objects occluding at the line. On multi-camera belts, also check the fields of view for overlap, which counts one part at the hand-off.

Does a rolling-shutter camera work for crossing counts?

For slow lines, yes. A rolling shutter reads rows in sequence, so a moving object leans slightly in the frame; the lean moves centroids by a pixel or two and the hysteresis band absorbs it. Compute before deciding: skew grows with speed, and once travel reaches tens of pixels per frame the distortion degrades detection itself. Fast lines deserve a global-shutter sensor so the crossing geometry stays exact; slow belts and walkways run fine on either type.