ApplicationsGuide

Edge based template matching steps for bottle detection

Build an edge template of a bottle, set pixel budget, pyramid levels, angle range and accept score, with a worked sizing example and tuning checks.

LumaSpec Vision12 min read Share on LinkedIn

On this page 11 sections
  1. 01Why gradient directions, not gray levels
  2. 02Step 1: fix the pixel budget and the exposure ceiling
  3. 03Step 2: light the edges, not the bottle
  4. 04Step 3: build the template from a golden bottle
  5. 05Step 4: pyramid levels, angle step and search speed
  6. 06Step 5: set the accept score, then prove it
  7. 07Worked example: sizing a 330 ml PET bottle station at 0.5 m/s
  8. 08Choose the camera route
  9. 09Checks before production
  10. 10When edge matching is the wrong tool
  11. 11Questions

A bottle detector has to keep its match while light, position and tilt change between scenes: day shift and night shift, backlit and front-lit, bottle at the guide rail and bottle mid-belt. Gray-level template matching gives up first, because a scene forty gray levels brighter breaks the correlation outright. Edge based matching keeps working, because it compares gradient directions, which barely move when brightness does. This guide walks the build in five steps — pixel budget, lighting, template, pyramid search, accept score — then a worked sizing example for a 330 ml PET line and the checks that prove the recipe before it runs in production.

Why gradient directions, not gray levels

Decide the method before you open any menu. If scene brightness can change between shifts, batches or cells, use edge based template matching: a model of gradient directions scores each candidate pose with the average cosine between model and image edge directions, so a uniform brightness or gain shift of tens of gray levels leaves the score almost untouched. Normalized gray-level correlation fails in exactly that case, because the pixel values themselves move.

The mechanics in one formula. The edge detection threshold basics guide covers extracting the gradients; matching then stores a direction per template point and averages the agreement:

θ = atan2(∂I/∂y, ∂I/∂x) score(pose) = (1/N) Σ cos(θ_model,i − θ_scene,i)

A score of 1.0 means every model edge found a parallel partner in the image. Occlusion and clutter cost partial credit only, so a bottle half-hidden by a neighbour still scores on its visible outline. For the wider failure modes as light drifts through a shift, the edge based pattern matching for stable results when lighting changes guide goes deeper; this article builds the pipeline itself from zero.

Decision: edge based matching whenever the lighting varies. Keep correlation only for enclosed cells with fixed light, where texture carries the pose better than outlines do.

Step 1: fix the pixel budget and the exposure ceiling

Work the numbers before the software. Two rules set the geometry:

  • Three pixels across the smallest feature that pins the pose. On a bottle that is usually the neck support flange or the cap-to-shoulder corner. Under 3 px the gradient points merge after smoothing and the score turns noisy; 5 px buys margin. If the feature cannot get its pixels, drop it from the model and pin the pose on larger structure.
  • Motion blur under one pixel. The exposure time must satisfy:
t ≤ pixel size on the object / belt speed

At a 0.5 m/s belt and the 0.67 mm pixel budget the worked example below derives, that is 0.67 ÷ 500 = 1.3 ms. Beyond it, smear softens exactly the gradients you match on, and gain cannot bring them back.

Set the pixel budget against your own smallest feature with the calculator, then let it filter the camera list:

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

Step 2: light the edges, not the bottle

The fastest route to a match that survives scenes is to make your edges the strongest gradients in the image. For PET and glass bottles that means backlight: a diffused bar light behind the belt turns wall, shoulder and cap into clean bright-dark transitions, and an empty bottle stops being invisible. Keep the bottle within about 100 mm of the diffuser so the silhouette edge stays sharp, and make the lit width wider than the field of view. The lights finder sorts by size, colour and diffusion, and configuring a light takes the lit width as its first input.

Front lighting stays right for filled, opaque bottles and cap text; against glossy cap glare there, cross polarization works, as the cross polarization surface glare guide shows. Ambient light leaking into the cell moves the score over the day; the ambient light control guide covers shielding and exposure coupling, and the glass bottle lighting guide shows how much a light fix buys before any software change.

A diffused bar sized for a single-lane field of view:

The LS-BAR-1276 is a diffused white bar light, 372 x 28 mm, on 24 V.

Bar lightsBar light, 372 x 28 mm, White (LS-BAR-1276)LS-BAR-1276
Size
200-400 mm
Type
Diffused
Voltage
24 V
$439.00View partBuild a system with it

Step 3: build the template from a golden bottle

Grab the reference under the final lighting, lens and working distance — never from a photo taken with another optic. Then:

  1. Draw the ROI on informative structure: cap, shoulder curve, base curve. Long straight walls add little, because a straight edge pins translation but not rotation; let the curves carry the model. If the whole bottle cannot fit the view, match the informative region and check presence separately — the template larger than the field of view guide walks that split.
  2. Set the model gradient threshold at roughly three times the gradient noise measured on the flat background, so weak texture edges stay out.
  3. Keep the surviving points moderate in count and spread over the outline; the FAQ below carries working numbers. Thin out any stretch of wall that contributes a disproportionate share.
  4. Set the search angle range from the line: jog twenty bottles through and read the tilt spread. A rail-guided belt typically lands near ±10°.
  5. Save template image, point list, gradient threshold and angle range together as one versioned recipe.

Step 4: pyramid levels, angle step and search speed

Coarse-to-fine search is what makes the method fit a cycle time. Halve the template repeatedly until the longest side reaches roughly 13 px: a 400 px tall silhouette runs through levels 400, 200, 100, 50, 25 and 13 px — five halvings, six levels.

The speed win is geometric. On a 1600 x 1200 frame, brute force at level 0 visits 1,920,000 positions; at the top level the grid is 50 x 38, about 1,900 positions — a thousand times fewer — and the finer levels only refine the handful of surviving candidates.

Set the angle step from geometry: a model point at radius r px moves r x Δθ px per radian of search rotation, so keep Δθ ≤ 1/r:

Δθ_max ≈ 1 / r radians, with r = template radius in px at that pyramid level

At full resolution with r = 200 px that allows 0.005 rad ≈ 0.3°; at the top level, where the template radius is about 6.5 px, a step near 9° still keeps every point within a pixel of home. Missed tilted bottles point at the coarse angle step first. The pattern matching update rate guide trades search size against cycle time, and the pattern search sensitivity and accuracy tuning guide covers the score-versus-position dial-in.

Step 5: set the accept score, then prove it

The score is a mean cosine, so it reads intuitively: 1.0 is perfect outline agreement, and the golden scene itself should return 0.9 or higher. Workable starts: accept at 0.7 on a clean backlit silhouette; 0.5 to 0.6 when bottles touch, occlude or sit over clutter; and never below the empty-belt floor — run the matcher on empty images and set the threshold a real margin above the highest score they return, with the working margin in the FAQ.

Then close the loop: record the lowest true-match score across the wander and tilt range, and keep the threshold under it with margin too. If no gap exists between empty scenes and true matches, the fix is the model or the light, not the threshold. When the pose feeds a pick on a near-symmetric silhouette, angle accuracy has its own ceiling regardless of score — the symmetric part orientation matching guide explains the limit.

Worked example: sizing a 330 ml PET bottle station at 0.5 m/s

Inputs, all typical single-lane values — substitute your own and redo the arithmetic:

Input Value Source
Body diameter 64 mm typical 330 ml PET value
Cap diameter ≈ 30 mm typical value
Visible bottle height 150 mm typical value
Belt speed 0.5 m/s typical single-lane value
Wander across belt ±15 mm measure on your line
Tilt ±10° measure on your line
Smallest pinning feature 2 mm neck flange typical value
Sensor 1/1.7" format, ≈ 7.4 x 5.6 mm typical format size
Working distance 300 mm mounting clearance choice

The rules in words: pixel budget from the 3 px rule; field of view from the worst-case silhouette sweep; focal length from magnification; exposure from the blur rule.

  1. Pixel budget: 2 mm ÷ 3 px = 0.67 mm/px.
  2. Tilt sweep at the bottle top: 150 x sin(10°) = 150 x 0.174 = 26 mm. Width: 64 + (2 x 15) + 26 = 120 mm, take 130 mm with margin. Height: 150 x cos(10°) ≈ 148 mm, take 160 mm with margin.
  3. Minimum pixels: 130 ÷ 0.67 = 194 px across and 160 ÷ 0.67 = 239 px tall — a 240 x 200 px budget that even a 0.3 MP VGA sensor clears with its 640 px.
  4. Exposure ceiling: 0.67 ÷ 500 = 1.34 ms, so set 1 ms; blur = 500 x 0.001 s = 0.5 mm, inside one 0.67 mm pixel.
  5. Magnification on the tight (height) axis: m = 7.4 ÷ 160 = 0.046. Focal length: f = 300 x 0.046 ÷ (1 + 0.046) = 13.3 mm → nearest stock lens, 12 mm.
  6. Check at 12 mm: m = 12 ÷ (300 − 12) = 0.0417, FOV = 7.4 ÷ 0.0417 = 178 mm tall and 5.6 ÷ 0.0417 = 134 mm wide — both budgets met.
  7. Object-side pixel size for a 4 µm class sensor pixel: 4 ÷ 0.0417 ≈ 96 µm = 0.096 mm; the 2 mm flange spans 2 ÷ 0.096 ≈ 21 px, seven times the 3 px minimum.
m = sensor size / FOV f = WD x m / (1 + m) FOV at stock lens = sensor / m'
Lens focal length, field of view and working distance
Units
Solve for
Focal length
– mm
Field of view
– mm
Working distance
– mm
Magnification
– x
Angle of view
– °
Pixel on the part
– mm

Thin-lens model: m = sensor width / field of view, f = WD × m / (1 + m). Accurate to a few percent above about 150 mm; check the lens's minimum object distance.

Find lenses with this focal length Open the full calculator

Result: geometry was never the binding constraint — the pixel budget is oversatisfied by roughly an order of magnitude on any current sensor, and 0.3 MP would do. The binding constraint is edge contrast, which is why the backlight decides the score. Decision: a 12 mm lens at 300 mm working distance covers this bottle with margin on both axes; hold exposure at or under 1.3 ms at 0.5 m/s, and if the belt speeds up to 1 m/s the ceiling halves to about 0.67 ms — buy that light with strobe overdrive, not gain.

Choose the camera route

Route Good for Watch for
Vision sensor, matching on the device one lane, one recipe, quickest commissioning model storage per SKU; fixed optics
Smart camera with integrated light tight cells with no room for a separate light fixed light and lens set the working distance
Area scan camera plus PC library multi-lane lines, large model libraries, logging PC, trigger wiring and software to plan

Every route lives in the smart cameras category or the area scan cameras category; the camera finder filters by shutter, resolution and mount.

On the device route, the HK-TD032-12-WBN vision sensor pairs a 1.6 MP mono global-shutter sensor, 1/2.9" format and M12 mount with Fast Ethernet — mono and global shutter being the right defaults for a moving belt.

Smart cameras & vision sensorsHikrobot HK-TD032-12-WBN 1.6 MP mono vision sensorHK-TD032-12-WBN
Interface
Fast Ethernet
Resolution
Up to 3 MP
Sensor format
1/2.9"
Shutter
Global
$1,699.00View part

Where you want the light included, the LS-SMC-0063 smart camera integrates a 1.6 MP sensor, a 16 mm lens, white light and 60 fps on GigE.

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

For the PC route, the MV-CU020-90GC area scan camera brings a 2.5 MP color sensor in the 1/1.7" format the example assumes, global shutter and C-mount on GigE; matching runs on the intensity channel and the colour stays available for label checks downstream.

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

Checks before production

  • Empty-belt set: matcher run on bottles-off images, highest score recorded, threshold kept a set margin above it — working numbers in the FAQ.
  • Twenty-bottle jog through the full wander and tilt range: lowest true score recorded; threshold at least 0.1 under it.
  • Touching-bottle run: neighbours must degrade the match to no-match, never to a wrong pose.
  • Light drift run: morning, evening and one shielded-lamp-off case; a score spread under about 0.1 says the light is controlled.
  • Cycle margin: match time under half the frame budget; camera frame rate at least twice the bottle rate.
  • Recipe file: template, gradient threshold, angle range, light settings and score baseline stored and versioned together.

When edge matching is the wrong tool

  • Transparent bottles in front light: no gradient, no match. Backlight first, or accept a presence check only.
  • Hunting defects rather than position: lighting then blob or anomaly methods fit better — the dark contaminant detection guide shows the lighting-first order.
  • Heavy overlap that hides most of the outline: that is a depth problem, and the 2D or 3D camera comparison for conveyor picking lays out the switch.
  • Fixed light, fixed part, texture-borne pose: correlation or a trained model can outscore edges.

Bottle detection across changing scenes is a lighting and model discipline more than a tuning exercise: set the pixel budget, backlight the silhouette, prove the score against empty scenes, and the recipe holds across shifts. Configure the camera chain from the call-out below, or tell us about your scene and we will help you spec it.

Build the whole station

The configurator matches a camera, lens, light, light controller and cables to each other, step by step, and lists only parts that fit what you already picked.

Start with the camera

Questions

How many edge points should a bottle template keep?

Between 40 and 80 points spread over the cap, shoulder curve and base. Below about 30 the score turns noisy and background clutter can climb past 0.5; above roughly 120 the search slows with no accuracy gain. Spread beats count: if one straight wall holds more than a third of the points, thin it out, because a straight edge cannot pin rotation.

How do I prove the accept score before going live?

Run the matcher on 20 or more images of the empty belt and record the highest score returned; on a backlit line this usually lands below 0.4. Set the accept threshold at least 0.15 above it, then confirm real bottles through the full wander and tilt range still clear it. Empty images scoring above 0.5 mean the model kept too many weak edges: raise the gradient threshold and rebuild.

Does edge based matching work on clear, empty PET bottles?

Only with backlight. In front light a clear wall produces almost no gradient, so the score collapses; behind a diffused bar light the wall, shoulder and cap become strong bright-dark transitions and the same pipeline applies. Filled or amber bottles behave more like solids and keep a usable front-lit edge, though the backlight still gives the cleaner model and the higher scores.

Can one template cover several bottle sizes?

Not reliably. Matching is scale sensitive, and a model trained on a 330 ml bottle drops in score on a taller or wider SKU. Train one template per shape; where a family shares an identical shoulder and cap, train on that shared region and search a limited scale range of roughly plus or minus 10 percent, then validate the score on every size before you trust the recipe.

Why does the score fall when the bottle nears the frame edge?

Lens and light falloff soften edges toward the border, gradient magnitudes drop under the model threshold, fewer points contribute and the score sinks even though the pose is correct. Keep the search region inside the middle 90 percent of the frame, flatten the illumination, or retrain the template from an image grabbed at that same border position so its point set matches the softer gradients there.