【图像处理opencv】_numpy基本操作
目錄
1 三個(gè)重要屬性
2 創(chuàng)建矩陣
3 矩陣轉(zhuǎn)換
4 最大值、最小值、平均值
5 數(shù)學(xué)運(yùn)算
6 元素獲取
1 三個(gè)重要屬性
A.dtype :?data? type? 即A的數(shù)據(jù)類型,常見(jiàn)有float型、uint8型和float32型
A.shape :即A的形狀,比如2×2型,3×3型
A.ndim:n dimension 即A的維度,比如2維,3維等等?
2 創(chuàng)建矩陣
np.array([[1]])
np.uint([1])
np.arange(2,10,2)
np.linsapace(0,2*pi,100)
A=np.zeros((4,3),dtype=np.uint8)
B=np.ones((2,2),dtype=np.float32)
I=np.eye(4)
I2=np.identity(6)
C=np.random,randint(0,10,(4,4))
在jupyter編程中查看某個(gè)函數(shù)具體用法:光標(biāo)定位到該函數(shù)中,按快捷鍵Shift+Tab
np.arange()方法---創(chuàng)建一個(gè)規(guī)定步長(zhǎng)的矩陣
介紹:
Docstring: arange([start,] stop[, step,], dtype=None, *, like=None)Return evenly spaced values within a given interval.舉例
np.linspace() 方法---創(chuàng)建一個(gè)等間隔矩陣
介紹:
Signature: np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0) Docstring:Return evenly spaced numbers over a specified interval.舉例
?np.zeros()方法---創(chuàng)建0矩陣
介紹:
zeros(shape, dtype=float, order='C', *, like=None)Return a new array of given shape and type, filled with zeros.舉例:
?np.eye()方法---創(chuàng)建對(duì)角線為1的矩陣,1的位置可通過(guò)k的值來(lái)移動(dòng)
介紹
Signature: np.eye(N, M=None, k=0, dtype=<class 'float'>, order='C', *, like=None) Docstring: Return a 2-D array with ones on the diagonal and zeros elsewhere.?舉例
?np.identity()方法---創(chuàng)建一個(gè)單位矩陣
介紹:
Signature: np.identity(n, dtype=None, *, like=None) Docstring: Return the identity array.舉例:
?np.random.randint()方法---創(chuàng)建一個(gè)隨機(jī)矩陣
介紹
Docstring: randint(low, high=None, size=None, dtype=int)Return random integers from `low` (inclusive) to `high` (exclusive).舉例
3 矩陣轉(zhuǎn)換
A.reshape()
A.flatten(), A.ravel()
A.T
A.transpose()
np.hstack([A,B]) , np.vstack([A,B])
A.reshape()方法---改變矩陣形狀
介紹
Docstring: a.reshape(shape, order='C')Return s an array containing the same data with a new shape.舉例:
?A.flatten()方法---拉平矩陣(會(huì)導(dǎo)致矩陣降維成向量)
介紹Docstring: a.flatten(order='C')Return a copy of the array collapsed into one dimension.舉例:
?A.ravel() 方法---拉平矩陣,與A.flatten()區(qū)別在于flatten()是復(fù)制,而ravel()是引用
介紹
Docstring:a.ravel([order])Return a flattened array.舉例:
?B.T()方法---矩陣轉(zhuǎn)置
舉例:
?B.transpose()---矩陣轉(zhuǎn)置
介紹
Docstring: a.transpose(*axes)Returns a view of the array with axes transposed.舉例:
?np.hstack([A,B])方法---將A、B矩陣水平疊加
介紹:
Signature: np.hstack(tup) Docstring:Stack arrays in sequence horizontally (column wise).舉例:
?np.vstack([A,B])方法---將A,B矩陣垂直疊加
介紹:
Signature: np.vstack(tup) Docstring:Stack arrays in sequence vertically (row wise).舉例:
4 最大值、最小值、平均值
A.max()
A.min()
A.mean()
np.max()
np.min()
np.mean()
這個(gè)很好理解,直接看例子
對(duì)整個(gè)數(shù)組運(yùn)算:
對(duì)數(shù)組內(nèi)的某一行或某一列進(jìn)行運(yùn)算(行:axis=0,列:axis=1):
5 數(shù)學(xué)運(yùn)算
np.power(A,2)
np.sqrt()
np.log() , np.log2() , np.log10()
A*x , A@x
A.dot(x)
np.power(A,2)方法---將矩陣A內(nèi)的每個(gè)元素平方
舉例:
?np.sqrt(B)---將B矩陣開(kāi)方
舉例
?np.log() , np.log2() , np.log10()---對(duì)矩陣內(nèi)的每個(gè)元素做對(duì)數(shù)運(yùn)算(np.log()默認(rèn)以e為底)
舉例
?A* x---矩陣A與x對(duì)位元素相乘,注意:如果矩陣形狀不一樣,則會(huì)自動(dòng)復(fù)制元素不全成相同形狀的矩陣后再運(yùn)算
舉例:
?A@ x , A .dot(x)---矩陣A與矩陣x的矩陣乘法
舉例
6 元素獲取
直接看例子
這是B矩陣
?對(duì)B矩陣做下面操作
??
總結(jié)
以上是生活随笔為你收集整理的【图像处理opencv】_numpy基本操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: 【图像处理opencv】_Jupyter
- 下一篇: 【图像处理opencv】_Jupyter
