常见重采样算法总结

Summary of Common Resample Algorithms

Aug 19, 25

一、基础

项目说明
目的对图像进行缩放、旋转、变形等操作时,重新计算像素值以获得更高质量的结果,抑制锯齿、混叠与模糊。
采样点输出像素通常对应输入图像的非整数坐标,需要插值计算。
频率响应理想重采样应为低通滤波,抑制高频混叠。实际算法在清晰度、平滑度、性能间权衡。
色彩空间建议在线性空间处理,避免 sRGB 直接插值导致色偏。

二、常见重采样算法 (插值核)

算法/类型做法优点缺点/备注
Nearest Neighbor (最近点)取最近整数坐标像素值,无插值。实现极快,代码简单明显块状、锯齿,质量最低
Bilinear (双线性)取周围 2x2 像素加权平均,线性插值。平滑、速度快边缘模糊,细节损失
Bicubic (三次插值)取 4x4 像素,用三次卷积核插值。清晰度与平滑度平衡,细节较好计算量较大,可能轻微振铃
Lanczos 重采样 (窗化 sinc)取更大邻域 (如 6x6) ,用 sinc 核窗化插值。锐利且混叠抑制优秀成本高,可能出现振铃 (ringing)
Mitchell-Netravali三次核,参数可调,常用于高质量缩放。细节与平滑度兼顾,适合内容管线算法复杂度中等
Area/Box (区域平均)输出像素对应输入区域平均,常用于缩小图像。缩小时抗混叠好,速度快放大时模糊,细节损失
Gaussian (高斯核)用高斯权重模糊后采样。平滑自然,混叠抑制好细节损失,计算较重
Catmull-Rom三次插值家族,锐度高,常用于内容管线。边缘清晰,细节好振铃风险,成本中等

在以往的工作经验中,使用非线性函数对双线性插值进行重映射 (如 smoothstep),在某些场景会有非常好的效果,详见 [[用 smoothstep 重参数化的双变量插值]]

三、特殊处理与工程建议

类型/场景说明
色彩空间建议 sRGB → 线性空间处理,插值后再转回 sRGB,避免色偏与暗化。
Alpha/透明度预乘 alpha 后插值,保证透明区域物理一致。
法线贴图反变换为单位向量,插值后归一化,避免直接 RGB 插值导致失真。
多通道/多分量各分量独立插值或用矢量插值,必要时加权 (如 Z 分量或曲率权重) 。
边界处理wrap/clamp 选择影响边缘采样,注意无缝拼接与边界像素复制。

四、算法选择建议

实时渲染 (游戏/交互)

  • 放大优先 bilinear,缩小优先 area/box
  • 重要资产可用 bicubic 或 Mitchell-Netravali
  • Lanczos 适合静态内容或高质量需求
  • 法线贴图用单位向量插值归一化

离线/内容管线

  • 推荐 Mitchell-Netravali、Lanczos
  • 严格在线性空间处理,颜色管理到位

移动/嵌入式

  • 优先 bilinear/box 以控成本
  • 必要时用 bicubic 替代高质量核

五、常见坑

  • sRGB 空间插值: 导致色偏、暗化
  • 直接插值法线或高光粗糙度贴图: 物理不一致
  • 边界 wrap/clamp 处理不当: 拼接有缝
  • 透明贴图不做预乘 alpha: 边缘异常

六、参考公式

  • Bilinear 插值: C=(1a)(1b)C00+a(1b)C10+(1a)bC01+abC11C = (1-a)(1-b)C_{00} + a(1-b)C_{10} + (1-a)bC_{01} + abC_{11}
  • Bicubic 插值: C=i=03j=03wiwjCijC = \sum_{i=0}^{3}\sum_{j=0}^{3} w_i w_j C_{ij}
  • Lanczos 核: L(x)=sinc(x)sinc(x/a)L(x) = \mathrm{sinc}(x) \cdot \mathrm{sinc}(x/a),常用 a=2,3a=2,3

1. Basics

ItemDescription
PurposeWhen scaling, rotating, or warping images, recompute pixel values for higher quality, suppressing aliasing and blurring.
Sample PointOutput pixels often map to non-integer input coordinates, requiring interpolation.
Frequency ResponseIdeal resampling is low-pass filtering to suppress high-frequency aliasing. Real algorithms balance sharpness, smoothness, and performance.
Color SpaceRecommended to process in linear space; avoid direct sRGB interpolation to prevent color shift.

2. Common Resample Algorithms (Interpolation Kernels)

Algorithm/TypeApproachAdvantagesDisadvantages/Notes
Nearest NeighborUse nearest integer pixel, no interpolation.Fastest, simplest codeBlocky, jagged, lowest quality
BilinearWeighted average of 2x2 neighbors, linear interpolation.Smooth, fastEdge blur, detail loss
Bicubic4x4 neighbors, cubic kernel interpolation.Balanced sharpness/smoothness, good detailMore computation, possible slight ringing
Lanczos (windowed sinc)Larger neighborhood (e.g. 6x6), windowed sinc kernel.Sharp, excellent anti-aliasingHigh cost, possible ringing
Mitchell-NetravaliCubic kernel, tunable parameters, used for high-quality scaling.Good detail/smoothness, suitable for content pipelineModerate complexity
Area/BoxOutput pixel is average of input region, used for downscaling.Good anti-aliasing when shrinking, fastBlurry when upscaling, detail loss
GaussianBlur with Gaussian weights before sampling.Natural smoothness, good anti-aliasingDetail loss, heavier computation
Catmull-RomCubic family, high sharpness, used in content pipelines.Sharp edges, good detailRinging risk, moderate cost

In practice, nonlinear remapping of bilinear interpolation (e.g. smoothstep) can yield excellent results in some scenarios. See [[Bivariate Interpolation with Smoothstep Reparameterization]].

3. Special Handling & Engineering Tips

Type/ScenarioDescription
Color SpacesRGB → linear for processing, interpolate then convert back to sRGB to avoid color shift/darkening.
Alpha/TransparencyPremultiply alpha before interpolation for physically correct transparency.
Normal MapsTransform to unit vector, interpolate then normalize; avoid direct RGB interpolation to prevent distortion.
Multi-channel/ComponentInterpolate channels independently or as vectors; weight if needed (e.g. Z or curvature).
Boundary Handlingwrap/clamp affects edge sampling; ensure seamless tiling and border pixel copy.

4. Algorithm Selection Advice

Real-time Rendering (Games/Interactive)

  • Prefer bilinear for upscaling, area/box for downscaling
  • Important assets: bicubic or Mitchell-Netravali
  • Lanczos for static or high-quality content
  • Normal maps: interpolate unit vectors then normalize

Offline/Content Pipeline

  • Recommend Mitchell-Netravali, Lanczos
  • Strictly process in linear space, ensure color management

Mobile/Embedded

  • Prefer bilinear/box for cost control
  • Use bicubic if high quality needed

5. Common Pitfalls

  • Interpolating in sRGB space: causes color shift/darkening
  • Directly interpolating normal/specular roughness maps: physically incorrect
  • Improper boundary wrap/clamp: visible seams
  • Not premultiplying alpha for transparency: edge artifacts

6. Reference Formulas

  • Bilinear interpolation: C=(1a)(1b)C00+a(1b)C10+(1a)bC01+abC11C = (1-a)(1-b)C_{00} + a(1-b)C_{10} + (1-a)bC_{01} + abC_{11}
  • Bicubic interpolation: C=i=03j=03wiwjCijC = \sum_{i=0}^{3}\sum_{j=0}^{3} w_i w_j C_{ij}
  • Lanczos kernel: L(x)=sinc(x)sinc(x/a)L(x) = \mathrm{sinc}(x) \cdot \mathrm{sinc}(x/a), commonly a=2,3a=2,3