题
将图像加载到Python中,如下所示,我如何知道通道的顺序? (例如BGR或RGB)
码
from PIL import Image
import numpy as np
image_pil = Image.open("Stonehenge.jpg")
image_np = np.array(image_pil)
image_np[0][0]
结果
array([ 52, 123, 155], dtype=uint8)
具体问题
我怎么知道52是对应红色通道,蓝色通道还是不同的通道?或者这个问题在概念层面上没有意义吗?
笔记
在类似于Java而不是Python的问题中,one person claims:
If you are reading in the image file, or you have access to the code
that reads in the file, know it is:
BGR order if you used cv2.imread(),
RGB order if you used mpimg.imread(), (assuming import matplotlib.image as mpimg)
If you don’t know how the file was opened, the accepted answer
BufferedImage is great for Java.
解决方法:
由于您使用PIL并且未指定任何其他模式来加载Image,因此您获得R G B.
您可以通过检查Image实例上的“mode”属性来验证: