Lulumoy Studio

Math guide

Interpolation and borders

Geometric transforms look up each output pixel at a position (x, y) of the original image that is almost never an integer. Interpolation estimates the value there from the neighbouring pixels.

Nearest neighbour

I(x,y)≈I(round(x),round(y))
Fast and creates no new values (useful for binary images), but produces jagged edges.

Linear (bilinear)

I(x,y)≈(1−a)(1−b)I00+a(1−b)I10+(1−a)bI01+abI11
a = x − ⌊x⌋, b = y − ⌊y⌋; I00…I11 are the four pixels around the point. It is the default: smooth and fast.

Cubic (bicubic)

w(t)={(a+2)|t|3−(a+3)|t|2+1|t|≤1a|t|3−5a|t|2+8a|t|−4a1<|t|<20en otro caso / otherwise / altrimenti,a=−0.75
Keys cubic kernel with a = −0.75 (the one OpenCV uses), applied over the 4×4 neighbouring pixels: I ≈ Σi,j w(x − xi) w(y − yj) Iij. Sharper than bilinear, though it may create small halos next to edges.

Empty areas (borders)

When the position falls outside the original image: Black uses 0; Repeat edge uses the nearest edge pixel (aaa|abcd|ddd); Mirror reflects the image at the edge (cba|abcd|dcb).

See also: Rotate, Affine transform (3 points), Perspective transform (4 points)

References

  1. OpenCV 4.x documentation — Geometric Image Transformations (warpAffine, warpPerspective, getAffineTransform, getPerspectiveTransform, getRotationMatrix2D, interpolation flags). docs.opencv.org/4.x/da/d54/group__imgproc__transform.html
  2. R. Keys. "Cubic convolution interpolation for digital image processing." IEEE Transactions on Acoustics, Speech, and Signal Processing, 29(6):1153–1160, 1981.
  3. R. C. Gonzalez, R. E. Woods. Digital Image Processing, 4th ed. Pearson, 2018.