当前位置:网站首页>OpenCV DNN blogFromImage()详解
OpenCV DNN blogFromImage()详解
2022-08-01 23:52:00 【New_Tank】
OpenCV3.x版本,增加了DNN模块,支持直接载入深度学习框架训练好的模型。如何往模型中传入数据,这就要查看blogFromImage()函数。

1. 背景知识:
为了从深度神经网络获取正确的预测,首先需要预处理数据。把图像数据处理为模型所需要的数据格式。这些预处理通常包括:
A. 均值减法(Mean Subtraction)。即整体像素减去平均值。
B. 缩放(Scaling)。通过缩放系数(scalefactor)对图像像素值进行缩放。
C. 把数据shape调整为模型需要的格式。
均值减法:
均值减法用来适应光照的变换(combat illumination changes)。因此,我们可以将均值减法视为一种协助卷积神经网络的技术。在开始训练我们自己的深度神经网络之前,需要计算我们要训练的图片中RGB三个通道的平均像素(average pixel)
2.具体使用:
Python:
cv.dnn.blobFromImage(image[, scalefactor[, size[, mean[, swapRB[, crop[, ddepth]]]]]]) ->retval
C++:
| Mat cv::dnn::blobFromImage | ( | InputArray | image, |
| double | scalefactor = 1.0, | ||
| const Size & | size = Size(), | ||
| const Scalar & | mean = Scalar(), | ||
| bool | swapRB = false, | ||
| bool | crop = false, | ||
| int | ddepth = CV_32F | ||
| ) |
参数:
| image | input image (with 1-, 3- or 4-channels).将要输入神经网络进行处理或者分类的图片 |
| size | spatial size for output image 这个参数是我们神经网络在训练的时候要求输入的图片尺寸 |
| mean | scalar with mean values which are subtracted from channels. Values are intended to be in (mean-R, mean-G, mean-B) order if 需要将图片整体减去的平均值,如果我们需要对RGB图片的三个通道分别减去不同的值,那么可以使用3组平均值,如果只使用一组,那么就默认对三个通道减去一样的值。减去平均值(mean):为了消除同一场景下不同光照的图片,对我们最终的分类或者神经网络的影响,我们常常对图片的R、G、B通道的像素求一个平均值,然后将每个像素值减去我们的平均值,这样就可以得到像素之间的相对值,就可以排除光照的影响。 |
| scalefactor | multiplier for 当我们将图片减去平均值之后,还可以对剩下的像素值进行一定的尺度缩放,它的默认值是1,如果希望减去平均像素之后的值,全部缩小一半,那么可以将scalefactor设为1/2 |
| swapRB | flag which indicates that swap first and last channels in 3-channel image is necessary. OpenCV中的图片通道顺序是BGR,但是有时平均值假设的顺序是RGB,所以如果需要交换R和G,那么就要使swapRB=true |
| crop | flag which indicates whether image will be cropped after resize or not |
| ddepth | Depth of output blob. Choose CV_32F or CV_8U. |
输出:
Returns
4-dimensional Mat with NCHW dimensions order.
例子:
import cv2
import numpy as np
import sys
#Sam add it. load mobilenet model.
image = cv2.imread("E:\\current\\svn_Python_home\\OpenCV_Study\\Example\\DNN_Sample\\Images\\voc_boat_pi.jpg")
if(image is None):
sys.exit(0)
net =cv2.dnn.readNetFromTensorflow("E:\current\svn_Python_home\OpenCV_Study\Example\DNN_Sample\models\mobilenet_v1_1.0_224_frozen.pb")
image = cv2.resize(image, (300,300))
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0))
print("blob shape:", blob.shape)
print("Success")blob shape: (1, 3, 300, 300)
边栏推荐
- 使用Ganache、web3.js和remix在私有链上部署并调用合约
- Various Joins of Sql
- TexturePacker使用文档
- contentEditable属性
- Department project source code sharing
- 【MySQL系列】 MySQL表的增删改查(进阶)
- Enterprise firewall management, what firewall management tools are there?
- 切面打印调取的方法
- Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections
- asyncawait和promise的区别
猜你喜欢

1个月写900多条用例,二线城市年薪33W+的测试经理能有多卷?

Bean的生命周期

oozie startup error on cdh's hue, Cannot allocate containers as requested resource is greater than maximum allowed

使用Jenkins做持续集成,这个知识点必须要掌握

Work for 5 years, test case design is bad?To look at the big case design summary

在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错

20220725 Information update

Quartus uses tcl files to quickly configure pins

Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections

GIF制作-灰常简单的一键动图工具
随机推荐
Quartus 使用 tcl 文件快速配置管脚
数据机构---第五章树与二叉树---二叉树的概念---应用题
asyncawait和promise的区别
Loading configuration of Nacos configuration center
Department project source code sharing
2022还想上岸学习软件测试必看,测试老鸟的肺腑之言...
sys_kill system call
在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决
字节跳动面试官:请你实现一个大文件上传和断点续传
分享一份接口测试项目(非常值得练手)
【Leetcode】470. Implement Rand10() Using Rand7()
FAST-LIO2 code analysis (2)
使用Ganache、web3.js和remix在私有链上部署并调用合约
【MySQL系列】MySQL数据库基础
如何用Redis实现分布式锁?
C language - branch statement and loop statement
获取小猪民宿(短租)数据
[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
1个月写900多条用例,二线城市年薪33W+的测试经理能有多卷?
经典文献阅读之--DLO