Border Extension for Neighborhood Operations
Handling "Undefined" Pixel Values in Image Processing Algorithms
fig. 1: 2D plot of the cosine hump function.
TODO: Add comparison image showing effects of various commonly-used kernels (Gaussian, edge enhancement, etc.) with different border extension methods.
TODO: Add image per-approach showing the resulting image after adding a "border" and applying the border extension.
"Neighborhood operations" means any process which operates on pixels surrounding a central pixel that is focused on by the algorithm.
Linear filters (also called "recursive filters") are a common class of neighborhood operation.
These replace each pixel in an image with a weighted sum of the pixels in a neighborhood around the pixel.
Method #0: Skip
This method isn't really a method for handling border pixels at all.
...
This can be combined with the approaches below as a simplification.
Rather than add special-case code to the inner loop for a neighborhood operation, the image can be padded with one of the below approaches,
- Add border around the image large enough to accommodate the support of the neighborhood operation (i.e., the size of the kernel), populate the border pixels using one of the border extension approaches below.
- Use Method #0 on the resulting enlarged image.
- Crop the image back to its original size.
The resulting algorithm is simpler than incorporating any of the approaches below directly into the inner loop.
I would have called this combo approach an "optimization" as well, except that with modern CPU architectures, adding more data for the sake of a simpler inner loop will not necessarily lead to faster code.
Method #1: Zero-Padding (Zero Extension)
.....
Pros:
Cons:
-
"Black border" artifact:
... (TODO: image).
-
-
Method #2: Symmetric Extension (Mirror Repeat)
...
In the context of the discrete cosine transform (DCT), this is known as Type-II symmetric extension.
Pros:
- Ensures continuity at image borders, unlike periodic extension.
- Theoretically, this is effectively the same as a periodic signal with twice the length of the original image.
Cons:
- Creates "phantom" features when filtering with a large kernel.
Method #3: Periodic Extension (Wrapping Repeat) (GL_REPEAT)
Pros:
TODO: Does this preserve the 3 qualities of wavelet-transformations? (find reference to verify).
- Matches the assumption of the Fourier transform that the image domain is periodic.
Cons:
- Causes noticeable filering artifacts due to artificial discontinuities introduced at image boundary.
Method #4: Border Replication (GL_CLAMP)
...
The traditional method is to "square" the corners.
Another way to go would be to "round" the corners.
The general approach is to use the P-norm.
Pros:
-
Used by Photoshop:
Their "Gaussian Blur" filter appears to use this method.
TODO: image showing different results for solid rectangle near & abutting image edge.
Image Processing Frameworks
JAI's convolution operator (ConvolveOp) has two modes of handling edge conditions:
- EDGE_ZERO_FILL: default option, fill "undefined" edge pixels with zero.
- EDGE_NO_OP: copy pixels into the , ...
Neither mode results in convolution of pixels in the region at the edge of the image.
These modes are simply provided for convenience.
JAI provides several BorderExtender sub-classes that correspond to the methods described above:
- BorderExtenderZero: fills border with zero
- BorderExtenderConstant: fills border with a constant value. For multi-band images, each band can have a different constant fill value.
- BorderExtenderCopy: fills border with copies of the edge and corners of the image.
- BorderExtenderReflect: symmetric border extension
- BorderExtenderWrap: periodic border extension
References
-
Recursive Filtering of Images with Symmetric Extension. Ben Appleton. 2005.
Studies the effects of symmetric extension on linear filters.
-
Weickert, ter Haar Romeny, Viergever. 1998.
Efficient and Reliable Schemes for Nonlinear Diffusion Filtering.
Method for recursive Gaussian filtering on an image with symmetric extension of the borders.
-
Lindeberg. 1998.
Scale-Space for Discrete Signals
When constructing a linear scale-space, symmetric extension (mirroring) is equivalent to imposing an adiabatic boundary on the related diffusion process.
This constraint has the effect of maintaining the total brightness of the image.
-
MathWorks's site has extensive documentation regarding this topic.
Linear Filtering: ...
imfilter: ...
-
Wikipedia: Neighborhood Operation.
-
Wikipedia: Discrete Cosine Transform.
Discusses border extension issues for 1D signals.