打印 RGB 通道

Printing RGB channels

如果我有 RGB 图像,即:img_RGB,我打印其中一个通道。执行 print(img_RGB[:,:,2])print(img_RGB[:,:,1]) 时究竟有什么区别? 因为我试过了,我得到了相同的矩阵。据我所知,我正在打印蓝色通道的值,但是我不确定在使用 '1''2'

时打印矩阵会有什么不同

正在使用的图片: [1]: https://i.stack.imgur.com/dKIf4.jpg

对于你的图像,似乎大部分像素在所有通道中都具有相同的值(至少在 BG 中),这就是为什么在打印时你看不到差异,因为不同值的数量太少了。我们可以通过以下方式检查:

>>> img = cv2.imread(fname, -1);img_RGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
>>> img_RGB[:,:,2] == img_RGB[:,:,1]

array([[ True,  True,  True, ...,  True,  True,  True],
       [ True,  True,  True, ...,  True,  True,  True],
       [ True,  True,  True, ...,  True,  True,  True],
       ...,
       [ True,  True,  True, ...,  True,  True,  True],
       [ True,  True,  True, ...,  True,  True,  True],
       [ True,  True,  True, ...,  True,  True,  True]])

看到这个结果,可能有人会说人人平等,但仔细一看,却不是这样的:

>>> (img_RGB[:,:,2] == img_RGB[:,:,1]).all()
False

# So there are some values that are not identical
# Let's get the indices

>>> np.nonzero(img_RGB[:,:,2] != img_RGB[:,:,1])
(array([  16,   16,   16, ..., 1350, 1350, 1350], dtype=int64),
 array([  83,   84,   85, ..., 1975, 1976, 1977], dtype=int64))

# So these are the indices, where :
# first element of tuple is indices along axis==0
# second element of tuple is indices along axis==1

# Now let's get values at these indices:
>>> img_RGB[np.nonzero(img_RGB[:,:,2] != img_RGB[:,:,1])]
#        R    G    B
array([[254, 254, 255],
       [252, 252, 254],
       [251, 251, 253],
       ...,
       [144, 144, 142],
       [149, 149, 147],
       [133, 133, 131]], dtype=uint8)
# As can be seen, values in `G` and `B` are different in these, essentially `B`.
# Let's check for the first index, `G` is:
>>> img_RGB[16, 83, 1]
254
# And `B` is:
>>> img_RGB[16, 83, 1]
255

因此打印形状为 (1351, 1982) 的图像数组不是检查差异的好主意。