Image Borders
- Updated2025-11-25
- 3 minute(s) read
Many image processing functions process a pixel by using the values of its neighbors. A neighbor is a pixel whose value affects the value of a nearby pixel when an image is processed. Pixels along the edge of an image do not have neighbors on all four sides. If you need to use a function that processes pixels based on the value of their neighboring pixels, specify an image border that surrounds the image to account for these outlying pixels. You define the image border by specifying a border size and the values of the border pixels.
The size of the border should accommodate the largest pixel neighborhood required by the function you are using. The size of the neighborhood is specified by the size of a 2D array. For example, if a function uses the eight adjoining neighbors of a pixel for processing, the size of the neighborhood is 3 × 3, indicating an array with three columns and three rows. Set the border size to be greater than or equal to half the number of rows or columns of the 2D array rounded down to the nearest integer value. For example, if a function uses a 3 × 3 neighborhood, the image should have a border size of at least 1; if a function uses a 5 × 5 neighborhood, the image should have a border size of at least 2. In Vision, an image is created with a default border size of 3, which can support any function using up to a 7 × 7 neighborhood without any modification.
Vision provides three ways to specify the pixel values of the image border. The following figure illustrates these options. Figure A shows the pixel values of an image. By default, all image border pixels are uninitialized. You can set all of the border pixels to have a value of 0, as shown in figure B. You can copy the values of the pixels along the edge of the image into the border pixels, as shown in figure C, or you can mirror the pixel values along the edge of the image into the border pixels, as shown in figure D.

The method you use to fill the border pixels depends on the processing function you require for your application. Review how the function works before choosing a border-filling method because your choice can drastically affect the processing results. For example, if you are using a function that detects edges in an image based on the difference between a pixel and its neighbors, do not set the border pixel values to zero. As shown in figure B, an image border containing zero values introduces significant differences between the pixel values in the border and the image pixels along the border, which causes the function to detect erroneous edges along the border of the image. If you are using an edge detection function, copy or mirror the pixel values along the border into the border region to obtain more accurate results.