Loading

opencv-rust.md

opencv-rust

此文使用项目: opencv-rust

学习参考: opencv c++

version: rust 2021 opencv 4.5.4

使用

在cargo.toml添加:

[package]
name = "game-ip"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
opencv = "0.61.3"

或: 在项目目录下运行 cargo add opencv-rust

测试

cargo build

Linux

yay -S opencv llvm clang

Window

choco install llvm opencv

also set OPENCV_LINK_LIBS, OPENCV_LINK_PATHS and OPENCV_INCLUDE_PATHS environment variables (see below for details).

具体: https://github.com/twistedfall/opencv-rust/issues/118#issuecomment-619608278

Introduction to OpenCV

读取图片

use opencv::core::{self, find_file};
use opencv::highgui::{imshow, wait_key};
use opencv::imgcodecs::{self, IMREAD_COLOR};
use opencv::prelude::MatTraitConst;

pub fn main() {
    let image_path = find_file("img.png", true, false).unwrap();
    let img = imgcodecs::imread(&image_path, IMREAD_COLOR).unwrap();

    // 此处,需要导入 opencv::prelude::MatTraitConst 此Trait,
    if img.empty() {
        println!("Could not read th image file: {}", image_path);
        return ;
    }

    imshow("Display Window", &img).unwrap();
    let k = wait_key(0).unwrap();  // 0表示一直等待

    // i32 转 char应该有更好的写法
    if k == 's' as i32 {
        imgcodecs::imwrite("2.png", &img, &core::Vector::new()).unwrap();
    }
}

opencv不同的模块负责不同的图像处理部分。

  • core: 定义库的基本构建block
  • imgcodecs: 处理(图片)输入和输出 [读取和写入]
  • highgui: 处理图像显示部分 (GUI)

函数:

  • imread:

    参数

    1. 图片路径

    2. 图片格式:

      • IMREAD_COLOR (default) 以BGR 8bit格式加载
      • IMREAD_UNCHANGED 原图? 会包含透明通道
      • IMREAD_GRAYSCALE 灰度图

    返回

    Mat格式

Mat

Opencv内部负责图片的内存管理

Mat负责表述图片结构。结构为: Mat headerpointer(指向实际图片数据)。Opencv使用引用技术,一个图片数据可能会在多个mat之间共享。多个Mat之间不同的只是header,复制时只复制头部。

/**
* A-E全部使用一个matrix数据
*/
Mat A, C;                          // creates just the header parts
A = imread(argv[1], IMREAD_COLOR); // here we'll know the method used (allocate matrix)
Mat B(A);                                 // Use the copy constructor
C = A;                                    // Assignment operator

// 仅使用一部分
Mat D (A, Rect(10, 10, 100, 100) ); // using a rectangle
Mat E = A(Range::all(), Range(1,3)); // using row and column boundaries

当需要复制matrix时,调用A.clone()A.copyTo(G)

Opencv使用BGR颜色构建,不是RGB

主要颜色表达格式:

  • RGB
  • HSV and HLS 将颜色分解为其色调、饱和度和值/亮度分量
  • YCrCb jpeg
  • CIE Lab* 测距

创建Mat

  • cv::Mat::Constructor

    Mat M(2,2, CV_8UC3, Scalar(0,0,255));
    cout << "M = " << endl << " " << M << endl << endl;
    

    MatBasicContainerOut1.png先指rowcolumn,再指定数据类型。

    // cv的格式
    CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number]
    

    Scalar用于填充数据。

  • cv::Mat::create:

    // 不能用于替代构造函数,当大小不合适时,会重新分配内存
    M.create(4,4, CV_8UC(2));
    
  • MATLAB风格: [cv::Mat::zeros] , [cv::Mat::ones], [cv::Mat::eye]

        Mat E = Mat::eye(4, 4, CV_64F);
        cout << "E = " << endl << " " << E << endl << endl;
        Mat O = Mat::ones(2, 2, CV_32F);
        cout << "O = " << endl << " " << O << endl << endl;
        Mat Z = Mat::zeros(3,3, CV_8UC1);
        cout << "Z = " << endl << " " << Z << endl << endl;
    

    MatBasicContainerOut3.png

  • cv::Mat::clone()cv::Mat::copyTo()

  • cv::randu 随机

    Mat R = Mat(3, 2, CV_8UC3);
    // 需指定上下限
    randu(R, Scalar::all(0), Scalar::all(255));
    

输出

Pointf

TODO: rust暂未找到相关函数?

Scan image && lookup tables && time measure

矩阵掩码

posted @ 2022-05-27 14:11  nsfoxer  阅读(289)  评论(0编辑  收藏  举报