1
1
# cv-convert
2
2
Convert computer vision data types in Rust
3
3
4
- Type conversions among famous Rust computer vision libraries. It
5
- supports the following crates:
6
-
7
- reference:
8
- https://github.com/jerry73204/rust-cv-convert
9
-
10
- ## sys related
11
- - [ opencv] ( https://crates.io/crates/opencv )
12
- - [ tch] ( https://crates.io/crates/tch )
13
- - [ rsmpeg] ( https://crates.io/crates/rsmpeg )
4
+ reference: [ jerry73204] ( https://github.com/jerry73204/rust-cv-convert )
5
+
6
+ ## Concept
7
+
8
+ ``` mermaid
9
+ graph LR
10
+ %% 核心节点定义
11
+ AF[AVFrame<br><i>视频原始数据</i>]:::avframe
12
+ MA[Mat<br><i>OpenCV矩阵</i>]:::mat
13
+ IM[Image<br><i>通用图像</i>]:::image
14
+ ND[ndarray<br><i>数值数组</i>]:::ndarray
15
+ TE[Tensor<br><i>深度学习张量</i>]:::tensor
16
+
17
+ %% 转换路径矩阵
18
+ AF <-.->|FFmpeg sws_scale| MA
19
+ AF <-.->|YUV2RGB转换| IM
20
+ AF <-.->|planes_to_3darray| ND
21
+ AF <-.->|CUDA内存映射| TE
22
+
23
+ MA <-.->|Mat::from_slice| ND
24
+ MA <-.->|imencode/imdecode| IM
25
+ MA <-.->|Mat::to_gpu| TE
26
+
27
+ IM <-.->|image::buffer| ND
28
+ IM <-.->|image_to_tensor| TE
29
+ IM <-.->|save_to_avframe| AF
30
+
31
+ ND <-.->|ndarray_to_tensor| TE
32
+ ND <-.->|reshape_to_mat| MA
33
+ ND <-.->|as_image_buffer| IM
34
+
35
+ TE <-.->|to_ndarray| ND
36
+ TE <-.->|tensor_to_mat| MA
37
+ TE <-.->|render_to_avframe| AF
38
+
39
+ classDef avframe fill:#FFEBEE,stroke:#FF5252;
40
+ classDef mat fill:#FFF3E0,stroke:#FFB300;
41
+ classDef image fill:#E3F2FD,stroke:#2196F3;
42
+ classDef ndarray fill:#E8F5E9,stroke:#4CAF50;
43
+ classDef tensor fill:#F3E5F5,stroke:#9C27B0;
44
+ ```
14
45
15
- ## lib
16
- - [ image] ( https://crates.io/crates/image )
17
- - [ imageproc] ( https://crates.io/crates/imageproc )
18
- - [ nalgebra] ( https://crates.io/crates/nalgebra )
19
- - [ ndarray] ( https://crates.io/crates/ndarray )
46
+ > 异常处理矩阵:
47
+ >
48
+ | 转换路径 | 可能异常 | 解决方案 |
49
+ | --------- | ------- | ------ |
50
+ AVFrame→Mat | 色彩空间不匹配 |自动插入sws_scale转换上下文
51
+ Image→ndarray | 通道顺序差异(RGB vs BGR) | 提供convert_channels特性方法
52
+ Mat→Tensor | 内存对齐问题 | 使用aligned_alloc分配器
53
+
54
+
55
+ ``` mermaid
56
+ graph TD
57
+ Start{选择起点} --> A[AVFrame]
58
+ Start --> B[Mat]
59
+ Start --> C[Image]
60
+ Start --> D[ndarray]
61
+ Start --> E[Tensor]
62
+
63
+ A -->|实时流处理| F[保持AVFrame]
64
+ A -->|视觉分析| G[转Mat]
65
+ A -->|AI推理| H[转Tensor]
66
+
67
+ B -->|算法优化| I[保持Mat]
68
+ B -->|持久化存储| J[转Image]
69
+ B -->|数值计算| K[转ndarray]
70
+
71
+ C -->|编辑处理| L[保持Image]
72
+ C -->|视频合成| M[转AVFrame]
73
+ C -->|模型训练| N[转Tensor]
74
+
75
+ D -->|科学计算| O[保持ndarray]
76
+ D -->|可视化| P[转Mat]
77
+ D -->|深度学习| Q[转Tensor]
78
+
79
+ E -->|推理结果| R[保持Tensor]
80
+ E -->|结果可视化| S[转Mat]
81
+ E -->|视频编码| T[转AVFrame]
82
+ ```
20
83
21
84
## Usage
22
85
@@ -25,27 +88,22 @@ https://github.com/jerry73204/rust-cv-convert
25
88
cv-convert = { git = " https://github.com/phial3/cv-convert" , branch = " main" }
26
89
```
27
90
28
- The minimum supported ` rustc ` is 1.81. You may use older versions of
29
- the crate (>=0.6) in order to use ` rustc ` versions that do not support
30
- const-generics.
31
-
32
- ## Cargo Features
91
+ ## Features
33
92
- ` default ` : enable ` image ` + ` imageproc ` + ` nalgebra ` + ` ndarray `
34
- - ` tch `
35
- - ` opencv `
36
- - ` rsmpeg `
93
+ - ` tch ` : optional, (System Required installation: [ tch ] ( https://crates.io/crates/tch ) )
94
+ - ` opencv ` : optional, (System Required installation: [ opencv ] ( https://crates.io/crates/opencv ) )
95
+ - ` rsmpeg ` : optional, (System Required installation: [ rsmpeg ] ( https://crates.io/crates/rsmpeg ) )
37
96
- ` full ` : enable ` tch ` + ` opencv ` + ` rsmpeg `
38
- - ` image `
39
- - ` imageproc `
40
- - ` nalgebra `
41
- - ` ndarray `
42
-
97
+ - ` image ` : optional, enable [ image] ( https://crates.io/crates/image )
98
+ - ` imageproc ` : optional, enable [ imageproc] ( https://crates.io/crates/imageproc )
99
+ - ` nalgebra ` : optional, enable [ nalgebra] ( https://crates.io/crates/nalgebra )
100
+ - ` ndarray ` : optional, enable [ ndarray] ( https://crates.io/crates/ndarray )
43
101
44
- ## Usage
102
+ ## Examples
45
103
46
104
The crate provides ` FromCv ` , ` TryFromCv ` , ` IntoCv ` , ` TryIntoCv ` traits, which are similar to standard library's ` From ` and ` Into ` .
47
105
48
- ``` rust
106
+ ``` rust,ignore,no_run
49
107
use cv_convert::{FromCv, IntoCv, TryFromCv, TryIntoCv};
50
108
use nalgebra as na;
51
109
use opencv as cv;
@@ -69,25 +127,6 @@ let cv_mat: cv::core::Mat = na_mat.try_into_cv()?;
69
127
70
128
## Contribute to this Project
71
129
72
- ### Add a new dependency version
73
-
74
- To add the new version of nalgebra 0.32 for cv-convert for example,
75
- open ` cv-convert-generate/packages.toml ` in the source repository. Add
76
- a new version to the list like this.
77
-
78
- ``` toml
79
- [package .nalgebra ]
80
- versions = [" 0.26" , " 0.27" , " 0.28" , " 0.29" , " 0.30" , " 0.31" , " 0.32" ]
81
- use_default_features = true
82
- features = []
83
- ```
84
-
85
- Run ` make generate ` at the top-level directory. It modifies Rust
86
- source files automatically. One extra step is to copy the snipplet in
87
- ` cv-convert/generated/Cargo.toml.snipplet ` and paste it to
88
- ` cv-convert/Cargo.toml ` .
89
-
90
-
91
130
### Add a new type conversion
92
131
93
132
To add a new type conversion, take ` image::DynamicImage ` and
0 commit comments