一、基础
项目 | 说明 |
---|---|
目的 | 对图像进行缩放、旋转、变形等操作时,重新计算像素值以获得更高质量的结果,抑制锯齿、混叠与模糊。 |
采样点 | 输出像素通常对应输入图像的非整数坐标,需要插值计算。 |
频率响应 | 理想重采样应为低通滤波,抑制高频混叠。实际算法在清晰度、平滑度、性能间权衡。 |
色彩空间 | 建议在线性空间处理,避免 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 插值:
- Bicubic 插值:
- Lanczos 核: ,常用
1. Basics
Item | Description |
---|---|
Purpose | When scaling, rotating, or warping images, recompute pixel values for higher quality, suppressing aliasing and blurring. |
Sample Point | Output pixels often map to non-integer input coordinates, requiring interpolation. |
Frequency Response | Ideal resampling is low-pass filtering to suppress high-frequency aliasing. Real algorithms balance sharpness, smoothness, and performance. |
Color Space | Recommended to process in linear space; avoid direct sRGB interpolation to prevent color shift. |
2. Common Resample Algorithms (Interpolation Kernels)
Algorithm/Type | Approach | Advantages | Disadvantages/Notes |
---|---|---|---|
Nearest Neighbor | Use nearest integer pixel, no interpolation. | Fastest, simplest code | Blocky, jagged, lowest quality |
Bilinear | Weighted average of 2x2 neighbors, linear interpolation. | Smooth, fast | Edge blur, detail loss |
Bicubic | 4x4 neighbors, cubic kernel interpolation. | Balanced sharpness/smoothness, good detail | More computation, possible slight ringing |
Lanczos (windowed sinc) | Larger neighborhood (e.g. 6x6), windowed sinc kernel. | Sharp, excellent anti-aliasing | High cost, possible ringing |
Mitchell-Netravali | Cubic kernel, tunable parameters, used for high-quality scaling. | Good detail/smoothness, suitable for content pipeline | Moderate complexity |
Area/Box | Output pixel is average of input region, used for downscaling. | Good anti-aliasing when shrinking, fast | Blurry when upscaling, detail loss |
Gaussian | Blur with Gaussian weights before sampling. | Natural smoothness, good anti-aliasing | Detail loss, heavier computation |
Catmull-Rom | Cubic family, high sharpness, used in content pipelines. | Sharp edges, good detail | Ringing 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/Scenario | Description |
---|---|
Color Space | sRGB → linear for processing, interpolate then convert back to sRGB to avoid color shift/darkening. |
Alpha/Transparency | Premultiply alpha before interpolation for physically correct transparency. |
Normal Maps | Transform to unit vector, interpolate then normalize; avoid direct RGB interpolation to prevent distortion. |
Multi-channel/Component | Interpolate channels independently or as vectors; weight if needed (e.g. Z or curvature). |
Boundary Handling | wrap/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:
- Bicubic interpolation:
- Lanczos kernel: , commonly