存储在numpy数组中的图像中像素的方向是什么?从左到右还是从上到下?

时间:2019-12-29 14:18:11

标签: python image numpy python-imaging-library

我试图理解Python中的图像处理。

因此,我将以下31 x 31像素JPG图像存储在numpy数组中。

enter image description here

from PIL import Image
import numpy as np

sample = Image.open('datasets/1.jpg')
sample_arr = np.array(sample)

现在,

sample_arr.shape

返回

(31, 31, 3)

sample_arr[0].shape

返回

(31, 3)

据此,我了解到sample_arr [0]存储前31个像素值,sample_arr [1]存储后31个像素值,依此类推,直到sample_arr [30]存储最后31个像素值。因此,我们存储在数组中的像素总数= 31 x 31 = 961

我的问题是,相对于我们看到的图像,数组元素的顺序是什么? sample_arr [0]是否存储从图像的最左上角向右的前31个像素?

1 个答案:

答案 0 :(得分:1)

TL; DR 的原点在左上角,第一个索引是行,第二个索引是列,第三个是颜色通道(R,G,B顺序)。

sample对象创建的numpy数组包含按行,列顺序排列的像素。

让我们在示例图像上进行检查:

This is a 2×4 binary image (magnified ×25)

此图像具有height=2width=4,除左上角(白色value=0)以外,所有像素均为黑色(value=255)。它存储为

[[255   0   0   0]
 [  0   0   0   0]]

如果引入RGB颜色,则每个像素分别由3个值表示,分别是红色,绿色和蓝色强度。

enter image description here

[[[255   0   0]
  [  0 255   0]
  [  0   0 255]
  [  0   0   0]]
 [[  0   0   0]
  [  0   0   0]
  [  0   0   0]
  [  0   0   0]]]