[Rust OpenCV] 1.01 OpenCV 4로 배우는 컴퓨터 비전과 머신 러닝을 Rust로 Re-write

/**
* original source : https://github.com/sunkyoo/opencv4cvml/blob/master/ch03/BasicOp/main.cpp
* Cargo.toml
*. opencv = "0.82.0"
*/
use opencv::core::*;
/* Point : https://docs.rs/opencv/0.83.0/opencv/core/struct.Point_.html
*/
fn point_op() {
let mut pt1 = Point::default();
pt1.x = 5;
pt1.y = 10;
let pt2 = Point::new(10, 30);
let pt3 = pt1 + pt2;
let pt4 = pt1 * 2;
let d1 = pt1.dot(pt2); // dot product. 5*10 + 10*30
let b1 = pt1 == pt2; // FALSE
println!("{},{}", pt1.x, pt1.y);
println!("{},{}", pt2.x, pt2.y);
println!("{},{}", pt3.x, pt3.y);
println!("{},{}", pt4.x, pt4.y);
println!("{}", d1);
println!("{}", b1);
}
/* Size : https://docs.rs/opencv/0.83.0/opencv/core/struct.Size_.html
*/
fn size_op() {
let mut sz1 = Size::default();
let sz2 = Size::new(10, 20);
sz1.width = 5;
sz1.height = 10;
let sz3 = sz1 + sz2;
let sz4 = sz1 * 2;
let area1 = sz4.area();
println!("{} {}", sz1.width, sz1.height);
println!("{} {}", sz2.width, sz2.height);
println!("{} {}", sz3.width, sz3.height);
println!("{} {}", sz4.width, sz4.height);
println!("sz4.area {}", area1);
}
/* Rect : https://docs.rs/opencv/0.83.0/opencv/core/struct.Rect_.html
*/
fn rect_op() {
let rc1 = Rect::default();
let rc2 = Rect::new(10, 10, 60, 40);
let rc3 = rc1 + Size::new(50, 40); // 50x40 from (0,0)
let rc4 = rc2 + Point::new(10, 10); // 60x40 from 20,20
let rc5 = rc3 & rc4; // 30x20 from 20,20
let rc6 = rc3 | rc4; // 80x60 from 0,0
println!("{}x{} from {},{}", rc1.width, rc1.height, rc1.x, rc1.y);
println!("{}x{} from {},{}", rc2.width, rc2.height, rc2.x, rc2.y);
println!("{}x{} from {},{}", rc3.width, rc3.height, rc3.x, rc3.y);
println!("{}x{} from {},{}", rc4.width, rc4.height, rc4.x, rc4.y);
println!("{}x{} from {},{}", rc5.width, rc5.height, rc5.x, rc5.y);
println!("{}x{} from {},{}", rc6.width, rc6.height, rc6.x, rc6.y);
}
/* RotectedRect : https://docs.rs/opencv/0.83.0/opencv/core/struct.RotatedRect.html
*/
fn rotated_rect_op() {
let rr1 = RotatedRect::new(Point2f::new(40.0, 30.0), Size2f::new(40.0, 20.0), 30.0).unwrap();
let mut pts = [Point2f::default(); 4];
let r = rr1.points(&mut pts);
/*
* 17.679493, 28.660255
* 27.679493, 11.339746
* 62.320507, 31.339745
* 52.320507, 48.660255
*/
for point in &pts {
println!("{}, {}", point.x, point.y);
}
}
fn main() {
point_op();
size_op();
rect_op();
rotated_rect_op();
}
view raw BasicOp.rs hosted with ❤ by GitHub




Original Code opencv4cvml/ch03/BasicOp/main.cpp

 

코딩을 배우는 가장 좋은 방법은 베끼는거죠!

opencv::core::{Point, Size, Rect, RotatedRect} 샘플 코드를 rust 로 작성해봤습니다.

RotatedRect.points 를 쓰는데 자꾸 에러가 발생해서 코드를 아래와 같이 바꾸었더니 정상 동작하였습니다.

use opencv::core::*; 

아직 Rust 내공이 미천하여 왜 그런지는 모르겠습니다만, 하다보면 알게 되겠지요!



댓글

이 블로그의 인기 게시물

WSL2 Ubuntu 20.04 및 네트워크 설정

리눅스 멀티코어를 사용하는 tar 압축/해제

WSL에 X-Window로 Terminator 설치하고 Minicom까지