提问人:Antodyn 提问时间:10/17/2023 更新时间:10/17/2023 访问量:31
如何使用 rust opencv 将完整的参数传递给 set_mouse_callback
How to use rust opencv to pass a complete parameter to set_mouse_callback
问:
我正在尝试用 Rust 的绑定 opencv 做一些事情,总体思路是根据鼠标按下和释放在相应位置捕获图像块。我主要参考 Python 代码,其中一种是 cv.line
by cv.rectangle
的替代方案,以满足我的需求。但是当我再次尝试用 Rust 编写时,我遇到了一些问题。我的代码如下:
use opencv::{highgui, imgcodecs, Result};
struct Point {
x: i32,
y: i32,
}
impl Point {
fn new(x: i32, y: i32) -> Self {
Self { x, y }
}
}
fn main() -> Result<()> {
let img_path = "./1.jpg";
let img = imgcodecs::imread(img_path, imgcodecs::IMREAD_COLOR)?;
highgui::named_window("image", 0)?;
highgui::set_window_property(
"image",
highgui::WINDOW_NORMAL,
highgui::WND_PROP_VISIBLE.into(),
)?;
// To store mouse positions when click and release
let mut pos: [Point; 2];
highgui::set_mouse_callback(
"image",
Some(Box::new(
|event: i32, x: i32, y: i32, _flags: i32| match event {
highgui::EVENT_LBUTTONDOWN => pos = [Point::new(x, y),Point::new(x, y)], // borrowed value does not live long enough
highgui::EVENT_LBUTTONUP => pos[1] = todo!(),
_ => (),
},
)),
)?;
let mut key: i32;
loop {
highgui::imshow("image", &img)?;
key = highgui::wait_key(1)?;
if key == b'q'.into() {
println!("Exit successfully.");
break;
}
}
Ok(())
}
我不确定我是否正确地编写了set_mouse_callback
,因为我当前的编写方式阻止了我将变量移动到闭包中。我想这是因为我在闭包内传递参数时遇到了问题。我是 Rust 的新手,很想和你谈谈。谢谢。pos
闭合的生存期不够长。我想修改 within 的值,以便以后可以使用该变量。pos
pos
pos
答: 暂无答案
评论