【问题标题】:Unable to load image using imread()无法使用 imread() 加载图像
【发布时间】:2018-05-13 16:28:46
【问题描述】:

我不确定为什么会发生这种情况,但我无法使用 imread() 加载图像。我能够在绘画中打开该图像,并在保存该图像后,正在加载和显示该图像。我正在使用 Jupyter 笔记本。

import os
import cv2
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline

def displayImage(image):
 plt.imshow(image)
 plt.show()

image = cv2.imread('path/to/image')
displayImage(image)

输出

预期输出:

【问题讨论】:

  • 照片的链接对我不起作用。我只看到黑屏
  • 为什么要用 matplotlib 显示 OpenCV 图像?请改用cv2.imshow()
  • @zindarod,cv2返回一个numpy数组,matplotlib是一个完美的图像展示,避免了imshow的一些问题
  • @MartinBeckett OpenCV 将图像读取为 BGR,而 matplotlib 将它们读取为 RGB。这就是 OP 代码的问题。
  • @zindarod 实际上红色、绿色或蓝色通道中没有任何内容 :-) 请参阅下面的答案。

标签: python opencv image-loading


【解决方案1】:

这是因为您的图像处于RGBA 模式(您的背景是透明的)。 所以你需要在RGBA模式下读取你的图像:

image = cv2.imread('path/to/image.png',-1)

或:

from scipy.ndimage import imread
rgba = imread('path/to/image.png', mode='RGBA')

结果:

【讨论】:

  • 谢谢萨尔曼。我可以使用您的解决方案读取图像。
  • 我为我的评论道歉——我真的没有恶意的意思。我已经撤回了。
【解决方案2】:

问题是您的图像不包含任何非零红色、绿色或蓝色像素,它完全是黑色的。它看起来如何使用"@ @ 6 L" 显示它的唯一原因是因为它有一个 alpha/透明通道,可以掩盖黑色并显示白色 PNG 背景颜色。

如果您使用 ImageMagick 的 identify 查看它,您会看到:

identify -verbose a.png | more
Image: a.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 203x50+0+0
  Resolution: 37.79x37.79
  Print size: 5.37179x1.3231
  Units: PixelsPerCentimeter
  Colorspace: sRGB
  Type: Bilevel
  Base type: Undefined
  Endianess: Undefined
  Depth: 8-bit
  Channel depth:
    Red: 1-bit
    Green: 1-bit
    Blue: 1-bit
    Alpha: 8-bit
  Channel statistics:
    Pixels: 10150
    Red:
      min: 0  (0)
      max: 0 (0)                 <--- Brightest Red is zero
      mean: 0 (0)
      standard deviation: 0 (0)
      kurtosis: -3
      skewness: 0
      entropy: 0
    Green:
      min: 0  (0)
      max: 0 (0)                 <--- Brightest Green is zero
      mean: 0 (0)
      standard deviation: 0 (0)
      kurtosis: -3
      skewness: 0
      entropy: 0
    Blue:
      min: 0  (0)
      max: 0 (0)                  <--- Brightest Blue is zero
      mean: 0 (0)
      standard deviation: 0 (0)
      kurtosis: -3
      skewness: 0
      entropy: 0
    Alpha:
      min: 0  (0)
      max: 255 (1)                         <--- Alpha channel is only one with info
      mean: 16.477 (0.0646159)
      standard deviation: 58.73 (0.230314)
      kurtosis: 10.7342
      skewness: 3.50997
      entropy: 0.128008
   ...
   ...
   Background color: white       <--- Background is white
   ...
   ...

答案是用cv2.IMREAD_UNCHANGED 读取所有四个通道,然后只使用第四个/alpha 通道:

def read_transparent_png(filename):
image_4channel = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
alpha_channel = image_4channel[:,:,3]
rgb_channels = image_4channel[:,:,:3]

here中提取的代码。

【讨论】:

  • 对不起,马克,我没有看到你的答案,但我认为责备别人抄袭你的答案不是一个好方法
【解决方案3】:

并检查实际加载的数据。使用 image.shape() 检查大小,查看最大/最小/平均值,或者如果您使用 spyder(强烈推荐),请查看变量查看器中的数据。

ps。对于单个显示项,不需要plt.show() 命令

【讨论】:

    【解决方案4】:

    加载图片后使用:

    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    

    【讨论】:

      猜你喜欢
      • 2020-07-23
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      • 2018-09-24
      • 2013-07-11
      相关资源
      最近更新 更多