gpt4 book ai didi

image - 如何将 DynamicImage 转换为 ImageBuffer?

转载 作者:行者123 更新时间:2023-12-03 11:42:29 26 4
gpt4 key购买 nike

当我运行以下代码时,最后一行会导致此错误:

error[E0308]: mismatched types
--> src\main.rs:25:13
|
25 | box_filter(&img, 5, 5);
| ^^^^ expected struct `ImageBuffer`, found enum `DynamicImage`
|
= note: expected reference `&ImageBuffer<Luma<u8>, Vec<u8>>`
found reference `&DynamicImage`
extern crate image;
extern crate imageproc;

use image::GenericImageView;
use imageproc::filter::box_filter;

fn main() {
println!("Hello, world!");

// Use the open function to load an image from a Path.
// `open` returns a `DynamicImage` on success.
// `unwrap` returns a `panic` when it receives a `None`.
let img = image::open(
"C:/Users/hp/Desktop/Multiprocessor real-time scheduling/Project2/data/aLIEz.jpg",
)
.unwrap();

// The dimensions method returns the images width and height.
println!("dimensions {:?}", img.dimensions());

// The color method returns the image's `ColorType`.
println!("{:?}", img.color());

// Write the contents of this image to the Writer in PNG format.
img.save(
"C:/Users/hp/Desktop/Multiprocessor real-time scheduling/Project2/data_output/test.png",
)
.unwrap();

box_filter(&img, 5, 5);
}
我不知道如何转换 DynamicImage进入 ImageBuffer这样我就可以应用过滤器。我看过 image 0.23.12 的文档依赖,但我不明白如何做到这一点。

最佳答案

  • image::open().unwrap() 结果为 image::DynamicImage
  • imageproc::filter::box_filter() 需要 image::GrayImage
  • 转换 image::DynamicImage变成 image::GrayImage使用
    to_luma8()
    方法

  • 这个更新的示例应该可以工作:
    extern crate image;
    extern crate imageproc;
    use image::GenericImageView;
    use imageproc::filter::box_filter;

    fn main() {
    let dynamic_image = image::open("path/here").unwrap();
    let gray_image = dynamic_image.to_luma8();
    let result = box_filter(&gray_image, 5, 5);
    }
    如果您需要 image::RgbImage image::RgbaImage 然后调用 into_rgb8() into_rgba8() .然而 box_filter()仅支持 image::GrayImage .

    关于image - 如何将 DynamicImage 转换为 ImageBuffer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65240917/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com