Search code examples
opencvimage-processingcomputer-visionscikit-image

the difference between opencv and skimage while reading PNG files


While reading the PNG image files, I tested the input process using both opencv and skimage, and found that the shape of the input image are different. What causes these differences, why skimage generate four channels for PNG file?

Here is the code segment

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)

The output are

skimage shape:  (247, 497, 4)
cv2 shape:  (247, 497, 3)

Solution

  • OpenCV's imread() discards alpha channel (4th channel in BGRA) by default. If you want to keep it, you need to use IMREAD_UNCHANGED flag:

    IMREAD_UNCHANGED Python: cv.IMREAD_UNCHANGED

    If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).

    skimage's imread() does not have this behavior. Alpha channel is included, if it exists:

    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.

    As noted in comments, with default options OpenCV's imread() always returns 3 channel images with BGR color order (see IMREAD_COLOR). skimage's imread() uses RGB(A) order for color images and can return a single channel ndarray for grayscale images.