iPython imshow()或numpy行选择

时间:2016-10-20 03:46:27

标签: python numpy ipython imshow

我正在尝试使用imshow()绘制,只绘制我所拥有的图像的中间一百行。我想知道是否有任何numpy命令只能切割我的图像数组的中间一百行。如果没有,我可以在imshow()本身上使用一些变体来选择并只显示中间的几百行吗?

1 个答案:

答案 0 :(得分:0)

您正在寻找的是:

pic[np.shape(pic)[0]/2-50:np.shape(pic)[0]/2+50,np.shape(pic)[1]/2-50:np.shape(pic)[1]/2+50]

示例代码:

import numpy as np
import matplotlib.pyplot as plt

pic = np.random.rand(300,300)
fig1 = plt.figure()
fig1.suptitle('Full image')
plt.imshow(pic)
cropped = pic[np.shape(pic)[0]/2-50:np.shape(pic)[0]/2+50,np.shape(pic)[1]/2-50:np.shape(pic)[1]/2+50]
fig2 = plt.figure()
fig2.suptitle('middle 100 rows and column cropped')
plt.imshow(cropped)
plt.show()

结果:

enter image description here enter image description here