A robot vision pipeline may shift a camera image by an integer number of pixels before comparing it with another frame. The operation should define what happens to the columns exposed by the shift. On an ordinary planar image, moving content to the right exposes a region on the left for which the original image supplies no sample. NumPy's roll has a different boundary rule. Values that leave one side of the array reappear on the other.
The example below uses a two-row image of invented grayscale values and was checked with NumPy 2.5.2. The small array makes the movement of individual pixels visible. It is a test of array indexing and image validity, with no camera calibration data or measured performance.
Identify the horizontal axis
Let the first axis index image rows and the second axis index columns. A shift of one along the column axis moves each pixel one place to the right. In a circular shift, the final value in each row reappears in the first column of the same row. NumPy documents that boundary rule and also documents what happens when the axis is omitted. The default flattens the array, rolls it, and restores the original shape.
import numpy as np
image = np.array([[2, 4, 6, 8, 9],
[1, 3, 5, 7, 0]], dtype=np.uint8)
across_columns = np.roll(image, 1, axis=1)
assert across_columns.tolist() == [[9, 2, 4, 6, 8],
[0, 1, 3, 5, 7]]
flattened = np.roll(image, 1)
assert flattened.tolist() == [[0, 2, 4, 6, 8],
[9, 1, 3, 5, 7]]
The 9 moves across an image boundary in both cases. Specifying the column axis keeps it in the same row. Omitting the axis sends it into the next row because the rows are adjacent in the flattened sequence. Restoring the two-dimensional shape does nothing to repair the correspondence. A small test with distinct values on each edge exposes both behaviors.
Copy just the overlap for a planar image
For a displacement dx, output column c should read source column c minus dx. The source column has to be inside the image. One direct implementation initializes an output image, computes the matching source and destination slices, and copies the overlap. It also copies an input mask into the same destination coordinates. The mask marks which output values come from usable source pixels.
def translate_columns(image, valid, dx):
width = image.shape[1]
shifted = np.zeros_like(image)
keep = np.zeros_like(valid, dtype=bool)
if abs(dx) >= width:
return shifted, keep
dst = slice(max(dx, 0), width + min(dx, 0))
src = slice(max(-dx, 0), width - max(dx, 0))
shifted[:, dst] = image[:, src]
keep[:, dst] = valid[:, src]
return shifted, keep
valid = np.ones(image.shape, dtype=bool)
valid[1, 2] = False
shifted, keep = translate_columns(image, valid, 1)
assert shifted.tolist() == [[0, 2, 4, 6, 8],
[0, 1, 3, 5, 7]]
assert keep.tolist() == [[False, True, True, True, True],
[False, True, True, False, True]]
For a positive shift, copying begins farther to the right in the destination and at the beginning of the source. For a negative shift, copying begins at the start of the destination and farther to the right in the source. When the absolute shift reaches the image width, none of the original columns overlap the destination. The returned mask is then entirely false. A circular array would wrap at that distance and can even return values in their original order.
Interpret the mask with the pixel values
The example fills exposed pixels with zero to give the output a defined value. Zero can also be a genuine dark pixel. A feature matcher, image difference, or coverage report should use the mask to distinguish these cases. The source mask in the second row already marks one pixel unusable. Its value, 5, moves into output column three, and its invalid state moves with it. This allows an application to keep track of missing measurements even when displaying a filled image.
The code assumes a two-dimensional image and an equally shaped validity mask. A color array needs the same spatial translation applied across its color channels. Specify which array dimensions are height and width, particularly when a batch dimension or channel-first layout is present. Subpixel shifts need a sampling rule and separate handling for support near the border. The integer slices above cover an exact displacement without interpolation.
For a frame comparison, combine the translated mask with the reference frame's own validity mask before computing a score. If the overlap is empty, record that the comparison has no supported pixels. If two candidate shifts produce different overlap sizes, retain the number of compared pixels alongside the score. Otherwise a shift that leaves a small easy-to-match patch can look better than one evaluated over much more of the frame. When comparing unsigned pixel arrays, convert to a signed or floating-point type before subtraction so the residual can represent negative differences.
A periodic boundary can be appropriate for a full cylindrical panorama whose horizontal coordinates wrap at the seam. It depends on what the image represents and on which axis is periodic. A planar crop from a panorama usually loses that full-period boundary. Keep the image representation and border rule together so later steps can apply the correct geometry.
The fixture checked shifts from negative six through positive six against the source coordinate of every output pixel. It checks the validity mask as well as the sample values. Distinct rows, a valid zero, and a preexisting invalid pixel make several mistakes visible even when the output has the expected dimensions.
Sources checked November 28, 2026
NumPy, numpy.roll
https://numpy.org/doc/stable/reference/generated/numpy.roll.html
NumPy, Indexing on ndarrays
https://numpy.org/doc/stable/user/basics.indexing.html
Illustration. AI-generated concept showing an exposed edge after a planar image translation on a robot vision display.