Skip to main content

5.2.4 图像处理加速

高斯滤波

功能介绍

实现高斯滤波功能,加速类型分为BPU加速和NEON加速,BPU加速暂时只支持int16格式,NEON加速暂时只支持int16和uint16格式。

代码仓库:https://github.com/D-Robotics/hobot_cv

支持平台

平台运行方式示例功能
RDK X3, RDK X3 ModuleUbuntu 20.04 (Foxy), Ubuntu 22.04 (Humble)读取ToF图片,进行高斯滤波

准备工作

RDK平台

  1. RDK已烧录好Ubuntu 20.04/Ubuntu 22.04系统镜像。

  2. RDK已成功安装TogetheROS.Bot。

使用介绍

BPU加速

当前版本支持的参数范围如下:

  • 滤波类型:高斯滤波

  • 支持的数据类型:int16

  • 支持的分辨率:320x240。

  • 滤波核:高斯3x3

  • sigmax: 0

  • sigmay: 0

NEON加速

当前版本支持的参数范围如下:

  • 滤波类型:高斯滤波

  • 支持的数据类型:int16、uint16

  • 滤波核:高斯3x3,5x5

  • sigmax: 0

  • sigmay: 0

package中提供了简单测试程序,输入为本地的ToF图片,调用hobot_cv中接口实现高斯滤波功能。具体接口说明可参考hobot_cv pakcage中的README.md进一步了解。

RDK平台

# 配置tros.b环境
source /opt/tros/setup.bash
# 从tros.b的安装路径中拷贝出运行示例需要的模型和配置文件。
cp -r /opt/tros/${TROS_DISTRO}/lib/hobot_cv/config/ .

# 启动BPU加速测试程序pkg
ros2 launch hobot_cv hobot_cv_gaussian_blur.launch.py

# 启动NEON加速测试程序pkg
ros2 launch hobot_cv hobot_cv_neon_blur.launch.py

结果分析

BPU加速

输出结果:

===================
image name :images/frame1_4.png
infe cost time:1314
guss_time cost time:2685
hobotcv save rate:0.510615

analyse_result start
---------GaussianBlur
out_filter type:2,cols:320,rows:240,channel:1
cls_filter type:2,cols:320,rows:240,channel:1
out_filter minvalue:96,max:2363
out_filter min,x:319,y:115
out_filter max,x:147,y:239
cls_filter minvalue:96,max:2364
cls_filter min,x:319,y:115
cls_filter max,x:147,y:239

diff diff diff
mat_diff minvalue:0,max:2
mat_diff min,x:2,y:0
mat_diff max,x:110,y:14

error sum:8.46524e+06,max:2,mean_error:0.439232
analyse_result,time_used_ms_end:2
analyse_result end

-------------------------

其中:

infe cost time:1314 //表示hobotcv加速的高斯滤波耗时1314微秒。

guss_time cost time:2685 //表示opencv的高斯滤波耗时2685微秒。

hobotcv save rate = (guss_time cost time - infe cost time)/ guss_time cost time = 0.510615

从以上比较结果,经过hobot_cv加速后性能提升50%。

error sum:8.46524e+06,max:2,mean_error:0.439232 //单张图片总误差是:8.46524e+06,单个像素最大误差是:2,平均误差:0.439232

平均误差 = sum / (width height) = 8.46524e+06 / (320 240)

hobot_cv高斯滤波BPU加速与opencv高斯滤波性能对比结果如下:

接口类型滤波核大小耗时(ms)单核CPU占比(%)
Hobotcv gaussianSize(3,3)1.1043515.9
Opencv gaussianSize(3,3)2.4186149.7

NEON加速

输出结果:
[neon_example-1] ===================
[neon_example-1] image name :config/tof_images/frame1_4.png
[neon_example-1] hobotcv mean cost time:674
[neon_example-1] opencv mean cost time:1025
[neon_example-1] hobotcv mean save rate:0.342439
[neon_example-1]
[neon_example-1] analyse_result start
[neon_example-1] ---------Mean_Blur
[neon_example-1] error sum:8.43744e+06,max:1,mean_error:0.430833
[neon_example-1]
[neon_example-1] hobotcv gaussian cost time:603
[neon_example-1] opencv gaussian cost time:2545
[neon_example-1] hobotcv gaussian save rate:0.763065
[neon_example-1]
[neon_example-1] analyse_result start
[neon_example-1] ---------Gaussian_Blur
[neon_example-1] error sum:9.13206e+06,max:1,mean_error:0.466302
[neon_example-1]
[neon_example-1] -------------------------

hobotcv gaussian cost time:603 //hobotcv 高斯滤波neon加速接口耗时603微秒。 opencv gaussian cost time:2545 //表示opencv的高斯滤波耗时2545微秒。 hobotcv gaussian save rate = (opencv cost time - hobotcv cost time)/ opencv cost time = 0.763065 从以上比较结果,经过hobotcv加速后高斯滤波性能提升76%。

hobot_cv高斯滤波NEON加速与opencv高斯滤波性能对比结果如下:

接口类型滤波核大小耗时(ms)单核CPU占比(%)
Hobotcv gaussianSize(3,3)0.43028427.1
Opencv gaussianSize(3,3)2.4222547
Hobotcv gaussianSize(5,5)0.85487139.1
Opencv gaussianSize(5,5)3.1564799.8

均值滤波

功能介绍

实现均值滤波功能,加速方式为NEON加速,暂时只支持int16,uint16格式。

代码仓库:https://github.com/D-Robotics/hobot_cv

支持平台

平台运行方式示例功能
RDK X3, RDK X3 ModuleUbuntu 20.04 (Foxy), Ubuntu 22.04 (Humble)读取ToF图片,进行均值滤波

准备工作

RDK平台

  1. RDK已烧录好Ubuntu 20.04/Ubuntu 22.04系统镜像。

  2. RDK已成功安装TogetheROS.Bot。

使用介绍

当前版本支持的参数范围如下:

  • 滤波类型:均值滤波

  • 支持的数据类型:int16,uint16

  • 滤波核:3x3,5x5

package中提供了简单测试程序,输入为本地的ToF图片,调用hobot_cv中接口实现均值滤波功能。具体接口说明可参考hobot_cv pakcage中的README.md进一步了解。

RDK平台

# 配置tros.b环境
source /opt/tros/setup.bash
# 从TogetheROS的安装路径中拷贝出运行示例需要的配置文件。
cp -r /opt/tros/${TROS_DISTRO}/lib/hobot_cv/config/ .

# 启动测试程序pkg
ros2 launch hobot_cv hobot_cv_neon_blur.launch.py

结果分析

输出结果:
[neon_example-1] ===================
[neon_example-1] image name :config/tof_images/frame1_4.png
[neon_example-1] hobotcv mean cost time:674
[neon_example-1] opencv mean cost time:1025
[neon_example-1] hobotcv mean save rate:0.342439
[neon_example-1]
[neon_example-1] analyse_result start
[neon_example-1] ---------Mean_Blur
[neon_example-1] error sum:8.43744e+06,max:1,mean_error:0.430833
[neon_example-1]
[neon_example-1] -------------------------

其中:

hobotcv mean cost time:674 //hobot_cv 均值滤波neon加速接口耗时674微秒。 opencv mean cost time:1025 //表示opencv的均值滤波耗时1025微秒。 hobotcv mean save rate = (opencv cost time - hobotcv cost time)/ opencv cost time = 0.342439 从以上比较结果,经过hobotcv加速后均值滤波性能提升34%

error sum:8.43744e+06,max:1,mean_error:0.430833 //均值滤波单张图片总误差是:8.43744e+06,单个像素最大误差是:1,平均误差:0.430833 均值滤波平均误差 = sum / (width x height) = 8.43744e+06 / (320 x 240)

hobot_cv与opencv处理性能对比

接口类型滤波核大小耗时(ms)单核CPU占比(%)
Hobotcv meanSize(3,3)0.46639731.8
Opencv meanSize(3,3)0.67667740.2
Hobotcv meanSize(5,5)0.73717147.7
Opencv meanSize(5,5)0.79817752.9

crop

功能介绍

实现图像裁剪功能,暂时只支持NV12格式。

代码仓库:https://github.com/D-Robotics/hobot_cv

支持平台

平台运行方式示例功能
RDK X3, RDK X3 ModuleUbuntu 20.04 (Foxy), Ubuntu 22.04 (Humble)读取图片,进行抠图
RDK X5, RDK X5 ModuleUbuntu 22.04 (Humble)读取图片,进行抠图

准备工作

RDK平台

  1. RDK已烧录好Ubuntu 20.04/Ubuntu 22.04系统镜像。

  2. RDK已成功安装TogetheROS.Bot。

使用介绍

RDK平台

# 配置tros.b环境
source /opt/tros/setup.bash
# 从tros.b的安装路径中拷贝出运行示例需要的模型和配置文件。
cp -r /opt/tros/${TROS_DISTRO}/lib/hobot_cv/config/ .

# 启动launch文件
ros2 launch hobot_cv hobot_cv_crop.launch.py

结果分析

[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [crop_example-1]: process started with pid [3064]
[crop_example-1] [INFO] [1655951627.255477663] [example]: crop image to 960x540 pixels, time cost: 1 ms
[crop_example-1] [INFO] [1655951627.336889080] [example]: crop image to 960x540 pixels, time cost: 1 ms
[INFO] [crop_example-1]: process has finished cleanly [pid 3064]

根据log显示,测试程序完成了对本地1920x1080分辨率图片crop的处理,耗时如下:

图片处理运行耗时
1920x1080 crop出960x5401ms

本地原图1920x1080、crop左上角960x540区域,效果图片展示如下:

resize

功能介绍

实现图像缩放功能,暂时只支持NV12格式。

代码仓库:https://github.com/D-Robotics/hobot_cv

支持平台

平台运行方式
RDK X3, RDK X3 ModuleUbuntu 20.04 (Foxy), Ubuntu 22.04 (Humble)
RDK X5, RDK X5 Module, RDK S100Ubuntu 22.04 (Humble)
RDK UltraUbuntu 20.04 (Foxy)
X86Ubuntu 20.04 (Foxy)

准备工作

RDK平台

  1. RDK已烧录好Ubuntu 20.04/Ubuntu 22.04系统镜像。

  2. RDK已成功安装TogetheROS.Bot。

X86平台

  1. 确认X86平台系统为Ubuntu 20.04,且已成功安装TogetheROS.Bot。

使用介绍

RDK/X86

# 配置tros.b环境
source /opt/tros/setup.bash
# 从TogetheROS的安装路径中拷贝出运行示例需要的模型和配置文件。
cp -r /opt/tros/${TROS_DISTRO}/lib/hobot_cv/config/ .

# 启动launch文件
ros2 launch hobot_cv hobot_cv_resize.launch.py

结果分析

RDK X3平台resize

[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [resize_example-1]: process started with pid [3083]
[resize_example-1] [INFO] [1655951649.930987924] [example]:
[resize_example-1] source image config/test.jpg is 1920x1080 pixels
[resize_example-1] [INFO] [1655951649.931155799] [example]: resize image to 960x540 pixels, time cost: 297 ms
[resize_example-1] [INFO] [1655951650.039223757] [example]: resize image to 960x540 pixels, time cost: 15 ms
[INFO] [resize_example-1]: process has finished cleanly [pid 3083]

根据log显示,测试程序完成了对本地1920x1080分辨率图片resize的处理。接口调用两次,两次耗时分别如下。

图片处理第一次运行耗时第二次运行耗时
1920x1080 resize到960x540297ms15ms

因为第一次运行,需要对vps硬件进行配置所以耗时较多,如果不再更改硬件配置属性,硬件直接进行处理,耗时就会显著降低。

本地原图1920x1080、resize后960x540图片如下:

RDK X3平台性能对比

使用top命令查看cpu占用,cpu占用为测试进程cpu占比。 耗时统计单位为ms,循环处理1000次取平均值。 测试时锁定cpu频率:

sudo bash -c 'echo performance > /sys/devices/system/cpu/cpufreq/policy0/scaling_governor'
src wxhdst wxhvps耗时vps接口
cpu占用
bpu耗时bpu接口
cpu占用
opencv耗时opencv处理
cpu占用
512x512128x1281.5378925.91.11054891.71119100.3
640x640320x3202.4853628.51.82232881.82384338.9
896x896384x3844.5442224.62.8195479.77.84396273.1
1024x1024512x5126.0110325.23.8932581.72.55761381.7
1920x1088512x51211.040620.65.851371.18.19324380.1
1920x1080960x54411.156222.37.0908577.715.2978382.4

rotate

功能介绍

rotate实现图像旋转功能,暂时只支持NV12格式的图片,支持的旋转角度为90、180、270。

代码仓库:https://github.com/D-Robotics/hobot_cv

支持平台

平台运行方式示例功能
RDK X3, RDK X3 ModuleUbuntu 20.04 (Foxy), Ubuntu 22.04 (Humble)读取图片,进行旋转

准备工作

RDK平台

  1. RDK已烧录好Ubuntu 20.04/Ubuntu 22.04系统镜像。

  2. RDK已成功安装TogetheROS.Bot。

使用介绍

RDK平台

# 配置tros.b环境
source /opt/tros/setup.bash
# 从tros.b的安装路径中拷贝出运行示例需要的模型和配置文件。
cp -r /opt/tros/${TROS_DISTRO}/lib/hobot_cv/config/ .

# 启动launch文件
ros2 launch hobot_cv hobot_cv_rotate.launch.py

结果分析

[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [rotate_example-1]: process started with pid [3096]
[rotate_example-1] [INFO] [1655951661.173422471] [example]: rotate image 180 , time cost: 415 ms
[rotate_example-1]
[rotate_example-1] [INFO] [1655951661.416188013] [example]: second rotate image 180 , time cost: 40 ms
[rotate_example-1]
[INFO] [rotate_example-1]: process has finished cleanly [pid 3096]

根据log显示,测试程序完成了对本地1920x1080分辨率图片rotate的处理。接口调用两次,两次耗时分别如下。

图片处理第一次运行耗时第二次运行耗时
1920x1080旋转180度415ms40ms

因为第一次运行,需要对vps硬件进行配置所以耗时较多,如果不再更改硬件配置属性,硬件直接进行处理,耗时就会显著降低。

本地原图1920x1080、rotate后1920x1080图片如下:

hobot_cv与opencv处理性能对比

使用top命令查看cpu占用,cpu占用为测试进程cpu占比。 耗时统计单位为ms,循环处理1000次取平均值。 测试时锁定cpu频率:

sudo bash -c 'echo performance > /sys/devices/system/cpu/cpufreq/policy0/scaling_governor'
src wxh旋转角度hobotcv耗时hobotcv接口cpu占用opencv耗时opencv处理cpu占用
1920x10809037.656861.655.8886100.0
640x6401807.313366.85.1806100.0
896x89627014.772362.513.6497100.0

pyramid

功能介绍

实现图像金字塔缩放功能,暂时只支持NV12格式。

代码仓库:https://github.com/D-Robotics/hobot_cv

支持平台

平台运行方式示例功能
RDK X3, RDK X3 ModuleUbuntu 20.04 (Foxy), Ubuntu 22.04 (Humble)读取图片,进行金字塔缩放

准备工作

RDK平台

  1. RDK已烧录好Ubuntu 20.04/Ubuntu 22.04系统镜像。

  2. RDK已成功安装TogetheROS.Bot。

使用介绍

RDK平台

# 配置tros.b环境
source /opt/tros/setup.bash
# 从tros.b的安装路径中拷贝出运行示例需要的模型和配置文件。
cp -r /opt/tros/${TROS_DISTRO}/lib/hobot_cv/config/ .

# 启动launch文件
ros2 launch hobot_cv hobot_cv_pyramid.launch.py

结果分析

[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [pyramid_example-1]: process started with pid [3071]
[pyramid_example-1] [INFO] [1655951639.110992960] [example]: pyramid image , time cost: 299 ms
[pyramid_example-1]
[pyramid_example-1] [INFO] [1655951639.432398919] [example]: pyramid image , time cost: 19 ms
[pyramid_example-1]
[INFO] [pyramid_example-1]: process has finished cleanly [pid 3071]

根据log显示,测试程序完成了对本地1920x1080分辨率图片金字塔缩小的处理。接口调用两次,两次耗时分别如下。

图片处理第一次运行耗时第二次运行耗时
1920x1080金字塔六层基础层输出299ms19ms

因为第一次运行,需要对vps硬件进行配置所以耗时较多,如果不再更改硬件配置属性,硬件直接进行处理,耗时就会显著降低。

本地原图1920x1080、pyramid缩放后图片如下:

输出基础层六层,每一层size是上一层的1/2

性能对比

输入图片1920x1080,向上获取5层输出获取到960x540、480x270、240x134、120x66、60x32分辨率的图片。分别对比opencv和hobotcv效率,结果如下:

cpu占用为单核百分比,耗时统计单位为ms

VPS接口耗时vps接口cpu占比opencv耗时opencv接口cpu占比
19ms42.556100

color

功能介绍

实现图像的nv12,gbr24之间的转换。

代码仓库:https://github.com/D-Robotics/hobot_cv

支持平台

平台运行方式示例功能
RDK X5, RDK X5 Module, RDK S100Ubuntu 22.04 (Humble)nv12,gbr24之间的转换

准备工作

RDK平台

  1. RDK已烧录好Ubuntu 22.04系统镜像。

  2. RDK已成功安装TogetheROS.Bot。

使用介绍

RDK平台

# 配置tros.b环境
source /opt/tros/humble/setup.bash
# 从tros.b的安装路径中拷贝出运行示例需要的模型和配置文件。
cp -r /opt/tros/${TROS_DISTRO}/lib/hobot_cv/config/ .

# 启动launch文件
ros2 launch hobot_cv hobot_cv_conversion.launch.py

结果分析

[INFO] [launch]: All log files can be found below /root/.ros/log/2025-03-25-14-50-55-535138-ubuntu-4139
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [test_conersion-1]: process started with pid [4140]
[test_conersion-1] [INFO] [1742885455.683144151] [hobot_cv]: bgr24_to_nv12 opencv time cost: 4 ms
[test_conersion-1] [INFO] [1742885455.685469463] [hobot_cv]: nv12_to_bgr24 neon 1 time cost: 2 ms
[test_conersion-1] [INFO] [1742885455.836798125] [hobot_cv]: nv12_to_bgr24 neon 2 time cost: 2 ms
[test_conersion-1] [INFO] [1742885455.992973665] [hobot_cv]: bgr24_to_nv12 neon 1 time cost: 1 ms
[test_conersion-1] [INFO] [1742885455.997803043] [hobot_cv]: nv12_to_bgr24 opencv time cost: 4 ms
[test_conersion-1] [INFO] [1742885456.156813423] [hobot_cv]: bgr24_to_nv12 neon 2 time cost: 1 ms
[test_conersion-1] [INFO] [1742885456.161413872] [hobot_cv]: nv12_to_bgr24 opencv time cost: 4 ms
[INFO] [test_conersion-1]: process has finished cleanly [pid 4140]