ApplicationsHow-to

Count rod-shaped objects in images: segment and number them

In line or lab images, segment rod-shaped objects by color, split touching rods with a watershed, and count components that pass area and 5:1 aspect filters.

LumaSpec Vision10 min read Share on LinkedIn

On this page 10 sections
  1. 01How do you count rod-shaped objects in an image?
  2. 02How many pixels do you need across one rod?
  3. 03How do you segment rods from the background?
  4. 04How do you set the minimum area filter without losing rods?
  5. 05How do you separate rods that touch?
  6. 06How do you filter by shape so only rods count?
  7. 07How do you keep the count stable while parts move?
  8. 08Which camera should you order for a counting job?
  9. 09How do you verify the count before it goes live?
  10. 10Questions

Counting elongated parts — dowel pins on a feeder, glass rods on a belt, stained bacilli on a smear — breaks in two places: dust outruns a naive filter, and two touching rods read as one. The fix is a pipeline whose thresholds come from the parts themselves. How many pixels one rod spans decides the minimum area, the aspect cut and even the exposure budget. This guide builds that pipeline step by step, works every threshold out in pixels for a 1.5 × 10 mm pin imaged at 12.5 µm/px, and closes with the camera, controller and verification decisions for three common set-ups.

How do you count rod-shaped objects in an image?

Threshold the image on the color channel that separates rods from background, clean the mask with a small morphological open, split touching rods with a distance-transform watershed, then count the connected components whose area and 5:1 length-to-width ratio pass your two filters. Every step feeds the next, and the two filters at the end carry all the tuning:

  1. Acquire with a fixed exposure and fixed illumination — no auto exposure, no auto gain.
  2. Take the color channel with the widest histogram gap and threshold it.
  3. Open the binary mask with a small structuring element (3-5 px radius here) and fill holes, so specks and internal shadows never reach the labelling step.
  4. Split rods that touch with the distance-transform watershed in section five.
  5. Label the connected components and record two values for each: pixel area, and the length-to-width ratio of the rotated bounding rectangle.
  6. Keep every component whose area and ratio pass the two filters — the survivors are the count.

Steps 1-3 prepare the mask, step 4 fixes the one failure a filter cannot fix, and step 5 is the same connected-components engine that drives counting lines per region with blob analysis and electrode-pair verification. What remains is to set four numbers: scale, minimum area, aspect cut and exposure.

How many pixels do you need across one rod?

Plan for at least 5 pixels across the smallest rod's width. In the worked example below, a 1.5 mm pin at 12.5 µm/px spans 120 pixels across — 24 times that floor — so the width measurement error stays below half a percent. Work the scale out before anything else. Use the LS-CMR-0166 for full-resolution counting: it produces a 4000 × 3000 frame. A 50 mm field of view over that frame gives 50 / 4000 = 0.0125 mm/px, or 12.5 µm/px. The smallest pin, 1.5 mm wide, then spans 1.5 / 0.0125 = 120 px across and 10 / 0.0125 = 800 px along.

scale = FOV / pixels across = 50 mm / 4000 px = 12.5 µm/px width in px = width / scale = 1.5 mm / 0.0125 mm/px = 120 px

Five pixels across the width is the floor: below it, a half-pixel edge error is already 10% of the width. At 120 px the same error is 0.4%, small enough that the 5:1 aspect test stays trustworthy. Counting alone survives the 5 px floor; shape filtering wants tens of pixels across, as here. Measure the true scale against a ruler in one frame — the same pixels-to-mm step the coin sorting guide walks through — because mounting tilt and lens distortion move the nominal value by a few percent.

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

How do you segment rods from the background?

Take the color channel with the widest histogram gap between rods and background, then threshold there: for pink-stained rods on a pale background the green channel usually separates them best, because the stain absorbs green light most strongly. Pick the channel from data rather than habit. Grab twenty frames, plot each channel's histogram, and keep the one with the widest gap between the rod population and the background. Otsu's method then fixes the threshold on that channel automatically — the Lab-color Otsu guide shows the mechanics and where it fails. Two hardware moves beat any channel trick: a backlight turns shiny metal rods into black silhouettes that threshold cleanly in mono, and polarization kills the specular glare that makes wet or glossy rods flare. Keep color when the stain carries the contrast; go mono when the silhouette does.

How do you set the minimum area filter without losing rods?

Set the minimum area to a quarter of the smallest rod's pixel area: the worked example's smallest pin covers about 75,000 pixels, so the filter goes at 19,000 pixels, letting a rod pass even if segmentation keeps only half of it.

rod area = 0.785 × 120 px × 800 px ≈ 75,000 px minimum area = 0.25 × 75,000 ≈ 19,000 px

The quarter rule buys margin for imperfect thresholds. A rod that keeps only half its pixels still lands near 37,500, roughly twice the cut, while dust, bubbles and sensor spatter sit far below — the 3-5 px open from step three already clears anything under a few hundred pixels. Before freezing the number, log the area histogram over a full shift: it should show one island for rods and a low tail for debris, and the cut belongs in the empty gap between them.

How do you separate rods that touch?

Run a distance-transform watershed: each rod's mask has a bright ridge along its long axis, and the watershed line cuts exactly where two ridges meet, which separates rods that lie side by side in one blob. In practice: threshold, compute the distance transform of the mask, keep its local maxima — one per rod axis — and flood the watershed from those markers. Two pins touching over part of their length show two ridges with a saddle between; the cut drops onto the saddle and the blob splits into two components that pass both filters on their own.

A head-to-tail pair is harder: the outline has no waist, one ridge runs the whole way, and the watershed sees a single long rod. Catch those by length. This recipe's pins are 800 px long, so any component longer than 1,200 px (1.5 times the maximum length) is a clump, and a 1,560 px component credits round(1560 / 800) = 2 pins. The full marker code and border cases are in Separating touching objects with watershed; where the fixture allows it, spacing rods by vibration beats every split, because each clump is an off-by-one waiting to happen.

How do you filter by shape so only rods count?

Keep only components whose length-to-width ratio is at least 5:1, measured from the rotated bounding rectangle. Round parts sit near 1:1, so a 5:1 cut removes them. At 120 pixels wide, a half-pixel edge error moves the ratio by well under one percent. Take the ratio from the rotated rectangle — long side over short side — never the axis-aligned box, or a rod at 45° reads like a square. These pins sit at 800 / 120 ≈ 6.7:1; bubbles, spatter and other round junk sit at 2:1 or lower, so the 5:1 cut separates the populations with margin on both sides.

Two things keep the filter honest. A head-to-tail clump passes aspect at 12:1, which is why section five splits by length instead. And a rod seen end-on collapses toward 1:1 — fixture that orientation away, or accept that those few fall to the area filter and under-count slightly.

How do you keep the count stable while parts move?

Cap the exposure at the blur budget: parts at 50 mm/s moving under a 12.5 µm/px image may only travel 12.5 µm during the exposure, which sets a 0.25 ms limit, and the light must hold its brightness for every one of those pulses.

max exposure = 1 px of travel / speed = 0.0125 mm / 50 mm/s = 0.00025 s = 0.25 ms

Set the exposure to 0.25 ms or shorter and the strobe pulse to the same width; once you strobe, the pulse rather than the exposure line sets the blur, as the strobe pulse width guide derives. Then switch off every auto mode: auto exposure and auto gain re-balance frame by frame and drag the threshold along — the brightness drift guide lists the modes and the order to turn them off. Power the light from a light controller rather than a raw supply — the finder filters by channels and strobe support — so the pulse height stays constant as the diodes age.

The LS-CTL-0070 is a 4-channel, 200 W digital unit with strobe output and RS232. Strobe one channel for the 0.25 ms pulse and run a second channel as a continuous backlight.

Light controllersDigital control box, 4 ch, 200 W, RS232 (LS-CTL-0070)LS-CTL-0070
Output
Constant voltage 24 V
Channels
4 channels
Control
Digital
Feature
Strobe
$549.00View partBuild a system with it

On a moving feeder, prefer a global shutter: a rolling sensor reads rows in sequence and shears fast parts. The MV-CU020-90GC pairs a 2.5 MP sensor with a global shutter on GigE. That is ample for this job, where a rod spans 120 px.

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

A 200 W strobe firing quarter-millisecond pulses is hard on eyes — never look straight into one while triggering.

Which camera should you order for a counting job?

Decide from the image size and the touch level: large 12 MB frames at high rates need USB3, because GigE tops out near 115 MB/s. Order a USB3 area scan for full-resolution counting, and a smart camera when the rods are clean and separated. The arithmetic: 4000 × 3000 px at 8-bit is 12 MB per frame, and 30 × 12 = 360 MB/s, against GigE's practical 115 MB/s (about 9.5 fps at this size) and USB3's 350 MB/s or more. That is why a 12 MP, 30 fps USB3 model such as the LS-CMR-0166 fits the full-resolution case. Three cases cover most counting jobs:

  • Clean, separated rods and no PC wanted: a smart camera counts on the device. The LS-SMC-0063 pairs a 1.6 MP, 60 fps sensor with an integrated polarized white light and a 16 mm lens, so the illumination question disappears with it.
  • Clumped or thin rods that need the watershed, area histograms and resolution headroom: the USB3 area scan above, feeding a PC.
  • Existing GigE wiring and relaxed rates: the 2.5 MP global-shutter GigE camera from the previous section.
Smart cameras & vision sensors1.6 MP smart camera, 60 fps, 16 mm lens, white light (polarized, transparent light) (LS-SMC-0063)LS-SMC-0063
Interface
GigE
Resolution
Up to 3 MP
$1,110.00View part
Specification LS-CMR-0166 MV-CU020-90GC LS-SMC-0063
Sensor model Sony IMX226 GMAX4002 –
Resolution 4000 × 3000 (12MP) 2048 × 1200 1440x1080
Max. frame rate 30 fps 49 fps @ 2048 × 1200 Bayer GB 8 60 fps
Pixel size 1.85 μm 4 μm × 4 μm –
Sensor size 1/1.7" 1/1.7" –
Data interface USB 3.0 Gigabit Ethernet (1000 Mbit/s), compatible with Fast Ethernet (100 Mbit/s) GigE
Digital I/O – 6-pin P7 connector provides power and I/O, including opto-isolated input × 1 (Line 0), opto-isolated output × 1 (Line 1), bi-directional non-isolated I/O × 1 (Line 2). RS232, 2 Opto-isolated inputs, 3 Opto-isolated outputs
Power supply – 9 VDC to 24 VDC, PoE is optional 9-26 V DC 2 A
Power consumption – Typ. 2 W @ 12 VDC <18 W
Weight – Approx. 100 g (0.2 lb.) <350 g
Ingress protection – IP30 (under proper lens installation and wiring) IP65
Humidity – 20% RH to 95% RH (no condensation) 20%-95%, no condensation
Price $560.00 $309.00 $1,110.00

Values as printed in each product's data sheet; open a part for its drawing and full specifications.

The area scan camera and smart camera finders at /shop/cameras/ filter by resolution, shutter and interface, and the components buyer's guide maps the whole chain. If none of the three cases fits, start the camera configurator with your rod width, field of view and line speed.

How do you verify the count before it goes live?

Prove the count on a golden set before production: annotate 50 images by hand, require an exact match on separated rods and a total error of 1% or less on clumped images, and rerun the set after every lighting or threshold change. Build the golden set while the system is still on the bench: fifty images spanning the shift — start of shift, after cleaning, worst ambient light — each with a hand count. Freeze the recipe, then rerun the set after any change to lighting, threshold or mechanics; a recipe that passes the set but drifts in production is telling you the illumination moved.

Live, watch the hourly mean: a drift of more than 3% from baseline sends you to the light first and the camera second. Keep the images beside the recipe file so the next person can rerun them without archaeology. Everything above reduces to four numbers you can write on the fixture: 12.5 µm/px scale, 19,000 px minimum area, 5:1 aspect cut, 0.25 ms exposure. For a set-up these cases do not cover — different contrast, faster line, smaller rods — send the part dimensions and speed through the contact page and we will work the thresholds out with you.

Questions

Does the same recipe count stained bacilli in a microscope smear?

Yes — only the scale changes. Assume a 3.45 µm camera pixel behind a 100× objective: the scale on the sample is 0.0345 µm/px, so a 0.5 × 3 µm bacillus spans about 14 × 87 px, an area near 960 px. Set the minimum area to a quarter of that, 240 px, keep the 5:1 aspect cut, and lean on the watershed, because smears clump far more than feeder parts do.

Why does my count drift between identical images?

Three usual causes: an auto mode re-balancing every frame, a supply rail sagging so the light's brightness drifts, and vibration changing which rods touch. Turn every auto mode off, power the light from a constant-current or strobe controller, and rerun the golden set after any mechanical change.

Can a smart camera run this whole recipe?

Yes, when the rods arrive separated and the recipe needs only threshold, area and aspect filters — those are standard tools in smart camera libraries. Clumped rods need a distance-transform watershed and rotated-rectangle metrics; check the camera's tool set includes them before ordering, otherwise run the count on a PC.

What aspect ratio cut separates rods from round junk?

Start from both populations, not a textbook value. Measure the rotated-rectangle ratio on 20 known rods and 20 known rejects; the worked example's pins sit near 6.7:1 and round debris near 1:1, so any cut between 3:1 and 5:1 separates them — 5:1 leaves the wider margin.