7个版本

0.3.0 2021年12月7日
0.2.3 2021年3月20日
0.2.0 2021年2月26日
0.1.1 2021年2月10日

图像类别中排名第341

每月下载量27

MIT协议

155KB
3K SLoC

imgproc-rs

crates.io Documentation cargo-test

一个Rust图像处理库。

特性

  • 通过rayon支持某些函数的多线程(更多信息请见启用多线程
  • 某些函数支持AVX2的SIMD

支持的图像格式

imgproc-rs使用image crate中提供的i/o函数。支持的图像格式列表可在此处找到。

示例

读取和写入图像

use imgproc_rs::io;

fn main() {
    // Read an image from a path
    let img = io::read("path/to/some_image.png").unwrap();
    
    // Print image information
    println!("{:?}", img.info());
    
    // Write the image to a path as a PNG
    io::write(&img, "path/to/save_image.png").unwrap();
}

创建图像

图像可以从现有的向量、切片、向量的向量、切片的向量创建。以下是一些示例。

use imgproc_rs::image::{Image, ImageInfo};

fn main() {
    let vec = vec![1, 2, 3, 4, 5, 6,
                   7, 8, 9, 10, 11, 12];

    // Create an image from a slice
    let img_slice = Image::from_slice(2, 2, 3, false, &vec);

    // Create an image from a vector
    let img_vec = Image::from_vec(2, 2, 3, false, vec);

    // Create a blank (black) image
    let img_blank: Image<u8> = Image::blank(ImageInfo::new(2, 2, 3, false));

    // Create an empty image
    let img_empty: Image<u8> = Image::empty(ImageInfo::new(2, 2, 3, false));
}

获取图像信息

use imgproc_rs::io;
use imgproc_rs::image::{Image, ImageInfo};

fn main() {
    let img = io::read("path/to/some_image.png").unwrap();

    // Get width and height of image
    let (width, height) = img.info().wh();

    // Get width, height, and channels of image
    let (width, height, channels) = img.info().whc();

    // Get width, height, channels, and alpha of image
    let (width, height, channels, alpha) = img.info().whca();

    /* Print image information
     * Example output:
     *
     * width: 2
     * height: 2
     * channels: 3
     * alpha: false
     *
     */
    println!("{}", img.info());
}

获取/设置图像像素

可以使用一维或二维索引访问图像像素。一维索引按行从左到右读取图像数据,从图像的右上角开始。二维坐标从图像的右上角开始,向下和向右增加。

use imgproc_rs::io;
use imgproc_rs::image::{Image, BaseImage};

fn main() {
    let img = io::read("path/to/some_image.png").unwrap();
    
    // Set an image pixel using a 1D index
    img.set_pixel_indexed(0, &[1, 1, 1]);
    
    // Get an image pixel using a 1D index
    let pixel_1d = &img[0];
    
    // Set an image pixel using 2D coordinates
    img.set_pixel(1, 1, &[1, 1, 1]);
    
    // Get an image pixel using 2D coordinates
    let pixel_2d = img.get_pixel(1, 1);
}

多线程

要启用多线程,请在您的Cargo.toml中包含parallel功能

[dependencies.imgproc-rs]
version = "0.3.0"
default-features = false
features = ["parallel"]

或者,将功能标志传递给cargo run

cargo run --features parallel

支持多线程的函数

  • transform模块
    • 裁剪
    • 缩放
    • lanczos缩放
  • filter模块中的所有函数,除了以下函数:
    • 阈值
    • 残差
    • 中值滤波器
    • alpha裁剪均值滤波器

SIMD

默认情况下,通过simd功能启用SIMD支持。支持的函数只有在支持AVX2的情况下才会使用SIMD(x86/x86_64);否则将使用备用实现。

支持SIMD的函数(除simd模块外)

  • tone::brightness()
  • tone::saturation()
  • colorspace::rgb_to_grayscale()

依赖项

~14MB
~76K SLoC