使用numpy實(shí)現(xiàn)矩陣的翻轉(zhuǎn)(flip)與旋轉(zhuǎn)
numpy.flip(m, axis=None)
Reverse the order of elements in an array along the given axis.
The shape of the array is preserved, but the elements are reordered.
把m在axis維度進(jìn)行切片,并把這個(gè)維度的index進(jìn)行顛倒
示例
隨機(jī)生成一個(gè)二維數(shù)組
import numpy as np a=np.random.randint(1,9,size=9).reshape((3,3))
[[5 8 6]
[3 1 7]
[8 7 8]]
axis=0:上下翻轉(zhuǎn),意味著把行看成整體,行的順序發(fā)生顛倒,每一行的元素不發(fā)生改變
print(np.flip(a,axis=0))
[[8 7 8]
[3 1 7]
[5 8 6]]
axis=1:左右翻轉(zhuǎn),意味著把列看成整體,列的順序發(fā)生顛倒,每一列的元素不發(fā)生改變
print(np.flip(a,axis=1))
[[6 8 5]
[7 1 3]
[8 7 8]]
Numpy矩陣的旋轉(zhuǎn)
使用skimage.io讀出來(lái)的圖片是numpy.darray格式,掌握numpy矩陣的旋轉(zhuǎn)與翻轉(zhuǎn),可實(shí)現(xiàn)數(shù)據(jù)增廣(data augmentation)。
可用rot90函數(shù)實(shí)現(xiàn),例子如下:
import numpy as np mat = np.array([[1,3,5], [2,4,6], [7,8,9] ]) print mat, "# orignal" mat90 = np.rot90(mat, 1) print mat90, "# rorate 90 <left> anti-clockwise" mat90 = np.rot90(mat, -1) print mat90, "# rorate 90 <right> clockwise" mat180 = np.rot90(mat, 2) print mat180, "# rorate 180 <left> anti-clockwise" mat270 = np.rot90(mat, 3) print mat270, "# rorate 270 <left> anti-clockwise"
如果mat是圖片,那么可視化效果更好。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持本站。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信