make tests return result and replace unwrap() with ?
MODIFY
tests/boxed.rs
tests/boxed.rs
use std::mem::transmute;
-use opencv::core::{DataType, Mat, Scalar};
+use opencv::{
+ core::{DataType, Mat, Scalar},
+ Result,
+};
#[test]
-fn layout() {
- let mat = Mat::new_rows_cols_with_default(1, 3, f32::typ(), Scalar::all(10.)).unwrap();
+fn layout() -> Result<()> {
+ let mat = Mat::new_rows_cols_with_default(1, 3, f32::typ(), Scalar::all(10.))?;
let mat_ref: &mut Mat = unsafe { transmute(&mut mat.as_raw_Mat()) };
- assert_eq!(mat.size().unwrap(), mat_ref.size().unwrap());
- assert_eq!(mat.typ().unwrap(), mat_ref.typ().unwrap());
- assert_eq!(mat.rows().unwrap(), mat_ref.rows().unwrap());
- assert_eq!(mat.cols().unwrap(), mat_ref.cols().unwrap());
- assert_eq!(mat.at_2d::<f32>(0, 1).unwrap(), mat_ref.at_2d::<f32>(0, 1).unwrap());
+ assert_eq!(mat.size()?, mat_ref.size()?);
+ assert_eq!(mat.typ()?, mat_ref.typ()?);
+ assert_eq!(mat.rows()?, mat_ref.rows()?);
+ assert_eq!(mat.cols()?, mat_ref.cols()?);
+ assert_eq!(mat.at_2d::<f32>(0, 1)?, mat_ref.at_2d::<f32>(0, 1)?);
+ Ok(())
}

MODIFY
tests/callback.rs
tests/callback.rs
use std::sync::{Arc, Mutex};
-use opencv::highgui;
+use opencv::{highgui, Result};
#[test]
-fn callback() {
+fn callback() -> Result<()> {
// only run under X11 if on linux
if cfg!(target_os = "linux") && option_env!("DISPLAY").is_none() {
- return;
+ return Ok(());
}
- highgui::named_window("test", 0).unwrap();
+ highgui::named_window("test", 0)?;
let mut value = 50;
let cb_value = Arc::new(Mutex::new(0));
highgui::create_trackbar("test_track", "test", &mut value, 100, Some(Box::new({
@@ -16,9 +16,10 @@ fn callback() {
move |s| {
*cb_value.lock().unwrap() = s;
}
- }))).unwrap();
- highgui::set_trackbar_pos("test_track", "test", 10).unwrap();
+ })))?;
+ highgui::set_trackbar_pos("test_track", "test", 10)?;
assert_eq!(value, 10);
assert_eq!(*cb_value.lock().unwrap(), 10);
+ Ok(())
}

MODIFY
tests/core.rs
tests/core.rs
-use opencv::core::{self, CV_32S, CV_32SC3, CV_64F, CV_64FC1, CV_8U, CV_8UC2, MAKETYPE, Moments};
+use opencv::{
+ core::{self, CV_32S, CV_32SC3, CV_64F, CV_64FC1, CV_8U, CV_8UC2, MAKETYPE, Moments},
+ Result,
+};
#[test]
fn make_type() {
@@ -8,15 +11,17 @@ fn make_type() {
}
#[test]
-fn moments() {
- let moments = Moments::default().unwrap();
+fn moments() -> Result<()> {
+ let moments = Moments::default()?;
assert_eq!(0., moments.m00);
assert_eq!(0., moments.m12);
assert_eq!(0., moments.mu30);
+ Ok(())
}
#[test]
-fn cpu_features_line() {
- let cpu_feats = core::get_cpu_features_line().unwrap();
+fn cpu_features_line() -> Result<()> {
+ let cpu_feats = core::get_cpu_features_line()?;
assert!(cpu_feats.is_ascii());
+ Ok(())
}

MODIFY
tests/dnn.rs
tests/dnn.rs
-use opencv::dnn::{Net, LayerParams};
+use opencv::{
+ dnn::{LayerParams, Net},
+ Result,
+};
#[test]
-fn net() {
- let mut net = Net::new().unwrap();
- assert!(net.empty().unwrap());
- net.enable_fusion(false).unwrap();
- let mut params = LayerParams::new().unwrap();
- assert_eq!(params.name().unwrap(), "");
- assert_eq!(params._type().unwrap(), "");
- params.set_name("param name").unwrap();
- params.set_type("param type").unwrap();
- assert_eq!(params.name().unwrap(), "param name");
- assert_eq!(params._type().unwrap(), "param type");
- let blobs = params.blobs().unwrap();
+fn net() -> Result<()> {
+ let mut net = Net::new()?;
+ assert!(net.empty()?);
+ net.enable_fusion(false)?;
+ let mut params = LayerParams::new()?;
+ assert_eq!(params.name()?, "");
+ assert_eq!(params._type()?, "");
+ params.set_name("param name")?;
+ params.set_type("param type")?;
+ assert_eq!(params.name()?, "param name");
+ assert_eq!(params._type()?, "param type");
+ let blobs = params.blobs()?;
assert_eq!(0, blobs.len());
- let res = net.add_layer("layer", "type", &mut params).unwrap();
+ let res = net.add_layer("layer", "type", &mut params)?;
assert_ne!(-1, res);
- assert!(!net.empty().unwrap());
+ assert!(!net.empty()?);
+ Ok(())
}

MODIFY
tests/features2d.rs
tests/features2d.rs
@@ -4,18 +4,20 @@ use opencv::{
core::Mat,
features2d::{Feature2D, ORB},
imgcodecs,
+ Result,
types::{PtrOfORB, VectorOfKeyPoint},
};
use opencv::core::Size;
#[test]
-fn orb() {
+fn orb() -> Result<()> {
let blox_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/blox.jpg");
- let img = imgcodecs::imread(blox_path.to_str().unwrap(), imgcodecs::IMREAD_COLOR).unwrap();
- let mut orb: PtrOfORB = ORB::default().unwrap();
+ let img = imgcodecs::imread(blox_path.to_str().unwrap(), imgcodecs::IMREAD_COLOR)?;
+ let mut orb: PtrOfORB = ORB::default()?;
let mut kp = VectorOfKeyPoint::new();
- let mut des = Mat::default();
- orb.detect_and_compute(&img, &Mat::default(), &mut kp, &mut des, false).unwrap();
+ let mut des = Mat::new()?;
+ orb.detect_and_compute(&img, &Mat::new()?, &mut kp, &mut des, false)?;
assert_eq!(290, kp.len());
- assert_eq!(Size::new(32, 290), des.size().unwrap());
+ assert_eq!(Size::new(32, 290), des.size()?);
+ Ok(())
}

MODIFY
tests/get_tick_count.rs
tests/get_tick_count.rs
-use opencv::core;
+use opencv::{core, Result};
#[test]
-fn test_primitives() {
- assert!(core::get_tick_count().unwrap() > 10000);
- assert!(core::get_cpu_tick_count().unwrap() > 10000);
- assert!(core::get_tick_frequency().unwrap() > 1000.);
- assert!(core::get_number_of_cpus().unwrap() >= 1);
- core::set_use_optimized(true).unwrap();
- assert!(core::use_optimized().unwrap());
- core::set_use_optimized(false).unwrap();
- assert!(!core::use_optimized().unwrap());
+fn test_primitives() -> Result<()> {
+ assert!(core::get_tick_count()? > 10000);
+ assert!(core::get_cpu_tick_count()? > 10000);
+ assert!(core::get_tick_frequency()? > 1000.);
+ assert!(core::get_number_of_cpus()? >= 1);
+ core::set_use_optimized(true)?;
+ assert!(core::use_optimized()?);
+ core::set_use_optimized(false)?;
+ assert!(!core::use_optimized()?);
+ Ok(())
}
#[test]
-fn test_return_string() {
- let info = core::get_build_information().unwrap();
+fn test_return_string() -> Result<()> {
+ let info = core::get_build_information()?;
assert!(info.contains("\nGeneral configuration for OpenCV"));
+ Ok(())
}

MODIFY
tests/imgcodecs.rs
tests/imgcodecs.rs
use std::mem::transmute;
use opencv::{
- core::{self, Mat, Size, Vec3b, DataType},
+ core::{self, DataType, Mat, Size, Vec3b},
imgcodecs,
+ Result,
};
const PIXEL: &[u8] = include_bytes!("pixel.png");
#[test]
-fn decode() {
+fn decode() -> Result<()> {
{
- let src = Mat::from_slice::<u8>(PIXEL).unwrap();
- let dest = imgcodecs::imdecode(&src, imgcodecs::IMREAD_COLOR).unwrap();
- assert_eq!(dest.size().unwrap(), Size::new(1, 1));
- assert_eq!(dest.channels().unwrap(), 3);
- assert_eq!(*dest.at_2d::<Vec3b>(0, 0).unwrap(), Vec3b::from([56u8, 56, 191]));
+ let src = Mat::from_slice::<u8>(PIXEL)?;
+ let dest = imgcodecs::imdecode(&src, imgcodecs::IMREAD_COLOR)?;
+ assert_eq!(dest.size()?, Size::new(1, 1));
+ assert_eq!(dest.channels()?, 3);
+ assert_eq!(*dest.at_2d::<Vec3b>(0, 0)?, Vec3b::from([56u8, 56, 191]));
}
{
let mut bytes = PIXEL.to_vec();
- let src = Mat::new_rows_cols_with_data(1, PIXEL.len() as _, u8::typ(), unsafe { transmute(bytes.as_mut_ptr()) }, core::Mat_AUTO_STEP).unwrap();
- let mut dest = Mat::default();
- imgcodecs::imdecode_to(&src, imgcodecs::IMREAD_COLOR, &mut dest).unwrap();
- assert_eq!(dest.size().unwrap(), Size::new(1, 1));
- assert_eq!(dest.channels().unwrap(), 3);
- assert_eq!(*dest.at_2d::<Vec3b>(0, 0).unwrap(), Vec3b::from([56u8, 56, 191]));
+ let src = Mat::new_rows_cols_with_data(1, PIXEL.len() as _, u8::typ(), unsafe { transmute(bytes.as_mut_ptr()) }, core::Mat_AUTO_STEP)?;
+ let mut dest = Mat::new()?;
+ imgcodecs::imdecode_to(&src, imgcodecs::IMREAD_COLOR, &mut dest)?;
+ assert_eq!(dest.size()?, Size::new(1, 1));
+ assert_eq!(dest.channels()?, 3);
+ assert_eq!(*dest.at_2d::<Vec3b>(0, 0)?, Vec3b::from([56u8, 56, 191]));
}
+
+ Ok(())
}

MODIFY
tests/imgproc.rs
tests/imgproc.rs
use opencv::{
core::{DataType, Mat, Point, Point2f, Scalar, Size, Vec2f},
imgproc,
+ Result,
types::VectorOfPoint,
};
#[test]
-fn min_enclosing() {
- let mut pts = Mat::new_rows_cols_with_default(1, 2, Vec2f::typ(), Scalar::default()).unwrap();
- let points = pts.at_row_mut::<Vec2f>(0).unwrap();
+fn min_enclosing() -> Result<()> {
+ let mut pts = Mat::new_rows_cols_with_default(1, 2, Vec2f::typ(), Scalar::default())?;
+ let points = pts.at_row_mut::<Vec2f>(0)?;
points[0].copy_from_slice(&[10., 10.]);
points[1].copy_from_slice(&[20., 10.]);
let mut center = Point2f::default();
let mut radius = 0.;
- imgproc::min_enclosing_circle(&pts, &mut center, &mut radius).unwrap();
+ imgproc::min_enclosing_circle(&pts, &mut center, &mut radius)?;
assert_eq!(radius, 5.0001);
- assert_eq!(center, Point2f::new(15., 10.))
+ assert_eq!(center, Point2f::new(15., 10.));
+ Ok(())
}
#[test]
-fn ellipse() {
+fn ellipse() -> Result<()> {
let mut pts = VectorOfPoint::new();
- imgproc::ellipse_2_poly(Point::new(100, 100), Size::new(200, 200), 0, 45, 90, 10, &mut pts).unwrap();
+ imgproc::ellipse_2_poly(Point::new(100, 100), Size::new(200, 200), 0, 45, 90, 10, &mut pts)?;
assert_eq!(6, pts.len());
assert_eq!(Point::new(241, 241), *pts.get(0));
assert_eq!(Point::new(100, 300), *pts.get(5));
+ Ok(())
}

MODIFY
tests/mat.rs
tests/mat.rs
use std::mem::transmute;
-use opencv::core::{self, DataType, Mat, Rect, Scalar, Size, Vec2b, Vec3d, Vec3f};
-use opencv::types::{VectorOfint, VectorOfMat};
+use common::*;
+use opencv::{
+ core::{self, DataType, Mat, Rect, Scalar, Size, Vec2b, Vec3d, Vec3f},
+ Result,
+ types::{VectorOfint, VectorOfMat},
+};
mod common;
-use common::*;
const PIXEL: &[u8] = include_bytes!("pixel.png");
#[test]
-fn mat_for_rows_and_cols() {
+fn mat_for_rows_and_cols() -> Result<()> {
let typ = Vec3d::typ();
- let mat = unsafe { Mat::new_rows_cols(400, 300, typ) }.unwrap();
- assert_eq!(mat.typ().unwrap(), typ);
- assert!(mat.is_continuous().unwrap());
- assert!(!mat.is_submatrix().unwrap());
- assert_eq!(mat.size().unwrap(), Size::new(300, 400));
- assert_eq!(mat.rows().unwrap(), 400);
- assert_eq!(mat.cols().unwrap(), 300);
- assert_eq!(Vec3d::depth(), mat.depth().unwrap());
- assert_eq!(2, mat.dims().unwrap());
- assert_eq!(3, mat.channels().unwrap());
- assert_eq!(24, mat.elem_size().unwrap());
- assert_eq!(8, mat.elem_size1().unwrap());
- assert_eq!(120000, mat.total().unwrap());
+ let mat = unsafe { Mat::new_rows_cols(400, 300, typ) }?;
+ assert_eq!(mat.typ()?, typ);
+ assert!(mat.is_continuous()?);
+ assert!(!mat.is_submatrix()?);
+ assert_eq!(mat.size()?, Size::new(300, 400));
+ assert_eq!(mat.rows()?, 400);
+ assert_eq!(mat.cols()?, 300);
+ assert_eq!(Vec3d::depth(), mat.depth()?);
+ assert_eq!(2, mat.dims()?);
+ assert_eq!(3, mat.channels()?);
+ assert_eq!(24, mat.elem_size()?);
+ assert_eq!(8, mat.elem_size1()?);
+ assert_eq!(120000, mat.total()?);
+ Ok(())
}
#[test]
-fn mat_at_1d_refers_to_rows() {
+fn mat_at_1d_refers_to_rows() -> Result<()> {
let mut mat =
- Mat::new_rows_cols_with_default(100, 100, f32::typ(), Scalar::all(1.23)).unwrap();
- *mat.at_mut::<f32>(5).unwrap() = 1.;
- assert_eq!(*mat.at_2d::<f32>(5, 0).unwrap(), 1.);
+ Mat::new_rows_cols_with_default(100, 100, f32::typ(), Scalar::all(1.23))?;
+ *mat.at_mut::<f32>(5)? = 1.;
+ assert_eq!(*mat.at_2d::<f32>(5, 0)?, 1.);
+ Ok(())
}
#[test]
-fn mat_2d_i0_is_rows_i1_is_cols() {
+fn mat_2d_i0_is_rows_i1_is_cols() -> Result<()> {
// Just a sanity check about which Mat dimension corresponds to which in Size
let mat =
- Mat::new_rows_cols_with_default(5, 6, f32::typ(), Scalar::all(1.23)).unwrap();
- let size = mat.size().unwrap();
+ Mat::new_rows_cols_with_default(5, 6, f32::typ(), Scalar::all(1.23))?;
+ let size = mat.size()?;
assert_eq!(size.width, 6);
assert_eq!(size.height, 5);
+ Ok(())
}
#[test]
-fn mat_at_2d() {
- let mut mat = Mat::new_rows_cols_with_default(100, 100, f32::typ(), Scalar::all(1.23)).unwrap();
- assert_eq!(*mat.at_2d::<f32>(0, 0).unwrap(), 1.23);
- *mat.at_2d_mut::<f32>(0, 0).unwrap() = 1.;
- assert_eq!(*mat.at_2d::<f32>(0, 0).unwrap(), 1.);
+fn mat_at_2d() -> Result<()> {
+ let mut mat = Mat::new_rows_cols_with_default(100, 100, f32::typ(), Scalar::all(1.23))?;
+ assert_eq!(*mat.at_2d::<f32>(0, 0)?, 1.23);
+ *mat.at_2d_mut::<f32>(0, 0)? = 1.;
+ assert_eq!(*mat.at_2d::<f32>(0, 0)?, 1.);
assert_is_err(mat.at::<i32>(0));
assert_is_err(mat.at::<f32>(100));
+ Ok(())
}
#[test]
-fn mat_at_2d_multichannel() {
- let mut mat = Mat::new_rows_cols_with_default(100, 100, Vec3f::typ(), Scalar::all(1.23)).unwrap();
- let pix = *mat.at_2d::<Vec3f>(0, 0).unwrap();
+fn mat_at_2d_multichannel() -> Result<()> {
+ let mut mat = Mat::new_rows_cols_with_default(100, 100, Vec3f::typ(), Scalar::all(1.23))?;
+ let pix = *mat.at_2d::<Vec3f>(0, 0)?;
assert_eq!(pix[0], 1.23);
assert_eq!(pix[1], 1.23);
assert_eq!(pix[2], 1.23);
- *mat.at_2d_mut::<Vec3f>(0, 0).unwrap() = Vec3f::from([1.1, 2.2, 3.3]);
+ *mat.at_2d_mut::<Vec3f>(0, 0)? = Vec3f::from([1.1, 2.2, 3.3]);
- let pix = *mat.at_2d::<Vec3f>(0, 0).unwrap();
+ let pix = *mat.at_2d::<Vec3f>(0, 0)?;
assert_eq!(pix[0], 1.1);
assert_eq!(pix[1], 2.2);
assert_eq!(pix[2], 3.3);
@@ -72,20 +79,21 @@ fn mat_at_2d_multichannel() {
assert_is_err(mat.at_2d::<i32>(0, 0));
assert_is_err(mat.at_2d::<Vec3f>(100, 1));
assert_is_err(mat.at_2d::<Vec3f>(1, 100));
+ Ok(())
}
#[test]
-fn mat_at_row() {
- let mut mat = Mat::new_rows_cols_with_default(100, 100, f32::typ(), Scalar::all(1.23)).unwrap();
+fn mat_at_row() -> Result<()> {
+ let mut mat = Mat::new_rows_cols_with_default(100, 100, f32::typ(), Scalar::all(1.23))?;
- let row = mat.at_row::<f32>(0).unwrap();
+ let row = mat.at_row::<f32>(0)?;
assert_eq!(row.len(), 100);
assert_eq!(row[0], 1.23);
- let row = mat.at_row_mut::<f32>(1).unwrap();
+ let row = mat.at_row_mut::<f32>(1)?;
row[0..4].copy_from_slice(&[10., 20., 30., 40.]);
- let data = mat.data_typed::<f32>().unwrap();
+ let data = mat.data_typed::<f32>()?;
assert_eq!(data[0], 1.23);
assert_eq!(data[100], 10.);
assert_eq!(data[101], 20.);
@@ -96,10 +104,11 @@ fn mat_at_row() {
assert_is_err(mat.at_row::<i32>(100));
assert_is_err(mat.at_row_mut::<i32>(0));
assert_is_err(mat.at_row_mut::<i32>(100));
+ Ok(())
}
#[test]
-fn mat_vec() {
+fn mat_vec() -> Result<()> {
{
let s = vec![
vec![1.0f32, 2., 3.],
@@ -107,11 +116,11 @@ fn mat_vec() {
vec![7., 8., 9.],
];
- let mat = Mat::from_slice_2d(&s).unwrap();
- assert_eq!(mat.size().unwrap(), core::Size { width: 3, height: 3 });
- assert_eq!(*mat.at_2d::<f32>(1, 1).unwrap(), 5.);
+ let mat = Mat::from_slice_2d(&s)?;
+ assert_eq!(mat.size()?, core::Size { width: 3, height: 3 });
+ assert_eq!(*mat.at_2d::<f32>(1, 1)?, 5.);
- let v = mat.to_vec_2d::<f32>().unwrap();
+ let v = mat.to_vec_2d::<f32>()?;
assert_eq!(s, v);
}
@@ -120,111 +129,117 @@ fn mat_vec() {
dims.push(3);
dims.push(3);
dims.push(3);
- let mut mat = Mat::new_nd_with_default(&dims, f64::typ(), Scalar::all(2.)).unwrap();
- *mat.at_3d_mut::<f64>(1, 1, 1).unwrap() = 10.;
- assert_eq!(3, mat.dims().unwrap());
+ let mut mat = Mat::new_nd_with_default(&dims, f64::typ(), Scalar::all(2.))?;
+ *mat.at_3d_mut::<f64>(1, 1, 1)? = 10.;
+ assert_eq!(3, mat.dims()?);
if let Ok(..) = mat.to_vec_2d::<f64>() {
assert!(false, "dims too high");
}
}
+
+ Ok(())
}
#[test]
-fn mat_continous() {
+fn mat_continous() -> Result<()> {
let s = vec![
vec![1.0f32, 2., 3.],
vec![4., 5., 6.],
vec![7., 8., 9.],
];
- let mat = Mat::from_slice_2d(&s).unwrap();
+ let mat = Mat::from_slice_2d(&s)?;
{
- let sub_mat_non_cont = Mat::roi(&mat, Rect::new(1, 1, 2, 2)).unwrap();
- assert_eq!(mat.typ().unwrap(), sub_mat_non_cont.typ().unwrap());
- assert_eq!(2, sub_mat_non_cont.rows().unwrap());
- assert_eq!(2, sub_mat_non_cont.cols().unwrap());
- assert!(sub_mat_non_cont.is_submatrix().unwrap());
- assert!(!sub_mat_non_cont.is_continuous().unwrap());
- assert_eq!(5., *sub_mat_non_cont.at_2d::<f32>(0, 0).unwrap());
- assert_eq!(6., *sub_mat_non_cont.at_2d::<f32>(0, 1).unwrap());
- assert_eq!(8., *sub_mat_non_cont.at_2d::<f32>(1, 0).unwrap());
- assert_eq!(9., *sub_mat_non_cont.at_2d::<f32>(1, 1).unwrap());
-
- let vec = sub_mat_non_cont.to_vec_2d::<f32>().unwrap();
+ let sub_mat_non_cont = Mat::roi(&mat, Rect::new(1, 1, 2, 2))?;
+ assert_eq!(mat.typ()?, sub_mat_non_cont.typ()?);
+ assert_eq!(2, sub_mat_non_cont.rows()?);
+ assert_eq!(2, sub_mat_non_cont.cols()?);
+ assert!(sub_mat_non_cont.is_submatrix()?);
+ assert!(!sub_mat_non_cont.is_continuous()?);
+ assert_eq!(5., *sub_mat_non_cont.at_2d::<f32>(0, 0)?);
+ assert_eq!(6., *sub_mat_non_cont.at_2d::<f32>(0, 1)?);
+ assert_eq!(8., *sub_mat_non_cont.at_2d::<f32>(1, 0)?);
+ assert_eq!(9., *sub_mat_non_cont.at_2d::<f32>(1, 1)?);
+
+ let vec = sub_mat_non_cont.to_vec_2d::<f32>()?;
assert_eq!(5., vec[0][0]);
assert_eq!(6., vec[0][1]);
assert_eq!(8., vec[1][0]);
assert_eq!(9., vec[1][1]);
- let mat_clone = sub_mat_non_cont.clone().unwrap();
- assert_eq!(mat.typ().unwrap(), mat_clone.typ().unwrap());
- assert_eq!(2, mat_clone.rows().unwrap());
- assert_eq!(2, mat_clone.cols().unwrap());
- assert!(!mat_clone.is_submatrix().unwrap());
- assert!(mat_clone.is_continuous().unwrap());
- assert_eq!(5., *mat_clone.at_2d::<f32>(0, 0).unwrap());
- assert_eq!(6., *mat_clone.at_2d::<f32>(0, 1).unwrap());
- assert_eq!(8., *mat_clone.at_2d::<f32>(1, 0).unwrap());
- assert_eq!(9., *mat_clone.at_2d::<f32>(1, 1).unwrap());
+ let mat_clone = sub_mat_non_cont.clone()?;
+ assert_eq!(mat.typ()?, mat_clone.typ()?);
+ assert_eq!(2, mat_clone.rows()?);
+ assert_eq!(2, mat_clone.cols()?);
+ assert!(!mat_clone.is_submatrix()?);
+ assert!(mat_clone.is_continuous()?);
+ assert_eq!(5., *mat_clone.at_2d::<f32>(0, 0)?);
+ assert_eq!(6., *mat_clone.at_2d::<f32>(0, 1)?);
+ assert_eq!(8., *mat_clone.at_2d::<f32>(1, 0)?);
+ assert_eq!(9., *mat_clone.at_2d::<f32>(1, 1)?);
}
{
- let sub_mat_cont = Mat::roi(&mat, Rect::new(0, 1, 3, 2)).unwrap();
- assert_eq!(mat.typ().unwrap(), sub_mat_cont.typ().unwrap());
- assert_eq!(2, sub_mat_cont.rows().unwrap());
- assert_eq!(3, sub_mat_cont.cols().unwrap());
- assert!(sub_mat_cont.is_submatrix().unwrap());
- assert!(sub_mat_cont.is_continuous().unwrap());
- assert_eq!(4., *sub_mat_cont.at_2d::<f32>(0, 0).unwrap());
- assert_eq!(6., *sub_mat_cont.at_2d::<f32>(0, 2).unwrap());
- assert_eq!(7., *sub_mat_cont.at_2d::<f32>(1, 0).unwrap());
- assert_eq!(9., *sub_mat_cont.at_2d::<f32>(1, 2).unwrap());
-
- let vec = sub_mat_cont.to_vec_2d::<f32>().unwrap();
+ let sub_mat_cont = Mat::roi(&mat, Rect::new(0, 1, 3, 2))?;
+ assert_eq!(mat.typ()?, sub_mat_cont.typ()?);
+ assert_eq!(2, sub_mat_cont.rows()?);
+ assert_eq!(3, sub_mat_cont.cols()?);
+ assert!(sub_mat_cont.is_submatrix()?);
+ assert!(sub_mat_cont.is_continuous()?);
+ assert_eq!(4., *sub_mat_cont.at_2d::<f32>(0, 0)?);
+ assert_eq!(6., *sub_mat_cont.at_2d::<f32>(0, 2)?);
+ assert_eq!(7., *sub_mat_cont.at_2d::<f32>(1, 0)?);
+ assert_eq!(9., *sub_mat_cont.at_2d::<f32>(1, 2)?);
+
+ let vec = sub_mat_cont.to_vec_2d::<f32>()?;
assert_eq!(4., vec[0][0]);
assert_eq!(6., vec[0][2]);
assert_eq!(7., vec[1][0]);
assert_eq!(9., vec[1][2]);
}
+
+ Ok(())
}
#[test]
-fn mat_operations() {
+fn mat_operations() -> Result<()> {
{
let mut src = VectorOfMat::new();
- src.push(Mat::new_rows_cols_with_default(1, 3, u8::typ(), Scalar::all(1.)).unwrap());
- src.push(Mat::new_rows_cols_with_default(1, 3, u8::typ(), Scalar::all(2.)).unwrap());
- let mut merged = Mat::default();
- core::merge(&src, &mut merged).unwrap();
- assert_eq!(merged.typ().unwrap(), Vec2b::typ());
- assert_eq!(merged.at_2d::<Vec2b>(0, 1).unwrap()[0], 1);
- assert_eq!(merged.at_2d::<Vec2b>(0, 2).unwrap()[1], 2);
+ src.push(Mat::new_rows_cols_with_default(1, 3, u8::typ(), Scalar::all(1.))?);
+ src.push(Mat::new_rows_cols_with_default(1, 3, u8::typ(), Scalar::all(2.))?);
+ let mut merged = Mat::new()?;
+ core::merge(&src, &mut merged)?;
+ assert_eq!(merged.typ()?, Vec2b::typ());
+ assert_eq!(merged.at_2d::<Vec2b>(0, 1)?[0], 1);
+ assert_eq!(merged.at_2d::<Vec2b>(0, 2)?[1], 2);
let mut split = VectorOfMat::new();
- core::split(&merged, &mut split).unwrap();
+ core::split(&merged, &mut split)?;
assert_eq!(2, split.len());
let mat = split.get(0);
- assert_eq!(u8::typ(), mat.typ().unwrap());
- assert_eq!(1, mat.channels().unwrap());
- assert_eq!(1, *mat.at_2d::<u8>(0, 2).unwrap());
+ assert_eq!(u8::typ(), mat.typ()?);
+ assert_eq!(1, mat.channels()?);
+ assert_eq!(1, *mat.at_2d::<u8>(0, 2)?);
let mat = split.get(1);
- assert_eq!(u8::typ(), mat.typ().unwrap());
- assert_eq!(1, mat.channels().unwrap());
- assert_eq!(2, *mat.at_2d::<u8>(0, 0).unwrap());
+ assert_eq!(u8::typ(), mat.typ()?);
+ assert_eq!(1, mat.channels()?);
+ assert_eq!(2, *mat.at_2d::<u8>(0, 0)?);
}
+ Ok(())
}
#[test]
-fn mat_from_data() {
+fn mat_from_data() -> Result<()> {
let mut bytes = PIXEL.to_vec();
- let src = Mat::new_rows_cols_with_data(1, PIXEL.len() as _, u8::typ(), unsafe { transmute(bytes.as_mut_ptr()) }, core::Mat_AUTO_STEP).unwrap();
- assert_eq!(src.size().unwrap(), Size::new(PIXEL.len() as _, 1));
- assert_eq!(src.total().unwrap(), PIXEL.len());
- let row = src.at_row::<u8>(0).unwrap();
+ let src = Mat::new_rows_cols_with_data(1, PIXEL.len() as _, u8::typ(), unsafe { transmute(bytes.as_mut_ptr()) }, core::Mat_AUTO_STEP)?;
+ assert_eq!(src.size()?, Size::new(PIXEL.len() as _, 1));
+ assert_eq!(src.total()?, PIXEL.len());
+ let row = src.at_row::<u8>(0)?;
assert_eq!(row[0], 0x89);
assert_eq!(row[11], 0x0D);
assert_eq!(row[89], 0x82);
+ Ok(())
}

MODIFY
tests/ml.rs
tests/ml.rs
use opencv::{
core::{DataType, Mat, Scalar, Size},
ml::{self, KNearest, StatModel},
+ Result,
types::PtrOfKNearest,
};
#[test]
-fn knn() {
- let mut knn: PtrOfKNearest = KNearest::create().unwrap();
- assert!(knn.empty().unwrap());
- let samp = Mat::new_rows_cols_with_default(1, 1, f32::typ(), Scalar::all(1.)).unwrap();
- let resp = Mat::new_rows_cols_with_default(1, 1, f32::typ(), Scalar::all(2.)).unwrap();
- knn.train(&samp, ml::ROW_SAMPLE, &resp).unwrap();
- let mut resp = Mat::default();
- let mut neigh = Mat::default();
- let mut dist = Mat::default();
- knn.find_nearest(&samp, 3, &mut resp, &mut neigh, &mut dist).unwrap();
- assert_eq!(2., *resp.at_2d::<f32>(0, 0).unwrap());
- assert_eq!(Size::new(1, 1), resp.size().unwrap());
- assert_eq!(2., *neigh.at_2d::<f32>(0, 0).unwrap());
- assert_eq!(Size::new(1, 1), neigh.size().unwrap());
- assert_eq!(0., *dist.at_2d::<f32>(0, 0).unwrap());
- assert_eq!(Size::new(1, 1), dist.size().unwrap());
+fn knn() -> Result<()> {
+ let mut knn: PtrOfKNearest = KNearest::create()?;
+ assert!(knn.empty()?);
+ let samp = Mat::new_rows_cols_with_default(1, 1, f32::typ(), Scalar::all(1.))?;
+ let resp = Mat::new_rows_cols_with_default(1, 1, f32::typ(), Scalar::all(2.))?;
+ knn.train(&samp, ml::ROW_SAMPLE, &resp)?;
+ let mut resp = Mat::new()?;
+ let mut neigh = Mat::new()?;
+ let mut dist = Mat::new()?;
+ knn.find_nearest(&samp, 3, &mut resp, &mut neigh, &mut dist)?;
+ assert_eq!(2., *resp.at_2d::<f32>(0, 0)?);
+ assert_eq!(Size::new(1, 1), resp.size()?);
+ assert_eq!(2., *neigh.at_2d::<f32>(0, 0)?);
+ assert_eq!(Size::new(1, 1), neigh.size()?);
+ assert_eq!(0., *dist.at_2d::<f32>(0, 0)?);
+ assert_eq!(Size::new(1, 1), dist.size()?);
+ Ok(())
}

MODIFY
tests/objdetect.rs
tests/objdetect.rs
@@ -4,48 +4,50 @@ use opencv::{
core::Mat,
imgcodecs,
objdetect,
+ Result,
types::VectorOfPoint,
};
#[test]
-fn qr_code() {
+fn qr_code() -> Result<()> {
let qr_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/qr.png");
{
- let mut detector = objdetect::QRCodeDetector::new().unwrap();
- let src = imgcodecs::imread(qr_path.to_str().unwrap(), imgcodecs::IMREAD_COLOR).unwrap();
+ let mut detector = objdetect::QRCodeDetector::new()?;
+ let src = imgcodecs::imread(qr_path.to_str().unwrap(), imgcodecs::IMREAD_COLOR)?;
let mut pts = VectorOfPoint::new();
- let mut straight = Mat::default();
- let res = detector.detect_and_decode(&src, &mut pts, &mut straight).unwrap();
+ let mut straight = Mat::new()?;
+ let res = detector.detect_and_decode(&src, &mut pts, &mut straight)?;
assert_eq!(res, "https://crates.io/crates/opencv");
assert_eq!(4, pts.len());
- assert!(!straight.empty().unwrap());
+ assert!(!straight.empty()?);
}
{
- let mut detector = objdetect::QRCodeDetector::new().unwrap();
- let src = imgcodecs::imread(qr_path.to_str().unwrap(), imgcodecs::IMREAD_COLOR).unwrap();
+ let mut detector = objdetect::QRCodeDetector::new()?;
+ let src = imgcodecs::imread(qr_path.to_str().unwrap(), imgcodecs::IMREAD_COLOR)?;
let mut pts = VectorOfPoint::new();
- let res = detector.detect(&src, &mut pts).unwrap();
+ let res = detector.detect(&src, &mut pts)?;
assert!(res);
assert_eq!(4, pts.len());
- let mut straight = Mat::default();
- let res = detector.decode(&src, &pts, &mut straight).unwrap();
+ let mut straight = Mat::new()?;
+ let res = detector.decode(&src, &pts, &mut straight)?;
assert_eq!(res, "https://crates.io/crates/opencv");
- assert!(!straight.empty().unwrap());
+ assert!(!straight.empty()?);
}
{
- let src = imgcodecs::imread(qr_path.to_str().unwrap(), imgcodecs::IMREAD_COLOR).unwrap();
+ let src = imgcodecs::imread(qr_path.to_str().unwrap(), imgcodecs::IMREAD_COLOR)?;
let mut pts = VectorOfPoint::new();
- let res = objdetect::detect_qr_code(&src, &mut pts, 0.2, 0.1).unwrap();
+ let res = objdetect::detect_qr_code(&src, &mut pts, 0.2, 0.1)?;
assert!(res);
assert_eq!(4, pts.len());
let mut out = String::new();
- let mut straight = Mat::default();
- let res = objdetect::decode_qr_code(&src, &pts, &mut out, &mut straight).unwrap();
+ let mut straight = Mat::new()?;
+ let res = objdetect::decode_qr_code(&src, &pts, &mut out, &mut straight)?;
assert!(res);
assert_eq!(out, "https://crates.io/crates/opencv");
- assert!(!straight.empty().unwrap());
+ assert!(!straight.empty()?);
}
+ Ok(())
}

MODIFY
tests/rect.rs
tests/rect.rs
-use opencv::core;
-use opencv::core::{Rect2d, Point2d, Size2d};
+use opencv::core::{self, Point2d, Rect2d, Size2d};
#[test]
fn rect_add() {

MODIFY
tests/types.rs
tests/types.rs
use opencv::{
core::{self, DataType, Mat, Point, Scalar, Size},
imgproc,
+ Result,
types,
};
use opencv::core::Vec3b;
#[test]
-fn simple_struct() {
- let res = imgproc::get_structuring_element(imgproc::MORPH_CROSS, Size { width: 100, height: 100 }, Point { x: 50, y: 50 }).unwrap();
- assert_eq!(res.typ().unwrap(), 0);
- let size = res.size().unwrap();
+fn simple_struct() -> Result<()> {
+ let res = imgproc::get_structuring_element(imgproc::MORPH_CROSS, Size { width: 100, height: 100 }, Point { x: 50, y: 50 })?;
+ assert_eq!(res.typ()?, 0);
+ let size = res.size()?;
assert_eq!(size.width, 100);
assert_eq!(size.height, 100);
+ Ok(())
}
#[test]
-fn vec() {
- let mut m = Mat::new_rows_cols_with_default(10, 10, Vec3b::typ(), Scalar::default()).unwrap();
+fn vec() -> Result<()> {
+ let mut m = Mat::new_rows_cols_with_default(10, 10, Vec3b::typ(), Scalar::default())?;
let mut ps = types::VectorOfMat::new();
assert_eq!(ps.len(), 0);
- let mut p1 = unsafe { Mat::new_rows_cols(3, 2, i32::typ()) }.unwrap();
- p1.at_row_mut::<i32>(0).unwrap().copy_from_slice(&[0, 0]);
- p1.at_row_mut::<i32>(1).unwrap().copy_from_slice(&[0, 9]);
- p1.at_row_mut::<i32>(2).unwrap().copy_from_slice(&[9, 9]);
+ let mut p1 = unsafe { Mat::new_rows_cols(3, 2, i32::typ()) }?;
+ p1.at_row_mut::<i32>(0)?.copy_from_slice(&[0, 0]);
+ p1.at_row_mut::<i32>(1)?.copy_from_slice(&[0, 9]);
+ p1.at_row_mut::<i32>(2)?.copy_from_slice(&[9, 9]);
ps.push(p1);
assert_eq!(ps.len(), 1);
- imgproc::fill_poly(&mut m, &ps, Scalar::new(127., 127., 127., 0.), core::LINE_8, 0, Point::default()).unwrap();
- assert_eq!(*m.at_2d::<core::Vec3b>(0, 0).unwrap(), core::Vec3b::from([127, 127, 127]));
- assert_eq!(*m.at_2d::<core::Vec3b>(0, 9).unwrap(), core::Vec3b::default());
- assert_eq!(*m.at_2d::<core::Vec3b>(9, 9).unwrap(), core::Vec3b::from([127, 127, 127]));
+ imgproc::fill_poly(&mut m, &ps, Scalar::new(127., 127., 127., 0.), core::LINE_8, 0, Point::default())?;
+ assert_eq!(*m.at_2d::<core::Vec3b>(0, 0)?, core::Vec3b::from([127, 127, 127]));
+ assert_eq!(*m.at_2d::<core::Vec3b>(0, 9)?, core::Vec3b::default());
+ assert_eq!(*m.at_2d::<core::Vec3b>(9, 9)?, core::Vec3b::from([127, 127, 127]));
+ Ok(())
}
#[test]
-fn scalar() {
- let mut m = Mat::new_rows_cols_with_default(1, 3, u8::typ(), Scalar::new(2., 0., 0., 0.)).unwrap();
- let sum = core::sum(&m).unwrap();
+fn scalar() -> Result<()> {
+ let mut m = Mat::new_rows_cols_with_default(1, 3, u8::typ(), Scalar::new(2., 0., 0., 0.))?;
+ let sum = core::sum(&m)?;
assert_eq!(sum[0], 6.);
- let s = m.at_row_mut::<u8>(0).unwrap();
+ let s = m.at_row_mut::<u8>(0)?;
s[0] = 1;
s[1] = 2;
s[2] = 3;
- let sum = core::sum(&m).unwrap();
+ let sum = core::sum(&m)?;
assert_eq!(sum[0], 6.);
+ Ok(())
}

MODIFY
tests/vector.rs
tests/vector.rs
use opencv::{
core::{DataType, Mat, Point2d, Scalar},
+ Result,
types::{VectorOfbool, VectorOfint, VectorOfMat, VectorOfPoint2d},
};
#[test]
-fn types() {
+fn types() -> Result<()> {
{
let mut vec = VectorOfint::with_capacity(10);
vec.push(1);
@@ -27,15 +28,15 @@ fn types() {
{
let mut vec = VectorOfMat::new();
- vec.push(Mat::new_rows_cols_with_default(1, 3, u8::typ(), Scalar::all(1.)).unwrap());
- vec.push(Mat::new_rows_cols_with_default(1, 3, u16::typ(), Scalar::all(2.)).unwrap());
- vec.push(Mat::new_rows_cols_with_default(1, 3, i32::typ(), Scalar::all(3.)).unwrap());
- assert_eq!(u8::typ(), vec.get(0).typ().unwrap());
- assert_eq!(1, *vec.get(0).at_2d::<u8>(0, 1).unwrap());
- assert_eq!(u16::typ(), vec.get(1).typ().unwrap());
- assert_eq!(2, *vec.get(1).at_2d::<u16>(0, 1).unwrap());
- assert_eq!(i32::typ(), vec.get(2).typ().unwrap());
- assert_eq!(3, *vec.get(2).at_2d::<i32>(0, 1).unwrap());
+ vec.push(Mat::new_rows_cols_with_default(1, 3, u8::typ(), Scalar::all(1.))?);
+ vec.push(Mat::new_rows_cols_with_default(1, 3, u16::typ(), Scalar::all(2.))?);
+ vec.push(Mat::new_rows_cols_with_default(1, 3, i32::typ(), Scalar::all(3.))?);
+ assert_eq!(u8::typ(), vec.get(0).typ()?);
+ assert_eq!(1, *vec.get(0).at_2d::<u8>(0, 1)?);
+ assert_eq!(u16::typ(), vec.get(1).typ()?);
+ assert_eq!(2, *vec.get(1).at_2d::<u16>(0, 1)?);
+ assert_eq!(i32::typ(), vec.get(2).typ()?);
+ assert_eq!(3, *vec.get(2).at_2d::<i32>(0, 1)?);
}
{
@@ -47,6 +48,8 @@ fn types() {
assert_eq!(Point2d::new(20., 20.), *vec.get(1));
assert_eq!(Point2d::new(30., 30.), *vec.get(2));
}
+
+ Ok(())
}
#[test]

make tests return result and replace unwrap() with ?