> ## Content Index
> Fetch the complete content index at: https://yinguobing.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# Rust调用OpenCV
- URL: https://yinguobing.com/blog/rust-opencv/
- Published: 2022-04-18T13:31:22.000Z
- Updated: 2022-04-18T13:34:35.000Z
- Description: Rust图像处理
- Author: 尹国冰
- Tags: Rust, OpenCV, 图像处理

今天遇到一篇介绍Rust下使用OpenCV的文章，受益匪浅，推荐阅读。地址如下：

[Rust and OpenCVWe all know why Rust is so great. However, it is a little bit too new and shiny compared to the old giants such as C/C++ and we so often…![](https://cdn-static-1.medium.com/_/fp/icons/Medium-Avatar-500x500.svg)Dev GeniusJonathan Österberg![](https://miro.medium.com/max/1200/1*E811pWD9lmijiAuFKRDfrg.png)](https://blog.devgenius.io/rust-and-opencv-bb0467bf35ff?ref=yinguobing.com)

在Rust下使用OpenCV大概分以下几步：

1. 安装OpenCV
2. 在Rust项目中添加OpenCV依赖
3. 在代码中 `use opencv`

下边这段代码在Rust中使用OpenCV调用了摄像头。

```rust
use opencv::{highgui, prelude::*, videoio};
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    highgui::named_window("window", highgui::WINDOW_AUTOSIZE)?;
    let mut cam = videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
    let mut frame = Mat::default(); 
    loop {
        cam.read(&mut frame)?;
        highgui::imshow("window", &frame)?;
        let key = highgui::wait_key(1)?;
        if key == 27 {
            break;
        }
    }
    Ok(())
}

```

这里的OpenCV并非官方开发的Rust语言版本，而是Rust社区开发的对C++ API的binding。所以，编译后的程序本质上依旧是在使用C++开发的图像处理模块。如果你有C++下使用OpenCV的经验，应该还是比较好上手的。

另外如果你感兴趣，它的crate地址在这里：

[https://crates.io/crates/opencv](https://crates.io/crates/opencv?ref=yinguobing.com)