关于安装 Rust-OpenCV 【请查看】,或者查看【crates安装方法】 一、读取本地图片并显示 extern crate opencv ; use opencv::imgcodecs::imread ; use opencv::prelude::MatTrait ; use opencv::h
关于安装 Rust-OpenCV 【请查看】,或者查看【crates安装方法】
一、读取本地图片并显示
extern crate opencv;use opencv::imgcodecs::imread;
use opencv::prelude::MatTrait;
use opencv::highgui::{imshow, wait_key, destroy_all_windows};
fn main() {
let imgpath = "C:\\Users\\xxx\\Desktop\\sss.png";
let mat = imread(&imgpath,1).unwrap();
if mat.empty().unwrap(){
println!("img read error!")
}else {
imshow("show_img",&mat).expect("显示失败");
wait_key(0).expect("等待失败");
destroy_all_windows().expect("关闭窗口失败");
}
}
可能会编译报错:(exit code: 0xc0000135, STATUS_DLL_NOT_FOUND) 复制此目录下C:\tools\opencv\build\x64\vc15\bin\opencv_world412.dll库文件至当前项目文件夹下即可。
一、读取摄像头并显示
use opencv::{core,highgui,prelude::*,videoio};fn run() ->opencv::Result<()> {
let window = "video capture";
highgui::named_window(window,1)?;
let mut cam = videoio::VideoCapture::new(0,videoio::CAP_ANY)?;
let opened = videoio::VideoCapture::is_opened(&cam)?;
if !opened{
panic!("Unable to open default camera!"); // win10 在【设置】->【隐私】中打开相机的使用权限
}
loop{
let mut frame = core::Mat::default()?;
cam.read(&mut frame)?;
if frame.size()?.width>0 {
highgui::imshow(window,&mut frame)?;
}
let key = highgui::wait_key(10)?;
if key>0 && key !=255{
break;
}
}
Ok(())
}
fn main(){
run().unwrap()
}