image_fft/
image_fft.rs

1use image::ImageFormat;
2use rapl::utils::rapl_img::open_lumaf32;
3use rapl::*;
4
5fn main() {
6    //open image as lumaf32 (gray scale) where 0.0 is white 1.0 is black.
7    let img = open_lumaf32("graphics/peppers.png").unwrap();
8    //transform to complex and take the 2D FFT
9    let fft = img.to_complex().fft2d();
10
11    //initialize kernel for convolution
12    let mut kernel: Ndarr<f32, _> = Ndarr::zeros(&fft.dim);
13    let (m, n) = (img.shape()[0], img.shape()[1]);
14    let mid_x = m / 2;
15    let mid_y = n / 2;
16    kernel[[mid_x, mid_y]] = 4.;
17    kernel[[mid_x + 1, mid_y]] = -1.;
18    kernel[[mid_x - 1, mid_y]] = -1.;
19    kernel[[mid_x, mid_y + 1]] = -1.;
20    kernel[[mid_x, mid_y - 1]] = -1.;
21    //FFT the kernell
22    let kernell = kernel.to_complex().fft2d();
23    //multiply the image FFT to the kernel fft to then do the inverse transform to ger the convolution of the
24    //image and the kernel
25    let out = (fft * kernell).ifft2().fftshif().re();
26    //save output image
27    out.save_as_luma("graphics/pepper_edges.png", ImageFormat::Png)
28}