输出被截断(img.sum(axis = 2)

时间:2018-12-12 16:34:40

标签: python

您能解释一下轴2的一部分吗? img.sum(axis = 2)

RGB输出如何被截断?谢谢

输出:     (480,480,3)     (480,480)

# Load the image into an array: img
img = plt.imread('some_random_image.jpg')

# Print the shape of the image
print(img.shape)

# Compute the sum of the red, green and blue channels: intensity
intensity = img.sum(axis=2) 

# Print the shape of the intensity
print(intensity.shape)

# Display the intensity with a colormap of 'gray'
plt.imshow(intensity,cmap='gray')

# Add a colorbar
plt.colorbar()

# Hide the axes and show the figure
plt.axis('off')
plt.show()

1 个答案:

答案 0 :(得分:1)

我不太确定我是否理解您的问题,但这是我可以告诉您的:

public void keyPressed(KeyEvent e)//Could be keyReleased or keyTyped as well { if (e.getKeyCode()==38)//38 is code for up button { up(); } else if (e.getKeyCode()==40)//40 is code of down button { down(); } 表示您正在对数组sum(axis=2)的第二个元素进行求和(等于[480,480,3]

因此img.shape将对 sum(axis=2)

因此,您将获得一个形状为(480,480)的数组,其中每个元素均等于:

img[:, :, 0] + img[:, :, 1] + img[:, :, 2]

您现在只有2D数组,因为默认情况下np.sum函数会减小数组的大小

如果要保留3D阵列,请执行img[i, j] = img[i, j, 0] + img[i, j, 1] + img[i, j, 2]