System designHow-to

Find 4 corner points for a perspective transform of a card

Find the 4 corner points with approxPolyDP, order them clockwise from top-left, then rectify the card with a perspective transform sized to its aspect ratio.

LumaSpec Vision9 min read Share on LinkedIn

On this page 10 sections
  1. 01What you need
  2. 02Step 1: Threshold and find the card contour
  3. 03Step 2: Approximate the contour to four corners
  4. 04Step 3: Order the corners clockwise from the top-left
  5. 05Step 4: Size the destination points to the real aspect ratio
  6. 06Step 5: Refine the corners, then verify the warp
  7. 07Common mistakes and their fixes
  8. 08When four contour corners are the wrong tool
  9. 09Ship-it checklist
  10. 10Questions

You have a card photographed off-axis — an ID card, a game card, a flat label on a tilted panel — and you need its four corner points in pixels. A perspective transform then flattens it into a front-facing, measurable image. The pipeline is short: threshold, find the contour, approximate it down to four vertices, order them clockwise from the top-left, then warp. What breaks it in practice is the approximation tolerance, the corner ordering and the destination aspect ratio. This guide walks the five steps with worked numbers for an 85.60 × 53.98 mm ISO ID-1 card, and ends with the checks that catch a bad warp before it reaches your line.

What you need

Three inputs and five documented OpenCV calls.

  • Contrast on all four edges. The contour method needs the card silhouette to separate from the background; on a laminated card, glare blobs break the outline, so light diffusely or polarize.
  • Enough pixels. A 1 px corner error is 1/s mm at an imaging scale of s px/mm. For a 0.25 mm corner tolerance, work at 4 px/mm — about 342 px across the 85.60 mm card width. Size your own sensor with the resolution calculator below.
  • OpenCV-Python 4.x, where cv2.findContours returns two values. The older three-value unpack is the error most teams hit first; the findContours unpacking fix covers it.
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

The five steps:

  1. Threshold and find the card contour.
  2. Approximate it to four corners.
  3. Order the corners clockwise from the top-left.
  4. Set destination points to the card's true aspect ratio.
  5. Warp, then verify numerically.

On a line you run this loop on the device itself, with no PC: smart cameras execute the threshold-to-warp script on board. The LS-SMC-0063 pairs a 1.6 MP sensor and a 16 mm lens with GigE output.

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

Tighter tolerances need more pixels on the short edge: the MV-SC3050XC-16M-ATN-H captures 5 MP color images with a global shutter over Fast Ethernet.

Smart cameras & vision sensorsHikrobot MV-SC3050XC-16M-ATN-H 5 MP color vision sensorMV-SC3050XC-16M-ATN-H
Interface
Fast Ethernet
Resolution
5 MP
Sensor format
1/1.45"
Shutter
Global
$1,809.00View part

Step 1: Threshold and find the card contour

Threshold the frame, then keep only contours whose area falls inside a window computed from your optics — never a floor copied from a tutorial.

import cv2
import numpy as np

img = cv2.imread("card.jpg")                  # BGR, uint8
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

Otsu picks the split from the histogram, which survives lighting drift better than a fixed level such as 130. If the contour stage needs a refresher, find rectangle edges and corners in an image walks it end to end.

Worked example, used throughout this guide: a 5 MP camera frames the card region over a 200 mm wide field at 2592 × 1944 px, so the scale is s = 2592 / 200 = 12.96, call it 13 px/mm. The ID-1 card is 85.60 × 53.98 mm; photographed tilted 30° about its long edge, the height foreshortens by cos 30° = 0.866.

  • Imaged width: 85.60 mm × 13 px/mm ≈ 1113 px
  • Imaged height: 53.98 mm × 13 px/mm × 0.866 ≈ 608 px
  • Expected contour area: 1113 px × 608 px ≈ 6.8 × 10⁵ px²

Accept 0.5 A to 1.5 A around that expectation — 3.4 × 10⁵ to 1.0 × 10⁶ px² here. A flat 10,000 px² floor also admits a 100 × 100 px glare blob at this scale.

lo, hi = 3.4e5, 1.0e6                     # px^2, from the window above
card = max((c for c in contours if lo < cv2.contourArea(c) < hi),
           key=cv2.contourArea)           # an empty window means: fix contrast

cv2.contourArea returns the enclosed silhouette area in px² as a float.

Step 2: Approximate the contour to four corners

cv2.approxPolyDP collapses the contour to a polygon, and you need exactly four vertices. Set epsilon as a fraction of the arc length and start small:

perimeter = cv2.arcLength(card, True)     # float, closed curve
approx = None
for frac in (0.01, 0.02, 0.03, 0.05):     # fractions of the perimeter
    approx = cv2.approxPolyDP(card, frac * perimeter, True)
    if len(approx) == 4:
        break

approxPolyDP guarantees each output vertex stays within epsilon of the original contour, so epsilon is also the worst-case corner error. With a perimeter of 2 × (1113 + 608) = 3442 px, 2% gives epsilon ≈ 69 px; 10% — a fraction widely copied between tutorials — gives ≈ 344 px, about 26 mm of possible corner shift at 13 px/mm. On a clean, high-contrast card, 1-2% returns four vertices at once. If you need more than 5% to get there, the contour itself is broken — glare, shadow, a corner fading into the background — and a bigger epsilon hides the problem instead of fixing it. Repair the lighting or the threshold first.

Step 3: Order the corners clockwise from the top-left

approxPolyDP returns vertices in contour walk order, which is arbitrary. getPerspectiveTransform pairs points by index, so both lists must share one order: top-left, top-right, bottom-right, bottom-left. The standard test uses coordinate sums and differences:

pts = approx.reshape(4, 2).astype(np.float32)   # (4, 2) float32
s = pts.sum(axis=1)                             # x + y
d = pts[:, 1] - pts[:, 0]                       # y - x
tl, br = pts[np.argmin(s)], pts[np.argmax(s)]   # from the sums
tr, bl = pts[np.argmin(d)], pts[np.argmax(d)]   # from the differences
src = np.array([tl, tr, br, bl], dtype=np.float32)

The top-left corner minimises x + y and the bottom-right maximises it; top-right minimises y − x and bottom-left maximises it. The test holds while the card stays within ±45° of upright; past that, sort by angle around the quad centroid instead — the FAQ below has the recipe.

Step 4: Size the destination points to the real aspect ratio

The rectified output is only as honest as its destination rectangle. ID-1 measures 85.60 × 53.98 mm, an aspect ratio of 85.60 / 53.98 = 1.586. At 4 px/mm the output is 342 × 216 px. A 600 × 300 destination — 2:1, another much-copied value — stretches the card horizontally by 2.0 / 1.586 = 1.26, a 26% distortion.

CARD_W_MM, CARD_H_MM = 85.60, 53.98       # ISO ID-1 card, mm
PX_PER_MM = 4.0
out_w = round(CARD_W_MM * PX_PER_MM)      # 342
out_h = round(CARD_H_MM * PX_PER_MM)      # 216
dst = np.array([[0, 0], [out_w - 1, 0],
                [out_w - 1, out_h - 1],
                [0, out_h - 1]], dtype=np.float32)  # tl, tr, br, bl

M = cv2.getPerspectiveTransform(src, dst)           # 3x3 matrix
rectified = cv2.warpPerspective(img, M, (out_w, out_h))

Keep dst in the same tl, tr, br, bl order as src, or the warp mirrors the card. If the printed size is unknown, take the width from the longer of the top and bottom edges and the height from the longer of the left and right pair. Four known points carry more than rectification: the same homography recovers a plane's pose in ground plane pose from one image using four known points.

Step 5: Refine the corners, then verify the warp

Contour vertices are integer pixels. cv2.cornerSubPix re-locks them to the gray-level corner before you build M:

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.01)
src = cv2.cornerSubPix(gray, src.reshape(4, 1, 2),
                       (11, 11), (-1, -1), criteria).reshape(4, 2)

Then verify numerically before anything downstream sees the image:

  1. Re-project the source points with cv2.perspectiveTransform(src.reshape(1, 4, 2), M); they must land on dst within 1 px. The matrix is exact at the corners by construction, so a miss means the two point lists got crossed.
  2. Measure the rectified card. The edges should read 342 × 216 px, a ratio of 1.583 against the 1.586 specification — the 0.2% gap is whole-pixel rounding.
  3. Sweep 20 images with the card at different positions and tilts. Corners that hold at the centre but drift near the frame edges point to lens distortion: calibrate and cv2.undistort before the contour stage. Matching the distortion model covers the classic failure there.

For measurement-grade corners — dimension checks, robot pick offsets — subpixel edge work is its own discipline; extract subpixel edge contour points and fit an edge line goes deeper.

Common mistakes and their fixes

Symptom Cause Fix
Fewer than four vertices at low epsilon Broken contour: glare, shadow, low contrast Diffuse or polarized light; Otsu threshold; raise epsilon last
Rectified card stretched Destination aspect 2:1 against a real 1.586 Build dst from the card's true size
Card mirrored or upside down dst not in src's tl, tr, br, bl order Pair the lists by index; check dst[0] is the top-left
Corner wanders a few px between frames Integer vertices, threshold flicker cv2.cornerSubPix on the grayscale frame; lock exposure and gain
Warp drifts near the frame edges Lens distortion Calibrate and undistort before the contour stage
Wrong blob wins the area filter Fixed floor such as 10,000 px² Window of 0.5-1.5 × the expected area

When four contour corners are the wrong tool

Contour corners fail in three predictable ways. A card rounded everywhere, with no straight run between corners, gives vertices that ride the curves and walk between frames — use a trained locator instead. A background the same color as the card leaves no contour at any threshold: change the lighting geometry, a backlight or dark field, not the code. A round part has no corners at all, and a Hough circle search fits it better — live feed circle detection shows the pattern. And when the corners are only the first step toward a robot move, remember the warp returns image coordinates; the camera-to-cell transform comes from camera to robot transform calibration.

Ship-it checklist

  • Contrast on all four edges at the worst-case tilt, before any code runs.
  • Area window of 0.5-1.5 × the expected area, derived from field of view and card size.
  • The epsilon loop lands on four vertices between 1% and 5% across 20 trial images.
  • src and dst both read tl, tr, br, bl, and the output honors the 1.586 aspect (342 × 216 px at 4 px/mm).
  • Reprojection error under 1 px, corner wander within 1 px across 20 frames.
  • Exposure short enough that the card moves under 1 px while the shutter is open.

If you are sizing hardware for this job, the area scan cameras finder filters by resolution and shutter, or open the camera configurator and step through sensor, lens and light in one pass. More inspection logic like this sits on the systems topic page, and the contact page is the way to a fit check on your own parts.

Questions

How do I order the corners when the card is rotated more than 45 degrees?

The sum-and-difference test assumes the top-left point is still the upper-left one. Past about 45° of in-plane rotation it picks the wrong vertex. Instead, compute each point's angle with atan2 about the quad centroid, sort ascending, then roll the list so it starts at the point with the smallest x + y. The same ordering works on the rectangle cv2.boxPoints returns from cv2.minAreaRect.

Why does my rectified card come out mirrored or upside down?

getPerspectiveTransform pairs source and destination points strictly by index. If the destination list starts somewhere other than the top-left while the source list starts there, the homography folds the image over. Build dst as [[0,0],[w-1,0],[w-1,h-1],[0,h-1]] so it reads tl, tr, br, bl exactly like src, and the warp comes out upright.

Can getPerspectiveTransform work with more than four point pairs?

No. It solves the eight-degree-of-freedom homography from exactly four source and four destination points, both float32. When the points come from feature matches and may contain outliers, switch to cv2.findHomography with the RANSAC flag on five or more pairs: it returns the same 3 × 3 matrix while discarding bad matches.

How many pixels do I need for a 0.5 mm corner tolerance?

A 1 px corner error equals 1/s mm at a scale of s px/mm, so 0.5 mm needs s of at least 2 px/mm. Because threshold noise costs another pixel or two, size for 4-8 px/mm: that is 342-685 px across the 85.60 mm card width, with the card filling a useful share of the frame.

Does this pipeline survive a moving conveyor?

Yes, if the shutter freezes the motion. Keep exposure short enough that the card travels under one pixel while the frame integrates, or blurred edges widen and pull each corner estimate. At 300 mm/s belt speed and 13 px/mm, one pixel is 1/13 ≈ 0.077 mm, so exposure stays below 0.077 / 300 s ≈ 0.26 ms. Trigger from a sensor at a fixed belt position for repeatable framing.