Skip to content

Commit cf618bf

Browse files
authored
Merge pull request #1 from phial3/develop
add convert for AVFrame, Mat, Image
2 parents 4fabce0 + 96072e8 commit cf618bf

11 files changed

+1638
-82
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ jobs:
7272

7373
- name: Setup ffmpeg env
7474
run: |
75-
# 设置 FFmpeg 环境变量
75+
# set env
7676
export FFMPEG_BUILD_DIR=${PWD}/tmp/ffmpeg_build
7777
echo "FFMPEG_DIR=${FFMPEG_BUILD_DIR}" >> $GITHUB_ENV
7878
echo "FFMPEG_INCLUDE_DIR=${FFMPEG_BUILD_DIR}/include" >> $GITHUB_ENV
7979
echo "FFMPEG_LIB_DIR=${FFMPEG_BUILD_DIR}/lib" >> $GITHUB_ENV
8080
echo "PKG_CONFIG_PATH=${FFMPEG_BUILD_DIR}/lib/pkgconfig:$PKG_CONFIG_PATH" >> $GITHUB_ENV
8181
echo "LD_LIBRARY_PATH=${FFMPEG_BUILD_DIR}/lib:$LD_LIBRARY_PATH" >> $GITHUB_ENV
82-
# 验证环境变量
82+
# check env
8383
echo "FFmpeg build directory: $FFMPEG_BUILD_DIR"
8484
echo "FFmpeg include directory: $FFMPEG_INCLUDE_DIR"
8585
echo "FFmpeg library directory: $FFMPEG_LIB_DIR"

README.md

Lines changed: 88 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,85 @@
11
# cv-convert
22
Convert computer vision data types in Rust
33

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+
```
1445

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+
```
2083

2184
## Usage
2285

@@ -25,27 +88,22 @@ https://github.com/jerry73204/rust-cv-convert
2588
cv-convert = { git = "https://github.com/phial3/cv-convert", branch = "main" }
2689
```
2790

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
3392
- `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))
3796
- `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)
43101

44-
## Usage
102+
## Examples
45103

46104
The crate provides `FromCv`, `TryFromCv`, `IntoCv`, `TryIntoCv` traits, which are similar to standard library's `From` and `Into`.
47105

48-
```rust
106+
```rust,ignore,no_run
49107
use cv_convert::{FromCv, IntoCv, TryFromCv, TryIntoCv};
50108
use nalgebra as na;
51109
use opencv as cv;
@@ -69,25 +127,6 @@ let cv_mat: cv::core::Mat = na_mat.try_into_cv()?;
69127

70128
## Contribute to this Project
71129

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-
91130
### Add a new type conversion
92131

93132
To add a new type conversion, take `image::DynamicImage` and

src/lib.rs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -226,21 +226,21 @@ pub mod with_nalgebra;
226226
pub mod with_ndarray;
227227

228228
/// 工具库组合模块
229+
#[cfg(all(feature = "image", feature = "nalgebra"))]
230+
pub mod with_image_nalgebra;
231+
229232
#[cfg(all(feature = "imageproc", feature = "nalgebra"))]
230233
pub mod with_imageproc_nalgebra;
231234

232235
#[cfg(all(feature = "imageproc", feature = "ndarray"))]
233236
pub mod with_imageproc_ndarray;
234237

235-
#[cfg(all(feature = "nalgebra", feature = "image"))]
236-
pub mod with_nalgebra_image;
237-
238-
#[cfg(all(feature = "nalgebra", feature = "ndarray"))]
239-
pub mod with_nalgebra_ndarray;
240-
241238
#[cfg(all(feature = "ndarray", feature = "image"))]
242239
pub mod with_ndarray_image;
243240

241+
#[cfg(all(feature = "ndarray", feature = "nalgebra"))]
242+
pub mod with_ndarray_nalgebra;
243+
244244
/// OpenCV 相关模块
245245
#[cfg(feature = "opencv")]
246246
pub mod with_opencv;
@@ -297,25 +297,3 @@ pub mod with_tch_nalgebra;
297297

298298
#[cfg(all(feature = "tch", feature = "ndarray"))]
299299
pub mod with_tch_ndarray;
300-
301-
/// Re-exports for convenience
302-
#[cfg(feature = "image")]
303-
pub use image;
304-
305-
#[cfg(feature = "imageproc")]
306-
pub use imageproc;
307-
308-
#[cfg(feature = "nalgebra")]
309-
pub use nalgebra;
310-
311-
#[cfg(feature = "ndarray")]
312-
pub use ndarray;
313-
314-
#[cfg(feature = "opencv")]
315-
pub use opencv;
316-
317-
#[cfg(feature = "rsmpeg")]
318-
pub use rsmpeg;
319-
320-
#[cfg(feature = "tch")]
321-
pub use tch;
File renamed without changes.
File renamed without changes.

src/with_opencv.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::{FromCv, IntoCv, TryFromCv, TryIntoCv};
22
use anyhow::{Error, Result};
33
use half::f16;
4-
use opencv::{core as cv_core, prelude::*};
4+
use opencv::core as cv_core;
5+
use opencv::prelude::*;
56

67
pub use element_type::*;
78
mod element_type {

src/with_opencv_nalgebra.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::{FromCv, IntoCv, TryFromCv, TryIntoCv};
22
use anyhow::{Error, Result};
33
use nalgebra::geometry;
4-
use opencv::{calib3d, core as cv_core, prelude::*};
4+
use opencv::calib3d;
5+
use opencv::core as cv_core;
6+
use opencv::prelude::*;
57

68
/// NOTE: for future maintainers: Since the matrixes need to accommodate any size Matrix, we are using nalgebra::OMatrix instead of SMatrix.
79
///

0 commit comments

Comments
 (0)