当前位置:网站首页>OpenCV DNN blogFromImage() detailed explanation
OpenCV DNN blogFromImage() detailed explanation
2022-08-02 00:07:00 【New_Tank】
OpenCV3.x版本,增加了DNN模块,Supports direct loading of models trained by deep learning frameworks.How to pass data into the model,This is to seeblogFromImage()函数.

1. 背景知识:
To get correct predictions from deep neural networks,The data needs to be preprocessed first.Process the image data into the data format required by the model.These preprocessing usually include:
A. 均值减法(Mean Subtraction).That is the overall pixel minus the mean.
B. 缩放(Scaling).通过缩放系数(scalefactor)Scales image pixel values.
C. 把数据shapeAdjust to the format required by the model.
均值减法:
Mean subtraction is used to adapt to lighting transformations(combat illumination changes).因此,We can think of mean subtraction as a technique to assist convolutional neural networks.Before starting to train our own deep neural network,Need to count the images we want to train onRGBAverage pixels of the three channels(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).The image that will be fed into the neural network for processing or classification |
| 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. OpenCVThe picture channel order in is BGR,But sometimes the order of the mean assumptions isRGB,所以如果需要交换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)
边栏推荐
- Chapter 19 Tips and Traps: Common Goofs for Novices
- Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
- asyncawait和promise的区别
- Chapter 11 Working with Dates and Times
- LeetCode_518_零钱兑换Ⅱ
- 技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
- Flink学习第五天——Flink可视化控制台依赖配置和界面介绍
- 深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
- 带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
- Flink Yarn Per Job - 提交流程一
猜你喜欢

Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions

ES中SQL查询详解

Making a Simple 3D Renderer

【图像融合】基于加权和金字塔实现图像融合附matlab代码

The Spark of Sql join on the and and where

WEB安全基础 - - - XRAY使用

带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?

GetHashCode与Equals

如何重装Win11?一键重装Win11方法

Excel导入和导出
随机推荐
云原生DevOps环境搭建
recursion: method calls itself
【MySQL系列】MySQL数据库基础
Secondary Vocational Network Security Competition B7 Competition Deployment Process
Classical Literature Reading--DLO
Excel文件读写(创建与解析)
Leetcode 129求根节点到叶节点数字之和、104二叉树的最大深度、8字符串转换整数(atoi)、82删除排序链表中的重复元素II、204二分查找、94二叉树的中序遍历、144二叉树的前序遍历
在CentOS下安装MySQL
contentEditable属性
【Leetcode】1206. Design Skiplist
Chapter 11 Working with Dates and Times
Share an interface test project (very worth practicing)
C语言七夕来袭!是时候展现专属于程序员的浪漫了!
2022还想上岸学习软件测试必看,测试老鸟的肺腑之言...
yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
numpy.isclose
background-image使用
深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练
nodejs--process
QML包管理