university computervision week1 theory

Linear Interpolation

Source:

  • HC1a Images and Interpolation

Main formula

For :

This mixes the two neighboring samples.

Easy meaning of the weights

Let

Then , so:

If is:

  • close to , then is small, so the result is closer to
  • close to , then is large, so the result is closer to

Your confusion: why “fit a line” for each pair?

This is the important part.

Suppose you have many samples:

One single line cannot pass through all neighboring pairs unless the whole signal is already perfectly linear.

So instead we do this:

  1. On the interval , use the line through and
  2. On the interval , use a different line through and
  3. On the interval , use another line through and

So you are not fitting one global line. You are fitting one local line per neighboring pair.

That is why the result is called:

Same thing written as y = ax + b

For the interval , a line has form

We want it to satisfy:

From these two conditions:

and

So on this interval:

This is algebraically the same line as the weighted-average formula above, just written in a more annoying way.

Why linear interpolation is nicer than nearest neighbor

  • it is continuous
  • no sudden flat stair steps
  • transitions look more natural

But:

  • it is still not perfectly smooth at the sample points
  • the slope can jump at each sample location

Python

import numpy as np
 
F = np.array([2.0, 5.0, 3.0, 7.0])
 
def linear_interp(F, x):
    k = int(np.floor(x))
    k = max(0, min(k, len(F) - 2))
    t = x - k
    return (1 - t) * F[k] + t * F[k + 1]
 
for x in [0.25, 0.5, 1.25, 2.8]:
    print(x, linear_interp(F, x))

Floor Function Intuition

In linear interpolation, the floor function is used to determine which interval contains the point .

If the samples are stored at integer positions, define:

Then:

  • is the left sample index
  • is the right sample index

So if

you interpolate between and using

Why The Floor Function Helps

The floor function gives the greatest integer less than or equal to .

That means:

  • it picks the sample immediately to the left of
  • it tells you which two neighboring samples to use
  • it gives you a local coordinate:

which measures how far is from the left sample

Example

If

then

So the two neighboring samples are:

The local position is:

So the interpolated value is:

Why The Bounds Work

The defining property of the floor function is:

Since

this becomes:

If we define

then we also get:

The last inequality is true because , so it is equality.

This is the standard bound used in linear interpolation.

Keeping Indices In Bounds

If your sampled signal is

then linear interpolation needs both:

  • left index
  • right index

So must satisfy:

because then is still inside the valid array range.

A safe definition is:

followed by clamping:

Then define:

Now:

So both indices are valid.

Local Coordinate

Once is known, define:

Then the interpolation formula becomes:

This works because:

  • if , then , so the output is exactly
  • if is close to , then is close to , so the output is close to

Boundary Subtlety

If lies outside the valid image domain, clamping only is not always enough, because then

can become negative or bigger than .

So in code, a fully safe version first clamps itself.

Safe Python Version

import numpy as np
 
def linear_interp(F, x):
    N = len(F)
    x = max(0.0, min(x, N - 1))
    k = int(np.floor(x))
    k = min(k, N - 2)
    k_prime = k + 1
    t = x - k
    return (1 - t) * F[k] + t * F[k_prime]

CV101 F1 Solution

Problem CV101_F1 asks:

  • use the floor function to explicitly define integer numbers and such that

Answer

Define

and then define

Equivalently:

Why This Is Correct

The key floor-function property is:

If we set

then this becomes

Now define

Then:

and also

because , so the last inequality is equality.

Therefore the required bound holds:

Short Explanation In Words

  • gives the largest integer that is still less than or equal to
  • so it is the left integer grid point
  • the next integer to the right is
  • that is exactly the right neighbor needed for linear interpolation

Why This Theory Applies

This problem comes directly from the part of the theory where linear interpolation is defined on one interval between two consecutive samples.

That part of the theory says:

  1. choose the interval containing
  2. use the left and right sample positions of that interval
  3. mix the two sample values according to how far is between them

The floor function is what solves step 1.

It tells you which interval

contains .

Once that is known, the interpolation formula from this note applies:

So the floor function is not the interpolation itself. It is the tool that identifies the correct neighboring sample locations.

Example

If

then

and

So:

which satisfies the required inequality. x \leftarrow \max(0, \min(x, N-1))

k = \lfloor x \rfloor,\quad k=\max(0,\min(k,N-2)),\quad k’=k+1