cv2.imread does not read jpg files

后端 未结 1 1864
[愿得一人]
[愿得一人] 2020-12-03 19:36

I am working on a toolbox in Python where I use cv2.imread function to load images.

While I am working with .png files it is OK, but it re

相关标签:
1条回答
  • 2020-12-03 20:02

    Something is off in your build of cv2. Rebuild it from source, or get it from the package manager.

    As a workaround, load jpeg files with matplotlib instead:

    >>> import cv2
    >>> import matplotlib.pyplot as plt
    >>> a1 = cv2.imread('pic1.jpg')
    >>> a1.shape
    (286, 176, 3)
    >>> a2 = plt.imread('pic1.jpg')
    >>> a2.shape
    (286, 176, 3)
    

    Note that opencv and matplotlib read the colour channels differently by default (one is RGB and one is BGR). So if you rely on the colours at all, you had better swap the first and third channels, like this:

    >>> a2 = a2[..., ::-1]  # RGB --> BGR
    >>> (a2 == a1).all()
    True
    

    Other than that, cv2.imread and plt.imread should return the same results for jpeg files. They both load into 3-channel uint8 numpy arrays.

    0 讨论(0)
提交回复
热议问题