读取PNG文件时opencv和skimage的区别
the difference between opencv and skimage while reading PNG files
在读取PNG
图像文件时,我测试了opencv
和skimage
的输入过程,发现输入图像的形状不同。是什么导致了这些差异,为什么 skimage
为 PNG
文件生成四个通道?
这里是代码段
from skimage.io import imread
image = imread("C:\Desktop\test1.png")
import cv2
img = cv2.imread("C:\Desktop\test1.png")
print("skimage shape: ",image.shape)
print("cv2 shape: ",img.shape)
输出是
skimage shape: (247, 497, 4)
cv2 shape: (247, 497, 3)
OpenCV 的 imread()
默认丢弃 alpha 通道(BGRA 中的第 4 个通道)。如果你想保留它,你需要使用IMREAD_UNCHANGED
标志:
IMREAD_UNCHANGED Python: cv.IMREAD_UNCHANGED
If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
skimage's imread()
没有这种行为。包含 Alpha 通道,如果存在:
The different color bands/channels are stored in the third dimension, such that a gray-image is MxN, an RGB-image MxNx3 and an RGBA-image MxNx4.
如评论中所述,使用默认选项 OpenCV 的 imread()
总是 returns 具有 BGR 颜色顺序的 3 通道图像(参见 IMREAD_COLOR
)。 skimage 的 imread()
对彩色图像使用 RGB(A) 顺序,并且可以 return 对灰度图像使用单通道 ndarray。
在读取PNG
图像文件时,我测试了opencv
和skimage
的输入过程,发现输入图像的形状不同。是什么导致了这些差异,为什么 skimage
为 PNG
文件生成四个通道?
这里是代码段
from skimage.io import imread
image = imread("C:\Desktop\test1.png")
import cv2
img = cv2.imread("C:\Desktop\test1.png")
print("skimage shape: ",image.shape)
print("cv2 shape: ",img.shape)
输出是
skimage shape: (247, 497, 4)
cv2 shape: (247, 497, 3)
OpenCV 的 imread()
默认丢弃 alpha 通道(BGRA 中的第 4 个通道)。如果你想保留它,你需要使用IMREAD_UNCHANGED
标志:
IMREAD_UNCHANGED Python: cv.IMREAD_UNCHANGED
If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
skimage's imread()
没有这种行为。包含 Alpha 通道,如果存在:
The different color bands/channels are stored in the third dimension, such that a gray-image is MxN, an RGB-image MxNx3 and an RGBA-image MxNx4.
如评论中所述,使用默认选项 OpenCV 的 imread()
总是 returns 具有 BGR 颜色顺序的 3 通道图像(参见 IMREAD_COLOR
)。 skimage 的 imread()
对彩色图像使用 RGB(A) 顺序,并且可以 return 对灰度图像使用单通道 ndarray。