System designHow-to

Square detection in images with contour geometry

Build square detection in images using thresholding, connected components and contour checks, with pixel budgets and tests for perspective errors.

LumaSpec Vision10 min read Share on LinkedIn

On this page 10 sections
  1. 01Define the inspection decision first
  2. 02Choose a setup that produces stable boundaries
  3. 03Budget pixels before setting geometric tolerances
  4. 04Create a binary mask without changing the shape
  5. 05Label candidates and remove obvious distractions
  6. 06Test the contour rather than its bounding box
  7. 07Separate perspective effects from part defects
  8. 08Verify acceptance and rejection independently
  9. 09Check the setup before ordering or deployment
  10. 10Questions

Detecting a square is not simply finding a region with equal width and height. Circles, merged parts and irregular blobs can pass that test, while a genuine square can fail after perspective distortion. This workflow is for inspecting isolated, approximately planar parts against a controlled background. You will build a binary mask, label candidate objects and test their actual contours, then verify the result against lighting changes, rotation and measurement uncertainty.

Define the inspection decision first

Decide whether you need to locate roughly square objects or verify manufactured square geometry. Those tasks require different acceptance limits. A loose shape detector can tolerate rounded corners; dimensional inspection needs tolerances expressed in physical units and a calibrated imaging setup.

Write down these requirements before choosing thresholds:

  • Expected side length and allowed dimensional variation.
  • Whether holes, chamfers and rounded corners are acceptable.
  • Permitted part rotation, height variation and tilt.
  • Whether neighboring parts can touch or overlap.
  • Required output: presence, center, orientation or a pass/fail decision.

Specify the inspected boundary as well. An outer silhouette can appear square even when an internal feature is missing. Outer-shape detection does not automatically verify holes, markings or surface condition.

The workflow differs from live feed circle detection: square detection needs straight sides and corner relationships rather than circular support. Finding a square-shaped region also does not decode any information printed inside it.

Choose a setup that produces stable boundaries

For opaque parts, place a backlight opposite the camera to create a silhouette. Start with backlights when the outside boundary is the inspection feature. Reflected illumination may be necessary when you cannot access the rear of the part, but surface finish then becomes another source of variation.

The LS-BKL-1468 backlight is a red flat backlight. Check its illuminated area against the complete field and expected placement variation.

BacklightsFlat Backlight, 150 x 150 mm, Red (LS-BKL-1468)LS-BKL-1468
Size
100-200 mm
Type
Standard
Voltage
24 V
$389.00View partBuild a system with it

Use the lighting finder to compare available geometries. If edge position changes as you adjust the part-to-light spacing, the distinction between collimated and diffuse backlighting becomes relevant.

Choose area scan cameras around the required pixel coverage and motion conditions. The LS-CMR-0053 camera has a rolling shutter and a C-mount. Treat it as a candidate for stationary acquisition rather than assuming it suits a moving conveyor.

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"
Interface
GigE
Resolution
12 MP
Shutter
Rolling
$560.00View partBuild a system with it

A rolling shutter exposes different image rows at different times. For moving parts, evaluate global-shutter acquisition and sufficiently short exposure, or stop the part during capture. Do not expect geometric filtering to repair motion-distorted edges.

For machine vision lenses, check mount compatibility, sensor coverage, field of view and working distance. The LS-FFL-0602 lens is a C-mount option to evaluate, not a guaranteed field-of-view match.

Machine vision lenses8.5 mm, 1", 10 MP, C-mount machine vision lens (LS-FFL-0602)LS-FFL-0602
Type
Machine vision fixed
Sensor format
1"
Mount
C-mount
Resolution
6-10 MP
$429.00View partBuild a system with it

Use the camera finder and lens finder to narrow candidates. Confirm the final optical combination using the requirements in machine vision lens selection, rather than selecting by mount alone.

Budget pixels before setting geometric tolerances

  1. Calculate how many pixels cover the smallest accepted square, including the widest required field of view.

For a perpendicular view of a flat inspection plane, estimate object-space sampling from the image width:

sampling = horizontal field of view / horizontal image pixels side pixels = physical side length / sampling relative width uncertainty ≈ 2 × edge uncertainty / side pixels

The uncertainty expression is a simple worst-case estimate. It assumes each opposing edge may shift by the stated amount; it does not include calibration error, distortion or part-height variation.

Suppose an application needs a 120 mm horizontal field across 2400 image pixels. A square has a nominal side of 40 mm:

sampling = 120 mm / 2400 pixels = 0.05 mm/pixel side pixels = 40 mm / 0.05 mm/pixel = 800 pixels width uncertainty ≈ 2 × 1 pixel = 2 pixels physical width uncertainty ≈ 2 × 0.05 mm = 0.10 mm relative uncertainty ≈ 2 / 800 = 0.25%

At only 80 pixels across the same nominal shape, that edge uncertainty becomes 2 / 80 = 2.5%. A tight aspect-ratio limit would then consume much of its allowance on image uncertainty alone.

Field of view depends on the sensor, lens and working distance, not simply the camera's megapixel rating.

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.

Use the resolution calculator to estimate the necessary pixel count, then reserve margin for positioning variation and image cropping.

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

For a fuller error budget, use choosing camera resolution for dimensional inspection. More pixels help only when the lens and illumination preserve the boundary clearly.

Create a binary mask without changing the shape

  1. Acquire a representative image, isolate the inspection region and threshold it into foreground and background.

Begin with fixed exposure, gain and light output. Check that the background is separated from the part without clipping away useful edge transitions. For a dark silhouette against a bright background, invert the threshold result if your processing library expects objects to be white.

A global threshold is a sensible starting point under uniform backlighting. If illumination varies spatially, first correct the lighting or background. Local thresholding can help with uneven backgrounds, but it can also create holes or shift boundaries differently across one part.

Apply smoothing or morphology only when you can explain the defect each operation removes. An opening can erase narrow features; a closing can join separate objects. Both can move corners and change measured side lengths.

As an initial experiment, compare processing with no morphology against a 3 × 3 neighborhood. Keep the operation only if it improves classification without hiding relevant defects.

Inspect the mask directly using a live binary image preview. A clean-looking grayscale image can still produce an unstable binary boundary.

Label candidates and remove obvious distractions

  1. Label connected foreground regions, then filter candidates using size, location and completeness.

Connected-component labeling groups neighboring foreground pixels into objects. Choose the connectivity convention deliberately: diagonal contact can merge regions under one convention but leave them separate under another.

Apply inexpensive checks before detailed contour fitting:

  • Reject components outside the expected area range.
  • Reject objects touching the inspection-region boundary unless partial objects are allowed.
  • Reject implausibly small widths or heights.
  • Flag touching parts instead of treating their combined outline as one square.
  • Preserve hole information if holes matter to acceptance.

For the earlier example, an ideal filled square occupies approximately 800 × 800 = 640,000 pixels. That provides a scale reference, not an automatic acceptance interval. Derive the interval from permitted side lengths, holes and boundary variation.

Keep component identity attached to every subsequent result. Store the label, bounding region, contour and rejection reason together. Otherwise, a valid measurement can accidentally be assigned to the wrong displayed object.

An axis-aligned bounding box is useful for cropping, but not for rotation-independent shape acceptance. A rotated square has a larger axis-aligned box even though its physical shape is unchanged.

Test the contour rather than its bounding box

  1. Extract each candidate's outer contour, simplify it to a polygon and evaluate several independent geometric checks.

A perimeter-based simplification tolerance is a practical starting point. Try a tolerance around 1–2% of contour perimeter during development, then inspect the fitted vertices. Excessive simplification can turn rounded or irregular objects into convincing quadrilaterals.

For a sharp-cornered square, require a convex polygon with four meaningful vertices. Then measure the actual polygon sides and angles. Do not measure the corners of a fitted rectangle: its angles are right angles by construction.

Useful checks include:

Check Definition What it helps reject
Side ratio Longest polygon side / shortest polygon side Elongated rectangles and unequal-sided quadrilaterals
Corner error Largest absolute difference from a right angle Rhombi and perspective-skewed shapes
Fill agreement Foreground area / minimum-area enclosing rectangle area Missing corners, concavities and irregular boundaries
Boundary residual Distance from original contour points to fitted boundary segments Curved sides, bulges and overly aggressive simplification
Convexity Consistent turn direction around ordered vertices Concave quadrilaterals

The minimum-area enclosing rectangle is rotation-independent, but its aspect ratio alone cannot establish squareness. A circle also has a square enclosing box.

For an illustrative candidate, suppose the fitted sides are 798, 804, 801 and 800 pixels. Its longest-to-shortest ratio is 804 / 798 = 1.008. If its angles range from 88.5° to 91.4°, the largest corner error is 1.5°.

Suppose its foreground area is 636,000 pixels and the enclosing rectangle measures 804 × 803 pixels:

rectangle area = 804 × 803 = 645,612 pixels fill agreement = 636,000 / 645,612 ≈ 0.985

Development limits of side ratio ≤ 1.05, corner error ≤ 5° and fill agreement ≥ 0.95 would accept those measurements. These are illustrative starting points, not universal manufacturing tolerances.

Also examine boundary residuals. A high fill score can conceal a small notch, while a rounded square may intentionally fail the four-sharp-corners requirement. If holes are permitted, distinguish outer-outline agreement from net foreground area; otherwise, a legitimate hole can cause an unintended rejection.

Separate perspective effects from part defects

  1. Verify that the image geometry supports the decision you intend to make.

A square viewed obliquely generally projects as a quadrilateral with unequal apparent sides and angles. Lens distortion can bend straight edges, especially near the image boundary. Part-height changes can alter apparent size with ordinary perspective imaging.

For coarse presence detection, a near-perpendicular camera and controlled part plane may be sufficient. For tighter acceptance, calibrate the lens and map the known inspection plane into physical coordinates before evaluating geometry.

Use an independent calibration target or known machine reference to establish the plane mapping. The approach described in camera calibration for length measurement helps separate pixel measurements from physical dimensions.

A single planar correction does not compensate for arbitrary part tilt or significant height variation. Control the presentation, measure the pose separately or use a measurement approach suited to the changing geometry.

Verify acceptance and rejection independently

  1. Test a labeled image set containing acceptable parts and specific failure cases before connecting the result to production decisions.

Include elongated rectangles, rhombi, circles, rounded squares, clipped objects, touching parts and damaged corners. Capture genuine squares at the permitted rotations and positions, including the image edges.

Change one acquisition condition at a time. If the contour shifts while the part remains stationary, investigate light output, focus and exposure before changing the geometric limits. Contour position jitter under automatic exposure explains why acquisition changes can resemble movement or dimensional variation.

Record each intermediate value, not only the final pass/fail result. A useful diagnostic record contains component area, vertex count, side ratio, corner error, fill agreement and boundary residual.

As an initial repeatability check, capture 100 stationary images and report measurement ranges. This can reveal unstable segmentation, but it does not establish the production false-accept rate. Keep a separate validation set that was not used to tune thresholds.

During integration, display the original contour, fitted polygon and rejection reason together. Confirm that the reported center and orientation refer to the intended component, especially when several objects enter the frame.

Check the setup before ordering or deployment

  1. Review the complete acquisition and acceptance chain, not just the square detector.
  • Confirm the smallest part has enough pixels for the required tolerance.
  • Check that the entire placement envelope remains inside the usable image and illuminated area.
  • Verify lens mount, sensor coverage, working distance and edge sharpness.
  • Match shutter behavior and exposure to actual part motion.
  • Test acceptable chamfers, holes and corner radii explicitly.
  • Keep incomplete and touching objects separate from ordinary shape failures.
  • Confirm calibration remains valid across permitted part heights and camera positions.
  • Save representative failures and the configuration used to classify them.

The usual silhouette approach is wrong when the desired square exists only as a printed marking, when parts overlap heavily, or when changing pose dominates the apparent geometry. Those cases need a different segmentation or pose-estimation strategy before the same contour checks become useful.

Start the camera configuration workflow once the field of view, pixel budget and motion conditions are defined. For a setup review, send the requirements and representative images through technical support, including acceptable parts and the defects the detector must reject.

Questions

Is an equal-width bounding box enough to detect a square?

No. Equal bounding-box width and height describe the enclosing box, not the object's boundary. A circle or irregular region can satisfy that condition. Test the actual contour for straight sides, suitable corner angles, side-length agreement and acceptable distance from a fitted square boundary.

How many pixels should cover the side of a square?

The required coverage depends on the smallest geometric deviation you must distinguish. With approximately one pixel of uncertainty at each opposing edge, a width measurement can vary by about two pixels. Budget that uncertainty against your tolerance, then include optical blur, calibration error and presentation variation.

Can the detector accept squares at different rotations?

Use a minimum-area enclosing rectangle and ordered contour vertices rather than axis-aligned dimensions. In-plane rotation does not change the underlying side lengths or angles. Out-of-plane tilt is different: it introduces perspective effects that require controlled presentation or an independently calibrated plane mapping.

How should rounded corners and chamfers be handled?

Define an acceptable corner radius or chamfer before selecting the fitting method. A strict four-vertex approximation may reject a legitimate rounded part. Instead, fit the straight portions of its sides and evaluate corner regions separately, retaining checks that detect bulges, missing material and excessive rounding.

Does square detection always need a backlight?

A backlight often simplifies inspection of an opaque part's outer profile, but it is not mandatory. Reflected illumination may be needed for printed shapes or inaccessible rear surfaces. Choose the arrangement that preserves the required boundary across normal changes in surface finish and part position.