System designHow-to

Check if an object is fully inside the image frame

Yes: an object is fully inside the image frame only when its bounding box stays clear of a border band. Rule, margin in pixels and mm, example and code.

LumaSpec Vision10 min read Share on LinkedIn

On this page 9 sections
  1. 01The rule: a border band on the box
  2. 02Why the detector cannot answer it alone
  3. 03Sizing the margin
  4. 04Worked example: the band in pixels and millimetres
  5. 05Step by step, with code
  6. 06When a border test is wrong
  7. 07Running it on a smart camera or a PC
  8. 08Checks before you switch it on
  9. 09Questions

An object detector answers what and where, never how much of it. Train it on line images and it will label the rear half of a gearbox casing with the same class and a high score, because partial objects in its training set carried the full class label. If your station must treat "part fully in frame" differently from "part clipped at the edge" — measure versus skip, accept versus re-present — you can build that classifier without retraining anything. Take the bounding box the detector already returns and test it against a border band a few pixels wide. This guide gives the rule, the margin size in pixels and millimetres, the optics arithmetic behind it, working code with its test, and the two cases where a border test is not enough.

The rule: a border band on the box

Run the detector you have today and classify by geometry. The object is treated as fully in frame when every edge of its box lies at least m pixels inside the image; any edge inside the border band means treat the object as clipped — reject it, or signal the station to re-present the part. W and H are the width and height of the image you actually process, with pixel coordinates running from 0 to W − 1 and 0 to H − 1.

partial = (x1 < m) or (y1 < m) or (x2 > W - 1 - m) or (y2 > H - 1 - m) m = max(5 px, 0.02 × W)

At 1920 px wide that band is 38 px; at 512 px it is 10 px. This one test turns any detector into a complete-versus-partial classifier, because the only run-time output that describes geometry is the box itself.

Why the detector cannot answer it alone

Training data is the reason. Detection datasets label a half-visible object with its full class name, so the model is rewarded for firing on fragments — exactly what you want when parts enter the field of view, and exactly why the class and the confidence score never separate the two cases. A clipped part can even score higher than a complete one sitting in weaker light.

Completeness is a statement about the object's extent relative to the frame borders, and the standard run-time output — class, score, box — does not carry it. Some annotation workflows tag truncated instances while labelling, but that tag does not reach the output you consume. The box coordinates do describe position relative to the borders, so the completeness decision belongs in your post-processing. If you have never inspected what a detector emits, the debug object detection output coordinates walkthrough covers every field and its coordinate system.

Sizing the margin

The band has one job: stay wider than the frame-to-frame jitter of the box edges, so a stationary, complete part never flips to partial. Two settings cover it.

  • Default, no measurement: m = max(5 px, 0.02 × W). The 2 % term covers typical detector coordinate jitter plus the smeared edge pixels at the border; the 5 px floor keeps small sensors usable.
  • Measured: capture about 100 frames of a stationary part, record x1 and x2 each frame, and take the spread (largest minus smallest). Set m to at least three times that spread. If the spread exceeds 2 % of W, the problem is imaging, not the threshold — fix triggering and lighting first.

Two coordinate traps eat more installations than the threshold does. If images are resized or letterboxed before inference, run the test in original image coordinates or the padding shifts every edge; the bounding box offset on new images: the double letterbox fix article shows the undo. And read W and H from the stream at start-up rather than hard-coding them — object detector input dimensions: height, width, and padding explains how easily the two axes get swapped.

Worked example: the band in pixels and millimetres

Take a flat 300 × 200 mm part presented to a fixed station, a 1920 × 1200 pixel image, and a 400 mm horizontal field of view.

  • Band: m = max(5, 0.02 × 1920) = 38 px.
  • Scale: 400 mm ÷ 1920 px = 0.208 mm per pixel.
  • Band in object space: 38 px × 0.208 mm/px = 7.9 mm. The part needs about 8 mm of clear space to every border before the test calls it clipped.
  • Part width on the sensor: 300 mm ÷ 0.208 mm/px = 1440 px. Centred, its box runs from x1 = (1920 − 1440) ÷ 2 = 240 to x2 = 1680, both far outside the band.
  • Placement tolerance: the part may drift 240 − 38 = 202 px ≈ 42 mm sideways before the verdict flips. Vertically it covers 200 ÷ 0.208 = 960 px, y1 = 120 px, so it may drift 120 − 38 = 82 px ≈ 17 mm.
  • Camera distance: a 1/2.4-inch sensor is about 5.9 mm wide; with an 8 mm lens the working distance for a 400 mm field of view is WD ≈ FOV × f ÷ sensor width = 400 × 8 ÷ 5.9 ≈ 542 mm (thin-lens estimate).

The same arithmetic reversed is the station rule: field of view = part + 2 × band, so 300 + 2 × 7.9 ≈ 316 mm horizontally and 200 + 15.8 ≈ 216 mm vertically. Mount the camera so the guaranteed field is slightly larger than that, and the software test becomes a guard against placement mistakes rather than the thing that saves every cycle.

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.

The lens focal length calculator runs this lens-side arithmetic for your own sensor size and field of view.

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

Step by step, with code

  1. Read W and H from the live stream at start-up; do not copy them from a datasheet or a config file.
  2. Set the band m = max(5, round(0.02 × W)); replace it with three times the measured edge spread once jitter is logged.
  3. Run the detector and take every box in original image pixel coordinates, with any letterbox padding undone first.
  4. Clamp each box to the image: some models predict edges slightly outside 0 to W − 1.
  5. Apply the four-way test and attach the verdict, complete or partial, to the detection record.
  6. Route on the verdict: complete parts go to measurement, partial parts to a reject output or a re-present signal.
  7. Prove the test: slide a good part over until one edge visibly touches the border, and confirm the verdict flips at the band, not at the pixel edge.
# Illustrative post-processing: one box in, one verdict out.
W, H = 1920, 1200                 # image size in pixels, read from the stream
m = max(5, round(0.02 * W))       # border band: 38 px at this width

def is_complete(box):             # box = (x1, y1, x2, y2), floats, image pixels
    x1, y1, x2, y2 = box
    x1, y1 = max(x1, 0.0), max(y1, 0.0)          # clamp to the image
    x2, y2 = min(x2, W - 1.0), min(y2, H - 1.0)
    return (x1 >= m and y1 >= m and
            x2 <= W - 1 - m and y2 <= H - 1 - m)

# Stationary 300 mm part, centred: box (240, 120, 1680, 1080)
print(m, is_complete((240.0, 120.0, 1680.0, 1080.0)))   # 38 True
# Same part pushed against the left border: x1 = 0
print(is_complete((0.0, 120.0, 1440.0, 1080.0)))        # False

On a moving line, require the same verdict on 3 consecutive frames before acting on it; single-frame jitter outliers then cost you nothing.

When a border test is wrong

Two failure modes, each with a committed fix.

Mid-frame occlusion. A hand, a fixture or a neighbouring part can hide a third of the object while the box sits comfortably inside the band. For a rigid part with stable corners, run a corner detector on the box crop and require all four corners; any missing corner means partially hidden. That costs a second small model or a classical corner finder, and it works well on flat parts such as covers, plates and labels.

Soft or irregular objects. Corners do not exist on a bag of granulate. Segment the object instead and compare visible area with nominal area: with the numbers above, a complete 300 × 200 mm part covers 1440 × 960 = 1.38 megapixels. Accept at a visible-to-expected ratio of 0.90 and flag anything below — segmentation edge error runs a few percent, so 0.90 ignores boundary noise without hiding a real 10 % occlusion.

Situation Method Decision rule
Fixed station, one rigid part Border band on the detector box m = max(5 px, 2 % of W); any edge in the band means partial
Part can be covered mid-frame Corner counting inside the box All four corners found means complete
Irregular or soft parts Segmentation area ratio Visible ÷ expected ≥ 0.90 means complete
Part moves through the frame Border band, gated in time Complete on 3 consecutive frames means measure

For measuring the object itself rather than its visibility, read bounding box area vs object area: which should you measure. If the next step needs the part's real dimensions rather than a visible/hidden flag, 2D bounding box to 3D: estimate object extent from depth extends the box with a depth stream.

Running it on a smart camera or a PC

The test itself is a handful of comparisons, so the detector decides where it runs. Smart cameras execute detection and verdict on the device and switch a discrete output to the PLC — no PC in the loop at run time. A standard area scan camera streams to a PC, where the same post-processing runs next to the inference. The trade-off is cabling and cycle budget, not accuracy: the border test is identical in both.

The MV-SC5060XM-08M-WBN is a 6 MP mono smart camera with an 8 mm focal length on an M12 mount and a GigE interface — the lens class the worked example assumes.

Smart cameras & vision sensorsHikrobot MV-SC5060XM-08M-WBN SC5000X series Smart Camera, 6 MP, Mono, 8 mm focal length, M12-mountMV-SC5060XM-08M-WBN
Interface
GigE
Resolution
6-10 MP
Sensor format
1/2.4"
Shutter
Rolling
$1,629.00View part

For more resolution, the LS-SMC-0017 is an 8.8 MP smart camera on an x86 platform with a C-mount and a global shutter, and the MV-CS200-10UM is a 20 MP USB3.0 area scan camera with a 1-inch sensor for PC-side processing.

Smart cameras & vision sensorsSC / SC smart camera (X86 platform), 8.8 MP, USB3, Color/Mono, global shutter (LS-SMC-0017)LS-SMC-0017
Interface
USB 2.0
Resolution
6-10 MP
$1,359.00View part
Area scan camerasHikrobot MV-CS200-10UM 20 MP Area Scan Camera, USB3.0, IMX183, 2nd Gen., MonoMV-CS200-10UM
Sensor format
1"
Lens mount
C-mount
$759.00View part

When the line speeds up, the MV-CH120-11UC is a 12 MP USB3 camera with a global shutter and a 1.1-inch sensor. For the on-device versus board-level decision, edge object detection: SoC camera boards vs smart cameras compares the two, and AI vs rule-based smart camera inspection: how to choose covers when a trained detector earns its place over rule-based checks.

Checks before you switch it on

  • Band width set: 2 % of image width with a 5 px floor, replaced by three times the measured edge spread once logged.
  • Boxes converted back to original image coordinates before the test.
  • Detector proven to still detect a deliberately clipped part — it should, and then the test flags it.
  • Field of view sized to part + 2 × band in millimetres, camera mounted to deliver it.
  • Verdict and edge distances logged with each image for a week of tuning data.
  • Reject path exercised end to end before the station runs unattended.

Set the camera, lens and light together in the camera configurator, compare sensors in the camera finder, or send us the part dimensions and we will size the field of view and the band through the contact page. More fundamentals on cameras, detectors and station design sit on the systems topic page.

Questions

Can YOLO or another detector output complete versus partial by itself?

No. The images a detector trains on label a half-visible object with its full class name, so the model is rewarded for firing on fragments. That is what you want when parts enter the field of view, and it is why neither the class nor the confidence score ever separates the cases. The run-time output stays class, score and box. Completeness comes from the box coordinates in post-processing, with no retraining.

How wide should the border band be?

Start at m = max(5 px, 2 % of the image width): 38 px at 1920 pixels wide, 10 px at 512. Then measure: capture about 100 frames of a stationary part, take the spread of the x1 edge across those frames, and set the band to at least three times that spread. If the measured spread exceeds 2 % of the width, fix lighting or triggering before touching the threshold.

The occluder is in the middle of the frame, not at the border. Now what?

The border band cannot see it, so add a geometry check inside the crop the box defines. For a rigid part, apply a corner detector and demand four hits before calling it complete. For irregular parts, segment and compare areas: at 0.208 mm per pixel a complete 300 × 200 mm part covers 1440 × 960 = 1.38 megapixels, so accept at a visible-to-expected ratio of 0.90 and flag anything lower.

Do I have to retrain the model to add this check?

No. The test is post-processing on the box your detector already returns, whatever framework it runs in. What changes is downstream routing: complete parts flow to measurement, partial parts to a reject or re-present output. The only model-side work is optional: a small corner detector if you also need to catch mid-frame occlusion, or a segmentation model for irregular parts.

Should the camera guarantee the full frame instead of testing for it?

On a fixed station, yes. That is the stronger control. Size the field of view to the part plus twice the band: 300 + 2 × 7.9 ≈ 316 mm with the numbers in this guide. Then the software check catches placement mistakes instead of rescuing every cycle, and a correctly presented part never lands near the band.