2 个版本
0.1.1 | 2023年4月29日 |
---|---|
0.1.0 | 2021年12月9日 |
#808 在 算法
137 每月下载量
用于 butter2d
39KB
429 行
图像的二维傅里叶变换
该包旨在提供易于使用的图像二维频谱变换,如傅里叶(FFT)和余弦(DCT)变换。默认情况下,只有傅里叶变换可用。余弦变换可通过名为 rustdct
的功能使用,该功能对应于相应的可选依赖项。
默认情况下,只有在此包的 fft2d::slice
模块中的可变切片上的实现可用,内部实现。如果激活了 nalgebra
功能,fft2d::nalgebra
模块中也提供了变换的实现,用于 nalgebra 矩阵。
以下是 low_pass
过滤器示例的部分代码摘录,其中包含输入和输出图像,以便了解 API。
// Compute the 2D fft of the complex image data (beware of the transposition).
fft_2d(width, height, &mut img_buffer);
// Shift opposite quadrants of the fft (like matlab fftshift).
img_buffer = fftshift(height, width, &img_buffer);
// Apply a low-pass filter.
let low_pass = low_pass_filter(height, width);
let fft_low_pass: Vec<Complex<f64>> =
low_pass.iter().zip(&img_buffer)
.map(|(l, b)| l * b).collect();
// Invert the FFT back to the spatial domain of the image.
img_buffer = ifftshift(height, width, &fft_low_pass);
ifft_2d(height, width, &mut img_buffer);
// Normalize the data after FFT and IFFT.
let fft_coef = 1.0 / (width * height) as f64;
for x in img_buffer.iter_mut() { *x *= fft_coef; }
示例
有一些示例演示了如何使用 API,位于 examples/
文件夹中。
通过傅里叶变换进行低通滤波
此示例演示了如何在图像上应用低通滤波器。它既适用于 examples/low_pass.rs
中的可变切片 API,也适用于 examples/low_pass_nalgebra.rs
中的 nalgebra API。示例结果在上面的说明文档中的图中可见。您可以使用以下命令编译和运行示例
cargo run --release --example low_pass
通过二维余弦变换进行图像缩放
此示例演示了如何使用二维余弦变换来缩放图像。它包括执行 DCT,然后截断(或扩展)图像缓冲区,并执行逆 DCT。您可以使用以下命令编译和运行示例
cargo run --release --features rustdct --example dct_resize
正常积分以生成深度图(或 3D 网格)
在这个例子中,我们从一张普通图开始,普通图是一种将表面法线的(x,y,z)分量编码到图像的RGB分量的图像,我们将普通图整合以获得包含每个像素估计深度Z的深度图。整合这些法线的一个算法是在基于2D DCT的Poisson求解器(如Poisson方程)中。相关的代码位于dct_poisson
函数中的examples/normal_integration.rs
文件。您可以使用以下命令编译和运行示例:
cargo run --release --features rustdct,nalgebra --example normal_integration
本例中使用的猫普通图来自哈佛的光度立体数据集。
法线整合的代码是以下论文中方法的Rust实现。非常感谢Yvain Quéau (@yqueau) 在设置他的Matlab代码移植中的帮助。
法线整合:综述 - Queau等人,2017
依赖项
~3–4MB
~78K SLoC