题
将图像加载到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”属性来验证:

本文介绍了如何在Python中使用PIL库加载图像后,确定图像通道的顺序,特别是RGB或BGR。通过检查`Image`对象的`mode`属性可以得知通道顺序。如果直接转换为numpy数组,不考虑通道顺序可能会导致颜色显示错误。
最低0.47元/天 解锁文章

6898

被折叠的 条评论
为什么被折叠?



