You want the widest chord of the fruit, not the twig. A polygon drawn over the stem cleans one still frame; it cannot follow a twig that changes length and angle on every cup, so the measurement drifts with every operator adjustment. Morphology can: open the binary image with a disk larger than the twig and far smaller than the fruit, then clear every pixel outside the opened region. The twig cannot contain the disk and disappears; the body can, so its outline survives untouched. This walkthrough builds the station around that idea: silhouette lighting, pixel scale, lens and working distance with the arithmetic shown, the three-line cleanup, the diameter readout and the 100-fruit proof. Design inputs are dessert apples 60–100 mm wide with stems and twig stubs 2–5 mm thick — the usual ranges — and a tolerance of ±0.5 mm on diameter.
Why an opening removes the stem (and a drawn region does not)
An opening is an erosion followed by a dilation with the same structuring element. Erosion deletes every pixel where a disk of radius r does not fit inside the foreground; dilation grows what is left back out. A twig 3 mm wide has nowhere a 4 mm-radius disk fits, so erosion deletes it and dilation cannot resurrect it. The apple body, 60 mm across, holds the disk easily and re-emerges at full width. The final step — clearing everything the opening did not cover — keeps the original pixels of the body, so the outline stays exactly where the threshold put it while the twig pixels are gone.
Concavities such as the stem basin get filled by the opening itself, but the clear step only removes pixels, never adds them, so the true edge is untouched. The same logic removes dust specks from thread images and spurs off a skeleton line; see removing spurs while keeping the longest line and filtering dust before the threshold for the sibling cases.
Fix the pixel scale before anything else
Work from the fruit outward. Largest apple 100 mm, twig adding 20 mm of height, cups wandering ±30 mm across the belt: a field of view of 160 × 120 mm (4:3) covers it. The LS-CMR-0166 is a 12 MP USB3 camera with a 1/1.7-inch sensor, 4000 × 3000 pixels, 30 fps and a rolling shutter. Across that sensor width the field gives 160 mm ÷ 4000 px = 0.04 mm per pixel — the 5 mm twig is 125 px wide, and the ±0.5 mm tolerance is 12.5 px, comfortable for sub-pixel edges.
- Sensor format
- 1/1.7"
- Interface
- USB3
- Resolution
- 12 MP
- Shutter
- Rolling
Two camera-level checks. Data rate: 4000 × 3000 × 1 byte × 30 fps = 360 MB/s, about 2.9 Gbit/s, inside USB3 with headroom — so take 8-bit output, because binary work has no use for 12-bit. Shutter: 30 fps rolling reads a full frame in about 33 ms, and a belt at 100 mm/s would smear the silhouette by 3.3 mm. Stop the cup for the shot; if the line never stops, use a global-shutter 12 MP USB3 camera such as the MV-CH120-11UC with a strobed exposure. Turning pixels into millimetres is the same job as measuring crack width in pixels and mm, and the scale deserves the same audit as calibrating from a ruler in the scene.
Lens and working distance
The 1/1.7-inch format carries a nominal image area of 7.6 × 5.7 mm (9.5 mm diagonal). Magnification and focal length follow directly:
With 350 mm of free working distance under the camera bracket: m = 7.6 mm ÷ 160 mm = 0.0475, and f = 350 mm × 0.0475 ÷ 1.0475 = 15.9 mm — a 16 mm lens. The LS-FFL-0567 is a 16 mm C-mount lens rated for 1.1-inch sensors and 20 MP, aperture F2.8 to F22, distortion under 0.3 %.
- Type
- Machine vision fixed
- Sensor format
- 1.1"
- Mount
- C-mount
- Resolution
- 16-25 MP
Its 1.1-inch coverage swallows the smaller 1/1.7-inch sensor with room to spare. Check the pairing at the real working distance: m = 16 mm ÷ (350 − 16) mm = 0.0479, so the sensor width spans 7.6 ÷ 0.0479 = 159 mm of belt and the height spans 119 mm. The 100 mm apple plus its 20 mm twig needs 120 mm, so the tallest twig tip just clips the frame — harmless, because that twig is about to be deleted anyway.
Aperture: fruit rides ±10 mm high or low in the cups. Allow 2 pixels of blur (0.08 mm on the belt) and convert it to the sensor: 0.08 mm × 0.0479 = 3.8 µm. Total depth of field at f/8:
DOF = 2 × 8 × 0.0038 mm × 1.0479 ÷ 0.0479² = 28 mm, so ±14 mm — the cup variation fits. Diffraction at f/8 with the red backlight (λ ≈ 630 nm): Airy diameter = 2.44 × 0.00063 mm × 8 = 12.3 µm on the sensor, about 6.4 px on the belt. A silhouette edge thresholds at its 50 % crossing, which stays put under that blur, so f/8 is the working aperture; stop to f/11 only if cups vary more than ±14 mm, which buys ±19 mm. The depth of field calculator runs the same arithmetic, and the reasoning behind both numbers is in aperture, f-number and depth of field and Airy disk size and the diffraction limit. The same field-of-view formulas on another sensor are worked through in blister inspection working distance and field of view.
Light it as a silhouette
The binary image comes cheapest from behind. The LS-BAR-1168 is a diffused red bar light, 423 × 40 mm, running on 24 V.
- Size
- 400 mm +
- Type
- Diffused
- Voltage
- 24 V
Stacked three high, edge to edge, the bars put 423 × 120 mm of even red behind a 159 × 119 mm field — length and height both cover it. Red illumination also sidesteps the red-versus-green skin problem: every variety is a black shape on a red field. Drive the bars continuously and set a 4 ms exposure; even a cup creeping at 30 mm/s during the shot smears the edge by only 30 mm/s × 0.004 s = 0.12 mm, 3 px against a 12.5 px budget. Read the levels rather than hoping: background above 200 grey, fruit below 60, threshold at 128. If you dim the bars with PWM, keep the exposure a whole number of drive periods or black bands crawl through the frame — the diagnosis is in black bars in camera images: flicker versus exposure.
Keep the cup rim out of the silhouette: low-profile cups plus a mask over the bottom 10 % of the frame, because a rim is wider than any twig and the opening will keep it.
Three lines that remove the stem
Run this on every frame:
- Threshold the backlight frame at mid-grey and fill interior holes; call the result
bw, a logical array with the fruit bright. - Open with a disk whose radius comes from the pixel scale, not from habit.
- Clear every pixel the opening did not cover.
bw = imread('apple_frame.png') > 128; % logical array, fruit bright
disk = strel('disk', 100); % radius 100 px = 4.0 mm at 0.04 mm/px
body = imopen(bw, disk); % twig deleted, body intact
bw(~body) = 0; % keep only pixels inside the body
The radius rule, with the arithmetic. The disk must not fit anywhere in the twig, so 2r must beat the widest appendage: 2 × 100 px = 200 px = 8 mm against a 5 mm twig, a 60 % margin. The disk must fit comfortably in the body: the narrowest fruit, 60 mm = 1500 px, is 7.5 times wider than 2r, so misshapen shoulders survive too. On a frame of an 80 mm apple with a 4 mm twig leaning out, the widest row falls from 2100 px to 2000 px and holds there — the twig's 100 px are gone, the body edge untouched.
| Disk radius | Deletes appendages up to | Verdict for 60–100 mm apples |
|---|---|---|
| 50 px (2.0 mm) | 4.0 mm | too small — a 5 mm twig stub survives |
| 100 px (4.0 mm) | 8.0 mm | correct — full twig margin, body 7.5× wider than 2r |
| 200 px (8.0 mm) | 16 mm | acceptable, less margin on lopsided fruit |
| 400 px (16 mm) | 32 mm | too big — 2r passes half the narrowest fruit |
Measure the diameter on the cleaned mask
Scan the rows of the cleaned mask between 20 % and 80 % of the fruit height — the equator band — and take the longest run of foreground pixels as the widest chord: diameter = chord px × 0.04 mm/px. An 80 mm apple reads 2000 px × 0.04 = 80.0 mm. Sub-pixel edge fitting holds each outline edge to about ±0.2 px = ±0.008 mm, so the chord carries ±0.02 mm of edge noise, a rounding error against the ±0.5 mm tolerance.
Reject rather than repair three frame types: the mask touches the frame edge (fruit out of place), the mask area leaves the band set by 60–100 mm fruit, or two masks merge into one component. The same widest-chord method drives extruded rope diameter measurement; the difference here is that a living appendage must be deleted first.
Prove it on 100 fruit
- Run 100 cups of calibrated fruit past both a caliper and the camera, and regress the system reading on the caliper reading.
- Accept slope 0.99–1.01 and offset within ±0.2 mm. A steady offset is a scale error: place a 75.0 mm gauge block on the belt where the fruit sits, demand 75.0 ± 0.2 mm, and correct the mm-per-pixel value — not the morphology.
- Add the worst case on purpose: 20 frames with 5 mm twig stubs at odd angles. The diameter distribution must not move; if it jumps, the disk radius is undersized.
After commissioning, the weekly check is the gauge block alone.
When opening is the wrong tool
- A leaf. A leaf is wider than 2r, so the opening keeps it and the chord reads long. Detect and reject instead: mask area above the 100 mm fruit band, or solidity (mask area ÷ convex hull area) below 0.9 where a clean apple sits near 0.98.
- Touching fruit. Two fruit in contact form one body wider than the disk. Hold cup pitch so silhouettes stay at least 5 mm (125 px) apart and the labeling pass separates them before opening. A line that cannot hold the gap needs a split along the concave neck — a segmentation job, not morphology.
- Stem pointing at the camera. A top-down view hides the stem inside the outline, so the chord is already stem-free, but on stem-up fruit that view cannot see the equator. The side view built here, stem up, camera horizontal, is the arrangement that measures the graded diameter.
- Cherry-sized fruit. The radius shrinks toward a few tens of pixels, where threshold ripple starts to survive the opening as stumps. Below about 30 px radius, measure the chord in a fixed band just under the stem base and skip morphology.
Checklist: a stem-free diameter station
- Field of view 160 × 120 mm from 100 mm fruit + 20 mm twig + ±30 mm wander.
- 0.04 mm/px from 4000 px across 160 mm; gauge block proves it weekly.
- 16 mm lens at 350 mm working distance; magnification 0.048.
- f/8 for ±14 mm depth of field; f/11 if cups vary more.
- 4 ms exposure on a continuously driven red backlight; threshold at 128.
- Cup stopped during exposure, or a global-shutter camera with strobe.
- Disk radius 100 px (4.0 mm): appendages to 8 mm deleted, 1500 px body kept.
- Clear outside the opened mask; never reconstruct.
- Widest chord in the 20–80 % height band, multiplied by the scale.
- Reject on frame-edge contact, area band or merged components.
More station walkthroughs like this one sit under the applications topic page.
Questions
How big should the disk be to remove the stem?
Fix it from the scale: the radius must exceed half the widest appendage, and twice the radius should stay under half the narrowest fruit. At 0.04 mm per pixel a 5 mm twig needs more than 63 px, so 100 px is the working value — it deletes anything under 8 mm wide while a 60 mm fruit, 1500 px across, keeps every shoulder. Too small leaves a stump; too large eats into lopsided fruit.
Why not draw a region over the stem and fill it?
A hand-drawn polygon with a region fill cleans one picture, not a shift. Twig angle, length and side change cup to cup, so a fixed polygon either misses stem pixels or bites into the shoulder and biases the widest chord low. An operator redrawing it per fruit costs seconds and audits badly. Opening adapts to each frame with no coordinates, and one parameter passes every cultivar.
Does opening shrink the apple and bias the diameter?
No. Erosion then dilation with the same disk cancel exactly on a round body, and the final clear step keeps only original pixels, so the outline that remains is the threshold's own edge, one pixel for one. On 100 fruit the chord should track calipers within ±0.2 mm; a steady offset means the pixel scale is wrong — correct it with a gauge block, not by touching the morphology.
What happens when two fruit touch in the image?
They merge into one foreground body, the opening keeps it whole, and the widest chord reads a double-width fruit — nonsense data. Prevent it mechanically: cup pitch that holds silhouettes at least 5 mm apart, 125 px at 0.04 mm per pixel, lets the labeling pass split them into components before each one is opened. Merged frames should be rejected, never measured.
Can I threshold front lighting instead of a backlight?
Not for a silhouette you plan to measure. Front light puts shading, gloss and the stem basin into the outline, and the threshold wanders by whole pixels as skin colour changes variety to variety. A red backlight behind the fruit holds the background above 200 grey and the body below 60, so a fixed 128 threshold stays stable across red and green varieties all shift.


