System designHow-to

Plastic card boundary detection for vision coordinates

Use plastic card boundary detection to build a clean mask, fit a rectangle, define card coordinates, and verify placement before inspection.

LumaSpec Vision10 min read Share on LinkedIn

On this page 10 sections
  1. 01Decide what the rectangle must represent
  2. 02Prepare the image and acquisition hardware
  3. 03Build a mask that follows the card silhouette
  4. 04Select the card region and extract its boundary
  5. 05Fit the rectangle and define card coordinates
  6. 06Work through a coordinate and resolution example
  7. 07Verify the result before using it for inspection
  8. 08Diagnose common failures and limits
  9. 09Check requirements before ordering or deployment
  10. 10Questions

A rectangle around a plastic card is useful only when it follows the card rather than its printing, reflections, or background shadows. For an inspection station, the task is to separate the card silhouette, estimate its position and orientation, and express later measurements in a consistent coordinate frame. This procedure takes you from an image to a validated boundary and card-relative inspection regions, without depending on a printed code being readable.

Decide what the rectangle must represent

Use the outside of the plastic card as the location reference. Treat printed text, photographs, and codes as internal features unless they are deliberately your registration target.

Three geometric outputs serve different purposes:

Output What it represents Suitable use Main limitation
Axis-aligned bounding box Smallest enclosing rectangle aligned with image axes Cropping and coarse position checks Its dimensions change as the card rotates
Minimum-area rotated rectangle Smallest-area enclosing rectangle with free orientation Estimating card center, angle, and extent Outlying boundary pixels can change its fit
Four fitted edge lines Estimated straight portions of the card edges Stable edge directions and corner construction Needs enough visible edge length

Keep the segmented region as well as the fitted rectangle. The mask describes which pixels belong to the card; the rectangle provides a simpler geometric reference.

If your actual target is the printed symbol rather than the plastic outline, use QR code finder pattern detection. Those patterns locate the code, not necessarily the card carrying it.

Prepare the image and acquisition hardware

  1. Capture a stationary card with a visible margin around every edge. Fix focus, exposure, and illumination while developing the segmentation. A changing image makes algorithm comparisons unreliable.

Choose a background that separates clearly from the card across the expected finishes. A dark background may suit a pale opaque card, but transparent edges or glossy reflections can reverse that advantage. Judge separation using actual samples, not the nominal material color.

Browse area scan cameras or the camera finder when selecting acquisition hardware. For the rolling-shutter examples below, begin with a stopped card; moving acquisition needs a separate exposure and readout assessment.

The LS-CMR-0053 is a GigE option.

Area scan cameras12 MP GigE area scan camera, IMX226, 1/1.7" rolling shutter, 9 fps (LS-CMR-0053)LS-CMR-0053
Sensor format
1/1.7"
Lens mount
C-mount
Interface
GigE
Resolution
12 MP
$560.00View partBuild a system with it

The LS-CMR-0166 is a USB3 option.

Area scan cameras12 MP USB3 area scan camera, IMX226, 1/1.7" rolling shutter, 30 fps, 4000 × 3000 resolution (LS-CMR-0166)LS-CMR-0166
Sensor format
1/1.7"
Interface
USB3
Resolution
12 MP
Shutter
Rolling
$560.00View partBuild a system with it

Select the interface around your acquisition system, then validate the image before selecting the segmentation method. Camera specifications alone do not establish whether the card edge will be separable.

If processing begins from a saved file, check image channels and PNG data handling. Unexpected channel order or transparency can make an otherwise sensible threshold fail.

Build a mask that follows the card silhouette

  1. Convert the image into a foreground mask, with card pixels true and background pixels false. Choose the simplest method that separates the complete card over your expected operating range.

Start with grayscale thresholding when the card and background have distinct intensity distributions. Use a color-space threshold when their colors differ but their grayscale intensities overlap. Background subtraction can work well when the camera, lighting, and empty scene remain fixed.

Inspect the mask itself before measuring anything. A successful threshold should preserve the outer silhouette despite printed areas inside the card. Do not assume the largest bright region is the card; it could be a reflection or part of the fixture.

  1. Clean the mask with operations sized to the defects you actually observe. Remove isolated specks, bridge small breaks where justified, and fill enclosed holes caused by printing when those holes are not relevant to the silhouette.

For example, suppose unwanted specks span roughly 2 pixels while the narrowest silhouette detail you need spans 12 pixels. Start with a small neighborhood and compare the cleaned outline against the original image. A neighborhood approaching the useful detail size risks changing the geometry you want to measure.

If the card appears as disconnected patches, repair the acquisition or segmentation first. Filling holes cannot recover an outer contour that was never detected as a closed region.

Select the card region and extract its boundary

  1. Label connected foreground regions and reject candidates using several independent checks. Useful checks include area, expected location, aspect ratio, and whether the region touches the image border.

Use the previous accepted position only when tracking a continuous sequence. For independent cards, a previous position can bias selection toward a fixture or residual background feature.

Consider this illustrative image-space example:

  • Expected card width: about 900 pixels.
  • Expected card height: about 570 pixels.
  • Nominal rectangular area: 900 × 570 = 513,000 pixels.
  • Nominal aspect ratio: 900 / 570 = 1.58.

An initial area gate of 400,000–600,000 pixels may help reject small fragments in this example. Establish the final limits from acceptable samples, rounded corners, perspective changes, and segmentation variation. These are example thresholds, not universal acceptance limits.

Once you select the component, extract its external boundary as an ordered contour. Exclude internal hole boundaries when the task concerns the card perimeter.

Check the coordinate convention at this interface. Some image-processing functions return row and column, while drawing functions expect horizontal and vertical coordinates. In the usual image convention:

x = column y = row

The image origin is commonly at the upper-left, with vertical coordinates increasing downward. Confirm this in your library rather than assuming it. The coordinate-indexing guide explains how coordinate-order mistakes produce misplaced overlays.

Fit the rectangle and define card coordinates

  1. Fit a rotated rectangle to the accepted contour. For a nearly rectangular card, it provides an initial center, two perpendicular axes, and estimated dimensions.

Do not calculate orientation from only the leftmost and rightmost contour points. A rounded corner, shadow, or isolated foreground pixel can move those extrema substantially.

For more demanding measurements, fit lines to the straight edge portions while excluding rounded corners and obvious outliers. Intersect adjacent lines to construct geometric corners. These corners represent the intersection of extrapolated straight edges, not necessarily points on the physical rounded boundary.

A minimum-area rectangle works best when the card plane is approximately parallel to the image plane. Under appreciable perspective, a rectangular card generally appears as a quadrilateral. Use four edge lines and a planar mapping when that geometry matters.

For a rotated image-space frame, define the card center as the origin. Let the unit vector along the chosen card axis be u, and let v be its perpendicular. A pixel position p then has local coordinates:

u = (cos(theta), sin(theta)) v = (-sin(theta), cos(theta)) x_card = (x - center_x) × cos(theta) + (y - center_y) × sin(theta) y_card = -(x - center_x) × sin(theta) + (y - center_y) × cos(theta)

Here, theta is measured from the image horizontal toward increasing image vertical coordinates. These local coordinates remain in pixels; rotation does not convert them into physical units.

A rectangle alone cannot distinguish one end from the other. Resolve a possible 180-degree reversal using a printed feature, asymmetric notch, or controlled loading direction. Also normalize libraries that exchange width and height while changing the reported angle.

Work through a coordinate and resolution example

Suppose the detected center is at image coordinates (1200, 800), and the chosen card axis is rotated by 10 degrees. A feature lies at (1400, 850).

The displacement from the center is:

dx = 1400 - 1200 = 200 pixels dy = 850 - 800 = 50 pixels x_card = 200 × cos(10°) + 50 × sin(10°) ≈ 206 pixels y_card = -200 × sin(10°) + 50 × cos(10°) ≈ 15 pixels

The feature is therefore about 206 pixels along the chosen card axis and 15 pixels across it. A card-relative inspection region can follow those coordinates as the card translates and rotates.

Now consider a separate acquisition-sizing example. An image width of 2400 pixels covers a 120 mm field of view:

Object-space sampling = 120 mm / 2400 pixels = 0.05 mm/pixel Pixels across a 0.5 mm feature = 0.5 mm / 0.05 mm/pixel = 10 pixels

Use the resolution calculator to explore field coverage and feature sampling.

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

Ten pixels across a feature describes sampling, not guaranteed measurement accuracy. Focus, contrast, distortion, motion, and boundary fitting all affect the result. The guide to object-space pixel size explains the distinction between pixel sampling and physical detail.

For perspective correction, define corresponding card corners in a planar coordinate system and estimate a homography. This requires a sufficiently flat card and reliable corner correspondence. Correct significant lens distortion before relying on straight-edge geometry or planar calibration.

Verify the result before using it for inspection

  1. Draw the extracted contour, fitted rectangle, center, and both axes over the original image. Use different visual styles so you can distinguish the measured silhouette from its geometric approximation.

Check these outputs across repeated captures:

  • Center repeatability: Does a stationary card appear to move between frames?
  • Angle repeatability: Does orientation jump when the card remains still?
  • Edge agreement: Do fitted edges follow the plastic rather than a shadow?
  • Frame margin: Are all physical edges visible without image clipping?
  • Axis consistency: Does the designated end remain the same after rotation?
  • Coordinate round trip: Does a local point return to its original image position after inverse transformation?

For a numerical acceptance check, calculate boundary-to-edge distances over the straight portions of the card. A low average error can hide one badly fitted side, so also inspect a high percentile or maximum after justified outlier rejection.

Test repeated images at representative positions and angles. For example, 20 captures at each of several placements can expose unstable thresholding before integration. This is a development check, not proof of performance across every production condition.

Reject invalid detections explicitly. A missing edge, implausible area, or unresolved orientation should produce a failure status rather than a plausible-looking coordinate.

Diagnose common failures and limits

If the rectangle follows a dark printed panel, the mask is segmenting artwork rather than the card. Change the background separation method or illumination before adding more geometric rules.

If the rectangle grows toward one side, inspect shadows and bright reflections. Compare the raw contour with the image before changing the rectangle-fitting algorithm.

If the angle changes abruptly by a quarter-turn, normalize the rectangle's width, height, and angle convention. If it changes by a half-turn, add an orientation feature; the silhouette may be genuinely ambiguous.

If rounded corners cause unstable line intersections, exclude a larger corner neighborhood and fit longer straight segments. Avoid claiming corner precision when only short or blurred edge sections are visible.

If the card is translucent, curled, or partly hidden, silhouette thresholding may not provide a reliable coordinate reference. A fixture, dedicated registration feature, or another imaging arrangement may be more appropriate.

When a code is present, keep its search region relative to the accepted card frame. Use QR code localization on dark backgrounds for the separate symbol-location task. A detected code does not by itself prove that the card boundary is correct.

Check requirements before ordering or deployment

Record the following before choosing hardware or releasing the inspection:

  • Required field of view, including placement variation and an edge margin.
  • Smallest feature to inspect and acceptable location error.
  • Whether acquisition occurs with the card stopped or moving.
  • Expected card colors, finishes, print layouts, and background changes.
  • Whether image-space coordinates are sufficient or calibrated physical coordinates are required.
  • Whether card orientation needs a directional reference beyond its rectangular outline.
  • Failure rules for clipped cards, merged regions, and low-confidence fits.

Keep saved examples of accepted and rejected detections, including their masks and overlays. They make later lighting or software changes easier to evaluate against the same evidence.

Start with the camera configurator once field coverage and motion conditions are defined. For help matching acquisition hardware to the required boundary repeatability, send representative images and measurement requirements through applications support.

Questions

Should I use an axis-aligned box or a rotated rectangle?

Use an axis-aligned box for a simple image crop. Use a rotated rectangle when you also need the card's approximate angle and dimensions. For tilted cards with visible perspective, four fitted edges and a planar mapping are usually more appropriate than forcing the outline into a rectangle.

Can the card outline provide coordinates without reading its code?

The outside silhouette can provide position and an axis direction, but an ordinary rectangular outline cannot identify which end is the intended top. Resolve that ambiguity with an asymmetric physical feature, a recognizable printed region, or a loading arrangement that constrains orientation.

Why does the detected card angle jump between images?

Check the segmented mask before replacing the fitting method. Reflections, shadows, clipped edges, and aggressive cleanup can move the contour between captures. Rectangle libraries can also change their angle representation when width and height exchange roles, which requires explicit normalization.

How do I convert detected card coordinates into millimeters?

Physical measurements need an appropriate calibration, not just a fitted rectangle. A fixed, nearly perpendicular setup may use a verified local scale. Perspective requires a planar mapping for a flat card, while significant lens distortion should be corrected before estimating that mapping.

What should happen when part of the card is hidden?

Do not accept a complete-card pose merely because a fitting function returns a rectangle. Hidden edges leave some geometry unsupported by image evidence. Either reject the capture or use a deliberately validated partial-visibility method with additional constraints and a clearly defined confidence check.