TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Bilinear Interpolation

Computer Vision
Medium

Resize a two-dimensional grid with corner-aligned bilinear interpolation. Map each output coordinate to the source grid:

y=iH−1Hnew−1y=i\frac{H-1}{H_{\mathrm{new}}-1}y=iHnew​−1H−1​ x=jW−1Wnew−1x=j\frac{W-1}{W_{\mathrm{new}}-1}x=jWnew​−1W−1​

Use source coordinate zero when the corresponding output dimension is one. Let y0=⌊y⌋y_0=\lfloor y\rfloory0​=⌊y⌋, x0=⌊x⌋x_0=\lfloor x\rfloorx0​=⌊x⌋, dy=y−y0d_y=y-y_0dy​=y−y0​, and dx=x−x0d_x=x-x_0dx​=x−x0​. Clamp y1=y0+1y_1=y_0+1y1​=y0​+1 and x1=x0+1x_1=x_0+1x1​=x0​+1 to the source boundary. Then compute:

V0=Iy0,x0(1−dx)+Iy0,x1dxV_0=I_{y_0,x_0}(1-d_x)+I_{y_0,x_1}d_xV0​=Iy0​,x0​​(1−dx​)+Iy0​,x1​​dx​ V1=Iy1,x0(1−dx)+Iy1,x1dxV_1=I_{y_1,x_0}(1-d_x)+I_{y_1,x_1}d_xV1​=Iy1​,x0​​(1−dx​)+Iy1​,x1​​dx​ Oi,j=V0(1−dy)+V1dyO_{i,j}=V_0(1-d_y)+V_1d_yOi,j​=V0​(1−dy​)+V1​dy​

Return the resized two-dimensional list.

Loading visualization...

Examples

Input: image = [[0, 10], [20, 30]], new_h = 3, new_w = 3

Output: [[0, 5, 10], [10, 15, 20], [20, 25, 30]]

Explanation: Corners remain fixed and every middle value is the linear blend of neighboring pixels.

Input: image = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]], new_h = 2, new_w = 2

Output: [[0, 3], [12, 15]]

Hint 1

Compute source coordinates before taking their floor and fractional parts.

Hint 2

Interpolate horizontally twice, then interpolate those two values vertically.

Requirements

  • Use corner-aligned source coordinates
  • Blend the four neighboring source values
  • Handle output height or width equal to one
  • Return a two-dimensional list with the requested dimensions

Constraints

  • The image is a nonempty rectangular list
  • New height and width are positive integers
Try Similar Problems
Image Rotation NearestConv2d Image FilteringGaussian Blur KernelLinear InterpolationHistogram Equalization

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
PrevNext

Bilinear Interpolation

Computer Vision
Medium

Resize a two-dimensional grid with corner-aligned bilinear interpolation. Map each output coordinate to the source grid:

y=iH−1Hnew−1y=i\frac{H-1}{H_{\mathrm{new}}-1}y=iHnew​−1H−1​ x=jW−1Wnew−1x=j\frac{W-1}{W_{\mathrm{new}}-1}x=jWnew​−1W−1​

Use source coordinate zero when the corresponding output dimension is one. Let y0=⌊y⌋y_0=\lfloor y\rfloory0​=⌊y⌋, x0=⌊x⌋x_0=\lfloor x\rfloorx0​=⌊x⌋, dy=y−y0d_y=y-y_0dy​=y−y0​, and dx=x−x0d_x=x-x_0dx​=x−x0​. Clamp y1=y0+1y_1=y_0+1y1​=y0​+1 and x1=x0+1x_1=x_0+1x1​=x0​+1 to the source boundary. Then compute:

V0=Iy0,x0(1−dx)+Iy0,x1dxV_0=I_{y_0,x_0}(1-d_x)+I_{y_0,x_1}d_xV0​=Iy0​,x0​​(1−dx​)+Iy0​,x1​​dx​ V1=Iy1,x0(1−dx)+Iy1,x1dxV_1=I_{y_1,x_0}(1-d_x)+I_{y_1,x_1}d_xV1​=Iy1​,x0​​(1−dx​)+Iy1​,x1​​dx​ Oi,j=V0(1−dy)+V1dyO_{i,j}=V_0(1-d_y)+V_1d_yOi,j​=V0​(1−dy​)+V1​dy​

Return the resized two-dimensional list.

Loading visualization...

Examples

Input: image = [[0, 10], [20, 30]], new_h = 3, new_w = 3

Output: [[0, 5, 10], [10, 15, 20], [20, 25, 30]]

Explanation: Corners remain fixed and every middle value is the linear blend of neighboring pixels.

Input: image = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]], new_h = 2, new_w = 2

Output: [[0, 3], [12, 15]]

Hint 1

Compute source coordinates before taking their floor and fractional parts.

Hint 2

Interpolate horizontally twice, then interpolate those two values vertically.

Requirements

  • Use corner-aligned source coordinates
  • Blend the four neighboring source values
  • Handle output height or width equal to one
  • Return a two-dimensional list with the requested dimensions

Constraints

  • The image is a nonempty rectangular list
  • New height and width are positive integers
Try Similar Problems
Image Rotation NearestConv2d Image FilteringGaussian Blur KernelLinear InterpolationHistogram Equalization

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

Accepts: number

You must run your code first.