使用C和Zig实现的快速图像与视频保真度指标
摘要
fmetrics是一个使用C和Zig开发的开源库,用于计算快速的感知图像和视频保真度指标,如IW-SSIM、MS-SSIM、SSIMULACRA2、Butteraugli和CVVDP,并且经过验证与人类评分具有相关性。
查看缓存全文
缓存时间: 2026/07/10 06:07
halidecx/fmetrics
来源:https://github.com/halidecx/fmetrics
fmetrics
使用 C 和 Zig 实现的快速图像与视频保真度指标。
使用方法
编译需要 Zig (https://ziglang.org/) ≥0.16.0 以及 macOS、Linux 或类 Unix 操作系统。
运行以下命令进行编译:
zig build --release=fast
可添加 -Dflto=true 启用链接时优化(FLTO),添加 -Dstrip=true 去除二进制文件中的调试信息。
编译后生成的文件结构如下:
tree zig-out
├── bin
│ ├── fmetrics
├── include
│ └── fmetrics.h
└── lib
└── libfmetrics.a
fmetrics 二进制文件的使用方法:
fmetrics by Halide Compression, LLC | [version]
usage: fmetrics [options] compare two images/videos using various perceptual quality metrics
metrics: iwssim, msssim, ssimu2, butter, cvvdp
run `fmetrics --help` for metric-specific help
options:
-h, --help show this help message
sRGB PNG, PNM/PAM, QOI, or Y4M input expected
不同指标的使用方式各异;部分指标支持通过 --err-map 输出视觉误差图,部分指标支持额外的配置选项。所有指标的输入输出方式相同,由 simpleimgio (https://github.com/gianni-rosato/simpleimgio) 提供支持。
库的使用
libfmetrics.a 提供了声明在 fmetrics.h 中的 C 语言 API。若要将其作为 Zig 的依赖使用,请在 build.zig.zon 中添加以下内容,并运行:
zig fetch --save git+https://github.com/halidecx/fmetrics.git
之后 build.zig.zon 中会显示类似如下的内容:
.dependencies = .{
.fmetrics = .{
.url = "git+https://github.com/halidecx/fmetrics.git#",
.hash = "fmetrics--",
},
},
接着你可以在 build.zig 中链接该依赖:
const fmetrics_dep = b.dependency("fmetrics", .{
.target = target,
.optimize = optimize,
});
const fmetrics = fmetrics_dep.artifact("fmetrics");
exe.root_module.linkLibrary(fmetrics);
exe.root_module.addIncludePath(fmetrics.getEmittedIncludeTree());
Zig 项目也可以直接导入原生的 Zig API:
const fmetrics_dep = b.dependency("fmetrics", .{
.target = target,
.optimize = optimize,
});
const fmetrics = fmetrics_dep.module("fmetrics");
exe.root_module.addImport("fmetrics", fmetrics);
exe.root_module.linkLibrary(fmetrics_dep.artifact("libfmetrics"));
const fmetrics = @import("fmetrics");
const reference = try fmetrics.Image.init(reference_rgb, width, height);
const distorted = try fmetrics.Image.init(distorted_rgb, width, height);
const score = try fmetrics.msssim(reference, distorted);
完整的 Zig API 请参见 src/fmetrics.zig。C 项目可使用 fmetrics.h。
参考对比
参考实现测试来源包括:
- Butteraugli:libjxl (https://github.com/libjxl/libjxl) 中的
butteraugli_main - CVVDP:我们的 fcvvdp (https://github.com/halidecx/fcvvdp)
- IW-SSIM:Python IW-SSIM (https://github.com/Jack-guo-xy/Python-IW-SSIM) 的一个分支 (https://github.com/gianni-rosato/Python-IW-SSIM)
- MS-SSIM:通过
ffmpeg调用 libvmaf (https://github.com/netflix/vmaf) 的 MS-SSIM 滤波器 - SSIMULACRA2:Cloudinary 的
ssimulacra2(https://github.com/cloudinary/ssimulacra2)
MOS 相关性
MOS 相关性衡量的是指标与主观人眼评分的吻合程度。使用 mos-correlation (https://github.com/gianni-rosato/mos-correlation) 中的 mos.py 在 CID22 数据集上进行测试。对我们而言,这些测试并不旨在评判不同指标的优劣,而是评估我们的实现相对于各自参考的准确性。此处仅报告 Spearman 秩相关系数(SRCC),数值越高越好。
| 指标 | 参考实现 SRCC | fmetrics SRCC | 差异 (%) |
|---|---|---|---|
| butteraugli (p3 i203)* | 0.7929 | 0.7863 | -0.83% |
| fcvvdp** | 0.8274 | 0.8286 | +0.15% |
| iw_ssim | n/a | 0.7925 | +0.00% |
| ms_ssim | 0.7845 | 0.8048 | +2.59% |
| ssimulacra2 | 0.8916 | 0.8910 | -0.07% |
*注意:由于 Butteraugli 是数值越小越好的指标,上述 SRCC 的符号已经翻转。 **注意:fmetrics 使用 fcvvdp 库(作为 Zig 模块),仅输入输出不同,因此底层指标实现相同。
速度与内存使用
测试环境:标准 Core i7-13700k,使用 3840x2160 的源图像和失真图像(PAM 格式)(Google Drive 链接 (https://drive.google.com/drive/folders/1Kxzmw-jMWtbh8elPuQidY5MM1pTXkF0L?usp=sharing),无损 JPEG-XL 源文件;运行 djxl <*.pam.jxl> <*.pam> 解压缩)。
速度(毫秒)
| 指标 | 参考实现 (ms) | fmetrics (ms) | 差异 (%) |
|---|---|---|---|
| butteraugli (p3 i203) | 4110 | 3130 | 快 31.31% |
| fcvvdp* | 1390 | 1390 | 0.00% |
| iw_ssim | 3020 | 228 | 快 1224.6% |
| ms_ssim** | 1110 | 106 | 快 947.2% |
| ssimulacra2 | 722 | 232 | 快 211.2% |
内存使用(MB)
| 指标 | 参考实现 (MB) | fmetrics (MB) | 差异 (%) |
|---|---|---|---|
| butteraugli (p3 i203) | 2440 | 1670 | -31.56% |
| fcvvdp* | 1600 | 1600 | 0.00% |
| iw_ssim | 2660 | 551 | -79.29% |
| ms_ssim** | 841 | 376 | -55.29% |
| ssimulacra2 | 1370 | 741 | -45.91% |
*注意:fmetrics 使用 fcvvdp 库(作为 Zig 模块),仅输入输出不同,因此底层指标实现相同。 **注意:MS-SSIM 的比较并不完全公平,因为 libvmaf 在滤波器链中同时计算了其他指标。
致谢
fmetrics 遵循 Apache 2.0 许可证。
fmetrics 由 Halide Compression (https://halide.cx) 开发。
特别感谢 Vship (https://codeberg.org/Line-fr/Vship),它的部分设计启发了 fmetrics。Vship 遵循 MIT NON-AI 许可证 (https://codeberg.org/Line-fr/Vship/src/branch/main/LICENSE)。
相似文章
Fc:一种针对浮点数流的无损压缩器
fc 是一款开源的 IEEE-754 64 位双精度浮点数流无损压缩器,对于结构化数据,其压缩率优于 zstd 和 fpzip,但编码速度较慢。
Perceptual Image Codec: 实际学习型图像压缩中的关键因素
PICO (Perceptual Image Codec) 是苹果公司推出的一种新型学习型编解码器,针对人类视觉系统进行了优化,相比AV1和VVC等传统编解码器可节省2.3–3倍的比特率,同时在iPhone 17 Pro Max上实现230毫秒编码/150毫秒解码。
LiteFrame 扩展视频大语言模型效率(6分钟阅读)
LiteFrame 为视频大语言模型引入了一种高效的视频编码器,采用压缩令牌蒸馏技术,在保持准确率的同时,能够处理多达8倍的帧数并降低35%的延迟,为长视频理解开创了新的帕累托前沿。
SwiftI2V:一种通过条件分段生成实现高效高分辨率图像到视频生成的框架
SwiftI2V 是一个新颖的高效框架,用于高分辨率图像到视频的生成,它采用条件分段生成技术,在显著降低计算成本的同时实现了 2K 分辨率的合成。该框架使得在单个消费级或数据中心 GPU 上进行实用的生成成为可能,同时保持了输入的保真度。
LiteFrame: 高效视觉编码器解锁视频大语言模型的帧缩放
LiteFrame提出了一种轻量级视频编码器,采用压缩令牌蒸馏(Compressed Token Distillation)训练,可降低延迟,并使视频大语言模型能够处理8倍以上的帧数以实现长视频理解,在降低计算量的同时提高准确性。