freqshow 0.3.1

A simple tool to visualize the frequency spectrum of an image using Rust.
Documentation

freqshow

Crates.io docs.rs CI Benchmark License

A Rust library for converting images to and from the 2D frequency domain via FFT.

Usage

Add to your Cargo.toml:

[dependencies]
freqshow = "0.1.0"

Example

use freqshow::FreqImage;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load any image (color images are converted to grayscale automatically).
    let mut fi = FreqImage::open("photo.jpg")?;

    // Forward FFT.
    fi.fft_forward();

    // Center DC and apply a low-pass filter.
    let mut centered = fi.fftshift();
    let mask = centered.low_pass_mask(0.10, 0.02);
    centered.apply_filter(&mask);

    // Inverse FFT and save the result.
    let mut result = centered.ifftshift();
    result.fft_inverse();
    result.to_image().save("filtered.png")?;

    Ok(())
}

API overview

Method Description
FreqImage::open(path) Load an image from disk (returns Result)
FreqImage::from_image(img) Convert a DynamicImage to a complex buffer
fi.fft_forward() 2D forward FFT in-place
fi.fft_inverse() 2D inverse FFT in-place (normalized)
fi.fftshift() / fi.ifftshift() Center / uncenter the DC component
fi.to_image() Convert back to a GrayImage
fi.view_fft_norm() Log-scale magnitude visualization
fi.low_pass_mask(cutoff, smoothing) Generate a low-pass filter mask
fi.high_pass_mask(cutoff, smoothing) Generate a high-pass filter mask
fi.band_pass_mask(low, high, smoothing) Generate a band-pass filter mask
fi.apply_filter(mask) Apply a filter mask in-place

Running the example

cargo run --example freq_out -- data/mandrill.jpg

This produces spectrum.png, low_pass.png, and high_pass.png in the working directory.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.