Rust在这里是全新的。我正在尝试使相机从camera_capture lib(基于rscam)中获取图像。当我尝试去做
grayscale(&frame)
它显示了这样的错误:error[E0277]: the trait bound `camera_capture::Frame: DerefMut` is not satisfied
--> src/main.rs:22:31
|
22 | let frame = grayscale(&frame);
| ^^^^^^ the trait `DerefMut` is not implemented for `camera_capture::Frame`
|
= note: required because of the requirements on the impl of `GenericImage` for `ImageBuffer<Rgb<u8>, camera_capture::Frame>`
这是完整的代码: extern crate camera_capture;
extern crate ncurses;
extern crate image;
use image::imageops::resize;
use image::imageops::colorops::grayscale;
use image::FilterType;
use image::load_from_memory;
const ASCII_GREYSCALE: &str = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'.";
fn main() {
let cam = camera_capture::create(0).unwrap();
let mut cam = cam.fps(30.0).unwrap().start().unwrap();
let window = ncurses::initscr();
let mut x: i32 = 0;
let mut y: i32 = 0;
ncurses::getmaxyx(window, &mut y, &mut x);
for frame in cam {
let frame = grayscale(&frame);
let frame = resize(&frame, x as u32, y as u32, FilterType::Nearest);
for (i, pixel) in frame.enumerate_pixels().enumerate() {
let dupa = pixel.2.data;
let value = (ASCII_GREYSCALE.len() - 1) * dupa[0] as usize/255 + 1;
let put_y = (i as i32+1)/x;
let put_x = i as i32 % x;
let ch = ASCII_GREYSCALE.chars().rev().nth(value).unwrap() as u32;
ncurses::mvaddch(put_y, put_x, ch);
}
ncurses::refresh();
}
}
有人知道如何将其转换为灰度吗?也许其他图书馆?
最佳答案
好的,我已经找到了解决方案。可以使用以下命令将其转换为普通图像:: DynamicImage子类型:
let frame = RgbImage::from_raw(frame.width(), frame.height(), frame.to_vec()).unwrap();
然后有可能:grayscale(&frame)
关于image - 如何在Rust中从摄像头捕获的图像灰度图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65809565/