当前位置:网站首页>OpenCV - Matrix Operations Part 3
OpenCV - Matrix Operations Part 3
2022-08-09 16:03:00 【why why】
OpenCV Comes with a large number of matrix processing functions,本文记录相关内容.
简介
- OpenCV The member functions of the matrix class can perform many basic matrix operations,本文基于 《学习 OpenCV3 》The content of Chapter 5 is organized Python OpenCV 矩阵操作函数.
内容列表
序号 | 函数 | 描述 |
|---|---|---|
1 | cv2.phase() | Calculates the orientation of a 2D vector |
2 | cv2.polarToCart() | Angle and magnitude are known,Find the corresponding two-dimensional vector |
3 | cv2.pow() | Raises each element in the matrix to the exponentiation |
4 | cv2.randu() | Fills the given matrix with uniformly distributed random numbers |
5 | cv2.randn() | Fills the given matrix with normally distributed random numbers |
6 | cv2.randShuffle() | Shuffle matrix elements randomly |
7 | cv2.reduce() | Reduce a two-dimensional matrix to a vector by specific operations |
8 | cv2.repeat() | Copies the contents of one matrix to another |
9 | cv2.setIdentity() | Set the elements on the diagonal in the matrix to be 1,其他置0 |
10 | cv2.solve() | 求出线性方程组的解 |
11 | cv2.solveCubic() | Find the real roots of the cubic equation |
12 | cv2.solvePoly() | Find the complex roots of a polynomial equation |
13 | cv2.sort() | Sorts the elements of any row or column in a matrix |
14 | cv2.sortIdx() | 与 cv2.sort() 的目的相同,Except the matrix is unmodified,并返回索引 |
15 | cv2.split() | Splits a multi-channel matrix into multiple single-channel matrices |
16 | cv2.sqrt() | Computes the element-wise square root of a matrix |
17 | cv2.subtract() | Implements element-wise subtraction of two matrices |
18 | cv2.trace() | Compute the trace of a matrix |
19 | cv2.transform() | Apply a matrix transformation on each element of the matrix |
20 | cv2.transpose() | 矩阵的转置运算 |
矩阵操作
0. 基础引用
- After that, we will demonstrate the above functions
- All code references the following packages by default
import cv2
import numpy as np
import mtutils as mt
from mtutils import PIS- 示例图片
img1.jpg和img2.jpg
- 查看 opencv 某函数
funcDocumentation works:
print(cv2.<func>.__doc__)1. cv2.phase()
Calculates the orientation of a 2D vector
- 函数使用
cv2.phase(x, y) --> dstCalculates the azimuth for a Cartesian-polar transformation for a two-dimensional vector field(角度)部分.The vector field is composed of two independent single-channel matrices.Of course the two input matrices are the same size.(If you have a two channel matrix,那么调用cv2.phase()will do what you need.)然后,dstEvery element in x和yCalculate the arctangent of the two in the corresponding elements of .
- 代码示例
x = np.array([1, 0, -1], dtype='float32')
y = np.array([1, -1, 1], dtype='float32')
res = cv2.phase(x, y)
-->
res
array([[0.7852316],
[4.712389 ],
[2.3563612]], dtype=float32)The above results are respectively \frac{\pi}{4}, \frac{3\pi}{2}, \frac{3\pi} { 4 }
2. cv2.polarToCart()
Angle and magnitude are known,Find the corresponding two-dimensional vector
- 函数使用
cv2.polarToCart(magnitude, angle, angleInDegrees=False) cv2.polarToCart() 从向量场的极坐标中计算笛卡尔坐标(x,y).Input two matrices of the same size and type:幅度和角度,Specify the magnitude and angle of the vector at each point.Output similar two matrices,They are the same size and type as the input,and will contain the vector at each pointx和y投影.附加标志angleInDegreeswill make the values in the angle matrix in degrees,而不是弧度.
- 代码示例
mag = np.array(2**0.5)
angle = np.array(np.pi/4)
res = cv2.polarToCart(mag, angle)
-->
res
(array([[1.]]), array([[1.]]))3. cv2.pow()
Raises each element in the matrix to the exponentiation
data = np.reshape(np.arange(20), [4, 5])
res = cv2.pow(data, 2)
-->
res
array([[ 0, 1, 4, 9, 16],
[ 25, 36, 49, 64, 81],
[100, 121, 144, 169, 196],
[225, 256, 289, 324, 361]], dtype=int32)4. cv2.randu()
Fills the given matrix with uniformly distributed random numbers 注意: The function is modified inline,The input data is directly transformed into a random matrix
- 函数使用
cv2.randu(data, low, high) Generated inline lowhigh A matrix of uniformly distributed random numbers between
- 示例代码
data = np.zeros([5, 5])
cv2.randu(data, low=2, high=3)
-->
data
array([[2.16117805, 2.03818934, 2.42586133, 2.84612762, 2.88760448],
[2.26471239, 2.27078305, 2.95266639, 2.6751313 , 2.81021636],
[2.18396021, 2.0944562 , 2.81590329, 2.52451766, 2.38083885],
[2.41199476, 2.25553534, 2.9448959 , 2.90012348, 2.45789156],
[2.84130697, 2.28030013, 2.3854357 , 2.45190146, 2.87543355]])5. cv2.randn()
Fills the given matrix with normally distributed random numbers 注意: The function is modified inline,The input data is directly transformed into a random matrix
- 函数使用
cv2.randn(data, mean, stddev) Generated inline with mean为均值,stddev A matrix of normally distributed random numbers with standard deviations
- 示例代码
data = np.zeros([5, 5])
cv2.randn(data, mean=5, stddev=1)
-->
data
array([[6.27751625, 6.43329835, 4.49584228, 3.92785478, 6.10956049],
[5.49447021, 4.10116905, 4.46068269, 4.88458967, 4.05134571],
[5.52288508, 5.11468363, 5.03454646, 4.02846879, 5.8627224 ],
[6.25682068, 4.23800135, 5.5566175 , 7.2824192 , 7.58638453],
[6.67061222, 4.90155111, 6.18168759, 3.01809192, 6.19408202]])6. cv2.randShuffle()
Shuffle matrix elements randomly
data = np.reshape(np.arange(25), [5, 5])
cv2.randShuffle(data)
-->
data
array([[14, 5, 1, 13, 2],
[23, 20, 21, 3, 8],
[22, 4, 16, 7, 6],
[18, 17, 19, 24, 15],
[10, 11, 9, 12, 0]])7. cv2.reduce()
Reduce a two-dimensional matrix to a vector by specific operations
- 函数使用
cv2.reduce(src, dim, reduceOp=cv2.REDUCE_SUM) --> vec简化是指使用一些reduceOp所代表的组合规则,对输入矩阵src的每一行(或列)system transformation,直到只剩一行(或列)为止,make it a vectorvec.参数dim决定如何进行简化.
- 参数说明
src
接受参数为 float 类型的一维、二维数据
reduceOp
参数 | 含义 |
|---|---|
cv2.REDUCE SUM | 计算向量的总和 |
cv2.REDUCE AVG | 计算向量的平均值 |
cv2.REDUCE MAX | 计算向量中的最大值 |
cv2.REDUCE MIN | 计算向量中的最小值 |
dim
参数 | 含义 |
|---|---|
0 | 合并为1行 |
1 | 合并为1列 |
- 示例代码
data = np.reshape(np.arange(25, dtype='float32'), [5, 5])
res = cv2.reduce(data, 0, rtype=cv2.REDUCE_SUM)
-->
res
array([[50., 55., 60., 65., 70.]], dtype=float32)8. cv2.repeat()
Copies the contents of one matrix to another
- 函数使用
cv2.repeat(src, nx, ny)将src 数据 x 方向重复 nx 次, y 方向重复 ny 次
- 示例代码
data = np.reshape(np.arange(4, dtype='float32'), [2, 2])
res = cv2.repeat(data, 2, 3)
-->
res
array([[0., 1., 0., 1., 0., 1.],
[2., 3., 2., 3., 2., 3.],
[0., 1., 0., 1., 0., 1.],
[2., 3., 2., 3., 2., 3.]], dtype=float32)9. cv2.setIdentity()
Set the elements on the diagonal in the matrix to be 1,其他置0
mtx = np.random.random([5,6])
res = cv2.setIdentity(mtx)
-->
res
array([[1., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.],
[0., 0., 0., 1., 0., 0.],
[0., 0., 0., 0., 1., 0.]])10. cv2.solve()
求出线性方程组的解
- 函数用法
cv2.solve(src1, src2, flags) - 其中 src1 为 A, src2 为 B
- flags
取值 | 含义 |
|---|---|
cv2.DECOMP_LU | 高斯消元法(LU分解) |
cv2.DECOMP_SVD | 奇异值分解(SVD) |
cv2.DECOMP_CHOLESKY | 对于对称正定矩阵 |
cv2.DECOMP_EIG | 特征值分解,只用于对称矩阵 |
cv2.DECOMP_OR | QR因式分解 |
cv2.DECOMP_NORMAL | 可选附加标志;表示要求解标准方程 |
The first five parameters in the table are mutually exclusive,But the last parametercv2.DECOMP_NORMALCan be combined with any of the first five(例如,通过逻辑OR).如果使用该参数,那么cv2.solve() 将尝试解决标准方程:hsT·Ihs·dst=lhsT·rhs, 而不是一般方程Ihs·dst=rhs.
- 示例代码
A = np.array([[1,2], [2,1]], dtype='float32')
B = np.array([[0], [1]], dtype='float32')
res = cv2.solve(A, B)
-->
res
(True, array([[ 0.6666667 ]...e=float32))
special variables
function variables
0:True
1:array([[ 0.6666667 ],
[-0.33333334]], dtype=float32)
special variables
[0:2] :[array([0.6666667], d...e=float32), array([-0.33333334],...e=float32)]
dtype:dtype('float32')Equation solved successfully, x = \frac {2}{3}, y=-\frac{1}{3}
11. cv2.solveCubic()
Find the real roots of the cubic equation
- 函数用法
cv2.solveCubic(coeffs)Given a cubic polynomial represented by three or four element vector coefficients,cv2.solveCubic()将计算该多项式的实根.如果coeffs有四个元素,则Compute the roots of the following polynomial:
如果coeffs只有三个元素,则Compute the roots of the following polynomial:
The returned result will have one or three elements,具体取决于多项式具有多少个实根.
- 示例代码
coeffs = np.array([1,1,-12,0], dtype='float32')
res = cv2.solveCubic(coeffs)
-->
res
(3, array([[-4.0000000e+...e=float32))
special variables
function variables
0:3
1:array([[-4.0000000e+00],
[ 3.0000000e+00],
[ 8.3266727e-16]], dtype=float32)
special variables
[0:3] :[array([-4.], dtype=float32), array([3.], dtype=float32), array([8.3266727e-16...e=float32)]
special variables
function variables得到 x3+x2-12x=0 three solutions {-4, 3, 0}
12. cv2.solvePoly()
Find the complex roots of a polynomial equation
- 函数使用
cv2.solvePoly(coeffs)给定以系数向量表示的任意阶数的多项式,cv2.solvePoly()将尝试计算该多项式的根.给定系数矩阵,Compute the roots of the following polynomial:
- 示例代码
coeffs = np.array([4, 0, 1], dtype='float32')
res = cv2.solvePoly(coeffs)
-->
res
(0.0, array([[[ 0., -2.]],...e=float32))
special variables
function variables
0:0.0
1:array([[[ 0., -2.]],
[[ 0., 2.]]], dtype=float32)
special variables
[0:2] :[array([[ 0., -2.]], ...e=float32), array([[0., 2.]], dt...e=float32)]
special variables解得 x^2+4=0 The solutions of the equations are imaginary numbers 2j,-2j
13. cv2.sort()
Sorts the elements of any row or column in a matrix
- 函数使用
cv2.sort(src, flags)可以通过使用cv2.SORT_EVERY_ROW或cv2.SORT_EVERY_COLUMN Flags to sort on each row or column.Sorting can be ascending or descending,分别由cv2.SORT_ASCENDING或cv2.SORT_DESCENDING标志指定.One logo from each group needs to be selected.
- 示例代码
data = cv2.randShuffle(np.reshape(np.arange(36), [6,6]))
res = cv2.sort(data, flags=cv2.SORT_EVERY_ROW)
-->
res
array([[ 1, 4, 8, 12, 30, 35],
[ 0, 5, 10, 11, 15, 22],
[ 3, 7, 16, 20, 25, 29],
[19, 21, 23, 24, 31, 34],
[ 9, 14, 18, 28, 32, 33],
[ 2, 6, 13, 17, 26, 27]], dtype=int32)14. cv2.sortIdx()
与
cv2.sort()的目的相同,Except the matrix is unmodified,并返回索引
data = cv2.randShuffle(np.reshape(np.arange(36), [6,6]))
res = cv2.sortIdx(data, flags=cv2.SORT_EVERY_ROW)
-->
res
array([[0, 1, 3, 2, 5, 4],
[3, 2, 4, 5, 1, 0],
[0, 2, 4, 5, 3, 1],
[1, 5, 3, 2, 0, 4],
[4, 1, 3, 0, 2, 5],
[1, 2, 0, 5, 4, 3]], dtype=int32)15. cv2.split()
Splits a multi-channel matrix into multiple single-channel matrices
image = mt.cv_rgb_imread('img1.jpg')
res = cv2.split(image)
PIS(image, *res)16. cv2.sqrt()
Computes the element-wise square root of a matrix
data = cv2.randShuffle(np.reshape(np.arange(16, dtype='float32'), [4, 4]))
res = cv2.sqrt(data)
-->
res
array([[3.3166249, 3. , 2.6457512, 1.4142135],
[2.4494898, 3.6055512, 2. , 1.7320508],
[3.4641016, 1. , 0. , 3.1622777],
[2.828427 , 3.7416575, 2.236068 , 3.8729835]], dtype=float32)17. cv2.subtract()
Implements element-wise subtraction of two matrices
mat_1 = np.ones([3,3])
mat_2 = np.zeros([3,3])
cv2.setIdentity(mat_2)
res = cv2.subtract(mat_1, mat_2)
-->
res
array([[0., 1., 1.],
[1., 0., 1.],
[1., 1., 0.]])18. cv2.trace()
Compute the trace of a matrix
mat_1 = np.ones([3,4], dtype='float32')
res = cv2.trace(mat_1)
-->
res
(3.0, 0.0, 0.0, 0.0)19. cv2.transform()
Apply a matrix transformation on each element of the matrix
- 函数使用
cv2.transform(src, mtx)函数cv2.transform()Can be used to compute arbitrary linear image transformations.它将多通道输入矩阵src视为向量的集合,你可以将其视为“channel space”,Then multiply these vectors by “小”矩阵mt×,以实现此channel space中的转换.
- 示例代码
image = mt.cv_rgb_imread('img1.jpg')
mtx = cv2.flip(cv2.setIdentity(np.zeros([3, 3], dtype='float32')), flipCode=0)
gbr = cv2.transform(image, mtx)
PIS(image, gbr)20. cv2.transpose()
矩阵的转置运算
mat = np.reshape(np.arange(16, dtype='float32'), [4, 4])
res = cv2.transpose(mat)
-->
res
array([[ 0., 4., 8., 12.],
[ 1., 5., 9., 13.],
[ 2., 6., 10., 14.],
[ 3., 7., 11., 15.]], dtype=float32)示例源码
参考资料
- 《学习 OpenCV3》 第五章
边栏推荐
猜你喜欢

leetcode 剑指 Offer 07. 重建二叉树

基于微信云开发的幼儿园招生报名小程序

Shell编程之正则表达式

【NodeJs篇】关于path 路径模块的学习和使用

A wave of Versailles: assault by the ali interview guide, I've got nine of the Offer

机器学习--数学库--概率统计

OpenSSF的开源软件风险评估工具:Scorecards

6大论坛,30+技术干货议题,2022首届阿里巴巴开源开放周来了!

Zero Time Technology | Nomad cross-chain bridge theft of 180 million US dollars incident analysis

Anaconda3安装后无法启动,启动闪退 2020-9
随机推荐
C语言程序设计笔记(浙大翁恺版) 第六周:数据类型
看完腾讯大佬90天整理的“Redis深度笔记”,我直接当场膜拜
benchmark性能测试
shell------常用小工具,sort,uniq,tr,cut
启动报错:Caused by: org.apache.ibatis.binding.BindingException汇总解决
【原神】手机版原神下错版本不能登录怎么办?B服修改为官服
Computational Imaging Technology
Detailed firewall firewall
DMPE-PEG-Mal Maleimide-PEG-DMPE 二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺
结合实际聊聊电平转换电路(常用电平转换电路总结)
运维--常用中间件
leetcode 剑指 Offer 17. 打印从1到最大的n位数
思维导图FreeMind安装问题及简单使用
Mongodb增加权限管理
985测试工程师被吊打,学历和经验到底谁更重要?
DSPE-PEG-Hydrazide,DSPE-PEG-HZ,磷脂-聚乙二醇-酰肼MW:1000
参考文献格式
正则表达式实战:最新豆瓣top250爬虫超详细教程
After reading the "Redis In-depth Notes" compiled by Tencent bosses in 90 days, I worshipped on the spot.
[LeetCode] 485.最大连续 1 的个数