junpter notebook_numpy_2
numpy Notebook
2023-06-13 09:15:36 时间
import matplotlib.pyplot as plt
import numpy as np
### 两个图片的列合并
In [12]:
o=plt.imread('onr.jpg')
plt.imshow(o)
o.shape
Out[12]:
(456, 334, 3)
In [28]:
m=plt.imread('mj.jpg')
plt.imshow(m)
m.shape
Out[28]:
(480, 640, 3)
In [37]:
plt.imshow(m[100:130,300:350][::2,::2])
mm = m.copy()
plt.imshow(mm)
Out[37]:
<matplotlib.image.AxesImage at 0x1c84f866a58>
plt.imshow(np.concatenate([o[:,:],m[0:456,:]],axis=1))
### 注意:
### 图片二比图片一大,axis=1列对齐,行宽相等,取较小的图片的行数比例
### axis : 0 行合并(y) , 1 列合并(x) , -1 本维度的最后一个维度
### 马赛克
z = m[100:130,300:350][::5,::5]
plt.imshow(z)
In [78]:
z.shape
Out[78]:
(6, 10, 3)
In [62]:
plt.imshow(m[100:130,300:350])
Out[62]:
<matplotlib.image.AxesImage at 0x1c84f1bbb38>
### 图片据部马赛克
mm = m.copy()
for i in range(6):
for j in range(10):
mm[100+5*i:105+5*i,300+5*j:305+5*j]=m[100+5*i:105+5*i,300+5*j:305+5*j][::5,::5]
In [80]:
plt.imshow(mm)
Out[80]:
<matplotlib.image.AxesImage at 0x1c850f56668>
In [70]:
plt.imshow(m[0:200,200:400][::10,::10])
Out[70]:
<matplotlib.image.AxesImage at 0x1c84fc5c978>
In [75]:
z = m.copy()
for i in range(20):
for j in range(20):
z[0+10*i:10+10*i,200+10*j:210+10*j]=m[0+10*i:10+10*i,200+10*j:210+10*j][::10,::10]
In [76]:
plt.imshow(z)
Out[76]:
<matplotlib.image.AxesImage at 0x1c84fd6f978>
### 图片的替换
plt.imshow(o)
In [82]:
plt.imshow(o[200:280,110:230])
Out[82]:
<matplotlib.image.AxesImage at 0x1c851002cf8>
In [160]:
o[200:290,120:220].shape
Out[160]:
(90, 100, 3)
In [161]:
v = m.copy()
mm = v[100:130,300:350]
plt.imshow(mm)
Out[161]:
<matplotlib.image.AxesImage at 0x1c8536415c0>
In [162]:
o[200:290,120:220][::3,::2].shape
Out[162]:
(30, 50, 3)
In [163]:
oo = o[200:290,120:220][::3,::2]
oo.shape
Out[163]:
(30, 50, 3)
In [168]:
v[100:130,300:350]=o[200:290,120:220][::3,::2]
# mm = oo
In [169]:
plt.imshow(v)
Out[169]:
<matplotlib.image.AxesImage at 0x1c853745eb8>
重设形状
ndarray.reshape(tuple)
tuple是新形状的参数
元素的总量不能变化
In [134]:
lj.shape
Out[134]:
(483, 483, 3)
In [139]:
lj.size
Out[139]:
699867
In [140]:
483*483*3
Out[140]:
699867
In [143]:
lj.reshape((483*483*3,1))
Out[143]:
array([[35],
[36],
[31],
...,
[42],
[43],
[35]], dtype=uint8)
#矩阵的偏平化
In [153]:
lj.reshape(-1)
Out[153]:
array([35, 36, 31, ..., 42, 43, 35], dtype=uint8)
In [152]:
A = np.array([1,2,3,4,5,6,7,8,9,10])
A.reshape(-1,1)
Out[152]:
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10]])
ndarray.ravel() 偏平化
In [154]:
lj.ravel()
Out[154]:
array([35, 36, 31, ..., 42, 43, 35], dtype=uint8)
相关文章
- 在pycharm中导入torch_pycharm导入numpy出错
- python中dtype什么意思_NumPy Python中的数据类型对象(dtype)
- 各种转码(bytes、string、base64、numpy array、io、BufferedReader )
- numpy中randn_flattening
- numpy 中的randn()函数介绍「建议收藏」
- OpenCV-Python学习(15)—— OpenCV 图像旋转角度计算(NumPy 三角函数)
- 【Python数据科学库】Numpy从入门到精通
- Pytorch中tensor和numpy互相转换[通俗易懂]
- Matplotlib+Numpy绘图之基本操作
- python>>numpy(第二讲)
- 【错误记录】Mac 中 Python 报错 ( ERROR: Could not build wheels for numpy which use PEP 517 | 问题未解决 | 问题记录 )
- NumPy广播机制剖析
- Numpy切片基础操作详解