System designTroubleshooting

Synthetic checkerboard corners detected in the wrong place

Why synthetic checkerboard corners are detected in the wrong place, the pixel offset a saved figure window adds, and the ground-truth test that catches it.

LumaSpec Vision10 min read Share on LinkedIn

On this page 8 sections
  1. 01Why are corners detected in the wrong place on a synthetic checkerboard?
  2. 02How many pixels does a saved figure window shift every corner?
  3. 03Which image should the detector actually run on?
  4. 04How do you verify detected corners against ground truth you computed yourself?
  5. 05Are the corners wrong, or just listed in a different order?
  6. 06What else shifts detected corner positions on a synthetic board?
  7. 07When is a synthetic board the wrong test, and what comes next?
  8. 08Questions

You generate a checkerboard array, run the corner detector, and every point lands off the intersections — or the calibration residuals explode for no visible reason. This page is for anyone validating a checkerboard detector or a calibration pipeline on synthetic images, in MATLAB or any other toolbox. It covers the one cause behind most of these failures — measuring corners in a PNG exported from the figure window instead of in the array itself — and the smaller causes that imitate it. You will quantify the shift in pixels, run detection on the image you actually plot against, gate every result against ground truth you computed yourself, and know when a synthetic board has told you everything it can.

Why are corners detected in the wrong place on a synthetic checkerboard?

Almost always the detector is right and the image is wrong: the corner positions were measured in a PNG exported from the figure window, that file carries a white or gray margin around the board, and every coordinate is offset from the array by exactly that margin.

The failure chain looks like this. You build the board as an array, display it with imshow, and save the window — saveas, print or a screenshot — instead of writing the array to disk. The saved PNG is a rendering of the whole window: the board plus the figure background around it. The detector then runs on imread of that file, so it measures corners in the padded image, while the plot and the comparison still use the original array. Two different images, two coordinate systems, one set of numbers applied to both.

The symptom has a fingerprint. Because the margin is constant, every detection is offset by the same vector. Corners that sit shifted by a fixed amount rather than scattered around the intersections point at this cause first. The same margin also explains why detection still succeeds: the board sits comfortably inside the padded frame. If your board is not found at all, that is a different failure, covered in checkerboard corners not detected: fix gaps in your target.

The one-line test compares sizes:

size(board)                    % the array: [800 600] for checkerboard(100,4,3)
size(imread('dataImage.png'))  % the exported window: larger, e.g. [850 900]

If the two differ, you have two images, and coordinates measured in one are wrong in the other.

How many pixels does a saved figure window shift every corner?

By the margin of the exported window, and it is the same for every corner. With an 800 × 600 board array centred in a 900 × 850 saved PNG, the shift is 150 px in x and 25 px in y: the corner that belongs at (100, 100) is found at (250, 125).

Take the board from the failing case: checkerboard(100,4,3) builds 8 × 6 squares of 100 px, an array of 800 rows and 600 columns. Its interior corners number (8 − 1) × (6 − 1) = 35. Suppose the exported window measures 900 × 850 with the image centred. The margins follow from the size difference divided by two:

col_file = col_array + (W_file − W_array) / 2 row_file = row_array + (H_file − H_array) / 2

Horizontal: (900 − 600) / 2 = 150 px. Vertical: (850 − 800) / 2 = 25 px. The first interior intersection sits 100 px in from the left and top edges — column 101, row 101 in the array's one-based indexing. Read back from the file, it is column 101 + 150 = 251 and row 101 + 25 = 126. All 35 corners carry the same (150, 25) px offset.

Two details decide how much this costs you. The formula assumes the image is centred in the window; if the export is not centred, measure the white border and add it directly. And the offset can be negative: a smaller or cropped file shifts corners the other way and can cut some off entirely. Either way, a fixed offset equal to half the size difference is the signature of an image mismatch, not detector noise.

Which image should the detector actually run on?

The array itself — pass the same numeric matrix to the detector, to the plot and to the calibration estimator, and if you need a file, write the array with imwrite and confirm the round trip with size. A figure export is a rendering, not the data.

I = checkerboard(100,4,3) > 0.5;        % logical, 800 x 600, square side 100 px
board = im2uint8(I);                    % uint8 image: detect, plot and store this
pts = detectCheckerboardPoints(board);  % pts: 35 x 2 double, [x y] in pixels
imshow(board); hold on
plot(pts(:,1), pts(:,2), 'r+', 'MarkerSize', 10);

Detection, display and calibration now share one coordinate system. Display never changes data: imshow scales pixels to the screen but leaves the array untouched, so plotting after imshow is safe. Only exporting the window creates a second image. If you need the board on disk for a record or a regression test, write the array and prove the round trip:

imwrite(board, 'board.png');                          % the array, not the window
assert(isequal(size(imread('board.png')), size(board)));

With that assertion in place, any coordinate measured in the file applies unchanged to the array.

How do you verify detected corners against ground truth you computed yourself?

Build the expected intersections from your own board definition and gate every detection against them within 1 pixel. For checkerboard(100,4,3) the 35 interior corners sit at columns 101:100:501 and rows 101:100:701, and a clean array should match all 35 inside that gate. Ground truth is free on a synthetic board: the squares are 100 px, so interior intersections sit at multiples of 100 from the edges.

[Xg, Yg] = meshgrid(101:100:501, 101:100:701);  % 7 x 5 grid of expected corners
gt = [Xg(:), Yg(:)];                            % gt: 35 x 2 double, [x y]
assert(size(pts,1) == 35, 'expected 35 interior corners');
err = zeros(size(pts,1),1);                     % err: 35 x 1 double, in px
for k = 1:size(pts,1)
    err(k) = min(sqrt(sum((gt - pts(k,:)).^2, 2)));
end
fprintf('max ground-truth error: %.2f px\n', max(err));

On the clean array this prints well under 1 px: the detector refines to subpixel positions, and what remains sits near half a pixel, from the convention difference between pixel centres and square boundaries. Run the same gate on the exported window and every point fails at once, clustered near √(150² + 25²) ≈ 152 px — the margin vector, measured.

That nearest-neighbour loop doubles as the guard against the causes below, and it is the same matching step you need before pairing image points with generated world points. For a pipeline that skips the board entirely, see camera calibration with known points: skip the checkerboard; for dots instead of squares, see MATLAB dot pattern camera calibration: detect the points yourself.

Are the corners wrong, or just listed in a different order?

If the coordinates land on intersections, the detection is right and the pairing is wrong: the detector lists points along the board as it walks it, and the first point can sit at either end. Match sets by nearest neighbour before comparing.

Nothing pins the walk to a particular starting corner: shift, rotate or mirror the board between images and the first returned point can sit at the opposite end. Two perfect detections of one board can therefore list 35 coordinates in two different orders. Compared index to index — against ground truth, or against the world points from generateCheckerboardPoints — a flawless detection still produces large residuals.

Treat the detections as a set. The loop in the previous section already does this: each detection is matched to its closest expected point, not to a fixed index. If you need the pairing for calibration, match first, then reorder. A quick visual check works too: text(pts(:,1), pts(:,2), string(1:size(pts,1))) prints the walk order onto the plot, and a reversed or flipped sequence shows up immediately.

What else shifts detected corner positions on a synthetic board?

Three smaller causes, each with a one-line test: coordinates built as [row column] against the detector's [x y] output, an imresize between detection and plotting, and JPEG compression of the test file. Check dimensions, keep the truth table in [x y], and save lossless PNG.

Swapped coordinate order. The detector returns [x y]; ground truth built as [row column] places each point wrongly, mirrored across the diagonal. The asymmetric board catches it: on an 800 × 600 array a coordinate of 701 cannot be a column, so any first value above 600 means the two lists were built in different orders.

A resize between detection and plotting. imresize scales coordinates proportionally — a 0.5 factor moves a corner at (250, 125) to (125, 62.5). Run the same size comparison as before at both ends: the array you detect on and the image you plot on must have identical dimensions. Desktop display scaling changes how pixels are drawn, not the array, so it cannot shift coordinates.

Lossy compression. A JPEG test file smooths the square edges, scattering refined corners by a fraction of a pixel and occasionally dropping weak ones. Save PNG: it is lossless, and a test built on a synthetic board has no artefacts to tolerate.

When is a synthetic board the wrong test, and what comes next?

When the question is a real lens rather than your pipeline: a synthetic board validates corner detection, ordering and plotting, but it cannot reproduce a physical lens's aberrations, sensor noise or a printed board's edge quality, so repeat the same ground-truth check on a filmed board before calibrating.

Once the 1 px gate passes on synthetic data, film a printed board with the actual camera and run the identical check on the real image. The physical target's own quality decides much of the margin you will see, and checkerboard calibration targets: image quality requirements covers how the board should be printed, matte and flat. For the choice between moving the camera and moving the board during that real calibration, see camera calibration: move the camera or the chessboard?.

Prevention checklist, in the order you will need it:

  • Run detection on the array you plot and calibrate with; export files with imwrite, never the figure window.
  • After writing any file, assert the read-back size equals the array size before using its coordinates.
  • Gate detections against computed ground truth within 1 px, matched by nearest neighbour.
  • Keep ground truth in [x y]; never mix in [row column].
  • Save test images as lossless PNG, so the file adds nothing the array does not contain.

When you move from arrays to hardware, the calibration targets page lists physical boards and depth-of-field rulers, and the area scan cameras finder filters cameras by shutter, resolution and interface. If you want help reviewing the pipeline before ordering hardware, the contact page reaches the applications team.

Questions

Why does detectCheckerboardPoints return fewer points than my board has corners?

It reports interior corners only, where four squares meet. The worked-example board — 8 × 6 squares of 100 px — yields (8 − 1) × (6 − 1) = 35 points, and the outer boundary corners are never returned. If you see fewer than 35, part of the board is being lost to a crop, a low-contrast region or a size mismatch between the two images in your pipeline, so compare the two size calls first.

Can I test a corner detector on a purely synthetic image, or do I need a camera?

Synthetic arrays are the right first test: they separate the corner pipeline from optics, so you can prove detection, ordering and plotting logic on geometry you computed yourself. They cannot test a real lens's aberrations, sensor noise, motion blur or print quality, so repeat the same ground-truth check on a filmed board before trusting any calibration result from the camera.

Does the first returned corner always sit at the same end of the board?

No. The detector walks the board as it appears in the image, and the starting point can flip when the board is shifted, rotated or mirrored between images. Two perfect detections of one board can list the same 35 coordinates in opposite orders, so match point sets by nearest neighbour, or sort both lists, before comparing them or pairing them with generated world points.

What error between detected and true corners is acceptable on a synthetic board?

Within 1 pixel on a clean binary array. The detector refines corners to subpixel positions, so residuals land well under that gate; the remaining difference near half a pixel comes from the convention between pixel centres and square boundaries. Errors above 1 px indicate a pipeline problem — two different images, swapped coordinate order or a resize — rather than detector noise.

How do I save a test image so coordinates in the file match the array?

Write the array itself with imwrite instead of saving the figure window, then read the file back and compare the two size calls: they must return identical dimensions. A PNG written from the array carries no background margin, so coordinates measured in it transfer unchanged to the array — which is what makes such a file usable as a fixed fixture in an automated regression test.