A robot vision pipeline can export image values together with the coordinates at which they were sampled. The export depends on matching the coordinate arrays to the image's row and column axes. NumPy's meshgrid offers two conventions that arrange the same coordinates differently. A rectangular image can expose the difference in the output shape. A square image can conceal the mismatch until the values are associated with the wrong positions.

The example uses invented horizontal and vertical coordinates and a small array of grayscale values. It was checked with NumPy 2.5.2. The coordinates describe a synthetic sampling grid. They do not convert an image position to a physical position in a calibrated robot workspace.

Build the grid in image order

Take horizontal coordinates 10, 20, and 30, and vertical coordinates 100 and 200. An image with these samples has two rows and three columns. Access a pixel with a row index followed by a column index. Under this convention, image[1, 2] belongs to horizontal position 30 and vertical position 200. The array indices identify slots. The coordinate values are stored separately and need not start at zero.

import numpy as np

x = np.array([10, 20, 30])

y = np.array([100, 200])

image = np.array([[12, 23, 34], [45, 56, 67]])

X, Y = np.meshgrid(x, y, indexing="xy")

assert X.shape == Y.shape == image.shape == (2, 3)

assert (X[1, 2], Y[1, 2], image[1, 2]) == (30, 200, 67)

In the X array, each row repeats the horizontal coordinate vector. In the Y array, each vertical coordinate is constant across its row. Both arrays can therefore be indexed in the same order as the image. The coordinate vector can also contain nonuniform positions. The grid records how coordinates are associated with values. It does not infer a uniform spacing or resample the image.

Understand the matrix-indexed convention

With indexing="ij" and the same argument order, the grid's axes follow the vectors in the order they were supplied. Supplying x then y creates arrays of shape (3, 2), with the first index selecting x and the second selecting y. That layout is convenient for fields whose first storage axis is horizontal. The position with coordinates 30 and 200 is now stored at [2, 1].

Xi, Yi = np.meshgrid(x, y, indexing="ij")

assert Xi.shape == Yi.shape == (3, 2)

assert (Xi[2, 1], Yi[2, 1]) == (30, 200)

An image-shaped result can also be built under the matrix convention by passing the vectors in storage-axis order. Pass the vertical vector first and the horizontal vector second. Keep the returned variables in the corresponding order.

Yr, Xr = np.meshgrid(y, x, indexing="ij")

assert np.array_equal(Xr, X)

assert np.array_equal(Yr, Y)

The names for the returned coordinate arrays describe which component they hold. The axis layout describes how to find the coordinate for a particular sample. Record both in the export so another program can interpret the data without guessing.

Verify the position on a square grid

Now keep only the first two horizontal coordinates and both vertical coordinates. Each convention returns arrays with shape (2, 2). At index [0, 1], the image-order coordinates are (20, 100). With the matrix convention and the original argument order x then y, the corresponding pair is (10, 200). Both versions pass a shape check. Directly comparing coordinates at an asymmetric position reveals the swap.

Xsquare, Ysquare = np.meshgrid(x[:2], y, indexing="xy")

Xmatrix, Ymatrix = np.meshgrid(x[:2], y, indexing="ij")

assert (Xsquare[0, 1], Ysquare[0, 1]) == (20, 100)

assert (Xmatrix[0, 1], Ymatrix[0, 1]) == (10, 200)

Keep the same traversal when exporting

To write one record per pixel, flatten each coordinate array and the image using the same logical index order. In NumPy's C order, the column index varies fastest for a two-dimensional array. For the rectangular example, the first three records therefore come from the first image row and the next three from the second row.

records = np.column_stack((

X.ravel(order="C"),

Y.ravel(order="C"),

image.ravel(order="C"),

))

assert records[1].tolist() == [20, 100, 23]

assert records[5].tolist() == [30, 200, 67]

Using a shared flattening order still requires that the arrays use the same axes first. When keeping only selected pixels, apply the same selection to both coordinate components and the image. Sparse coordinate grids can be convenient for evaluating a function through broadcasting. In this example, sparse X has shape (1, 3) and sparse Y has shape (2, 1). Materialize or broadcast them to the shared image shape before flattening them into rows of a table.

For example, a function that uses both coordinates can evaluate on the sparse arrays without first filling two dense coordinate arrays. The sum below is an indexing check. A real calibration calculation would substitute the function defined by that calibration. If the final consumer needs both coordinates as separate columns, the broadcast views can supply them in the image's logical order.

Xs, Ys = np.meshgrid(x, y, indexing="xy", sparse=True)

assert Xs.shape == (1, 3) and Ys.shape == (2, 1)

assert np.array_equal(Xs + Ys, X + Y)

Retain the coordinate origin, units and direction along each axis. A vertical image coordinate can increase downward, while a Cartesian plotting view may put increasing vertical values upward. An export with a correct row-column association can still be displayed differently if an application applies a different origin. Check a non-square grid for dimension mismatches and a square grid for a silent swap. In both cases, compare known coordinates with their original image samples after export.

Sources checked November 28, 2026

NumPy, numpy.meshgrid

https://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html

NumPy, numpy.ravel

https://numpy.org/doc/stable/reference/generated/numpy.ravel.html

Illustration. AI-generated concept showing separate horizontal column and vertical row directions for a rectangular camera grid.