简体   繁体   English

Matplotlib:缺少使用imread的频道

[英]Matplotlib: Missing channel using imread

When I try to load an image that has three channels with matplotlib it only has one channel when I issue the numpy shape command. 当我尝试使用matplotlib加载具有三个通道的图像时,在发出numpy shape命令时它只有一个通道。 This shows the following image: 这显示了以下图像:

matplotlib的一通道图像

Here is the code I used: 这是我使用的代码:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

img = mpimg.imread('dolphin.png')
plt.imshow(img)
plt.show()

img.shape
(320, 500)

I also followed the matplotlib image tutorial which uses the same commands as above. 我还遵循了matplotlib图像教程 ,该教程使用与上述相同的命令。

Loading the image with opencv the result is an image with three channels, as expected. 用opencv加载图像,结果是具有三个通道的图像,与预期的一样。

import cv2
imgcv = cv2.imread('dolphin.png')
plt.imshow(imgcv)
plt.show()

imgcv.shape
(320, 500, 3)

带cv2的三通道图像

I am using Python 3.5.6 with anaconda. 我在蟒蛇上使用Python 3.5.6。

Here is a short output of the conda list command: 这是conda list命令的简短输出:

...
matplotlib                3.0.0
...
opencv3                   3.1.0
...
pillow                    5.2.0
...

The original image I used: 我使用的原始图像:

原始的海豚图像

Am I missing a package or is there another command to load a *.png file? 我是否缺少软件包或是否有另一个命令来加载* .png文件? Everything seems to work with *.jpg images 一切似乎都适用于* .jpg图像

As I see it, matplotlib's imread correctly reads in the image. 如我所见,matplotlib的imread正确读取图像。 If the image contains only a single channel, the resulting numpy array will be 2D. 如果图像仅包含一个通道,则生成的numpy数组将为2D。 If the image contains 3 or 4 channels, the numpy array will be 3D. 如果图像包含3个或4个通道,则numpy数组将为3D。

Taking the dolphin image from the question you get 从您得到的问题中获取海豚图像

plt.imread("https://i.stack.imgur.com/cInHj.png").shape
> (320, 500)

Concerning the stinkbug image from the matplotlib documentation there is indeed a little problem. 关于matplotlib文档中的stinkbug映像,确实存在一个小问题。 The image you see is a grey scale image as well, 您看到的图像也是灰度图像,

plt.imread("https://matplotlib.org/_images/stinkbug.png").shape
> (375, 500)

However the tutorial claims it to be a 3 channel image. 但是本教程声称它是3通道图像。 This is correct from the point of view of the tutorial, because it takes the image from the doc on the github repository folder. 从本教程的角度来看,这是正确的,因为它从github存储库文件夹中的doc获取图像。

plt.imread("https://raw.githubusercontent.com/matplotlib/matplotlib/master/doc/_static/stinkbug.png").shape
> (375, 500, 3)

The problem is that the documentation is built through sphinx and sphinx-gallery and in addition may use some other libraries. 问题在于该文档是通过sphinx和sphinx-gallery构建的,此外还可能使用其他一些库。 In the course of this, the image is not copied in its raw format to the output folder. 在此过程中,图像不会以其原始格式复制到输出文件夹。 This problem has been reported already here , the reason is not yet fully tracked down. 此问题已在此处报告,原因尚未完全找到。

In any case, the remaining open question is then, why does cv2.imread give you a 3D array for a greyscale image? 无论如何,剩下的问题是,为什么cv2.imread为您提供3D灰度图像阵列?

From the OpenCV imread documentation : OpenCV imread文档

Second argument is a flag which specifies the way image should be read. 第二个参数是一个标志,用于指定应读取图像的方式。

  • cv2.IMREAD_COLOR : Loads a color image. cv2.IMREAD_COLOR:加载彩色图像。 Any transparency of image will be neglected. 图像的任何透明度都将被忽略。 It is the default flag. 这是默认标志。
  • cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode cv2.IMREAD_GRAYSCALE:以灰度模式加载图像
  • cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel cv2.IMREAD_UNCHANGED:加载图像,包括alpha通道

Note Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively. 注意除了这三个标志,您可以分别简单地传递整数1、0或-1。

So here you need to specify yourself, which mode you want to use. 因此,在这里您需要指定自己的使用方式。

Let's verify: 让我们验证一下:

import cv2
import urllib.request as req

dolphinurl ="https://i.stack.imgur.com/cInHj.png"
stinkbugweburl = "https://matplotlib.org/_images/stinkbug.png"
stinkbuggiturl = "https://raw.githubusercontent.com/matplotlib/matplotlib/master/doc/_static/stinkbug.png"

def printshape(url, **kw):
    req.urlretrieve(url, "image_name.png")
    im = cv2.imread("image_name.png", **kw)
    print(im.shape)

printshape(dolphinurl)
printshape(stinkbugweburl)
printshape(stinkbugweburl)

This prints 此打印

(320, 500, 3)
(375, 500, 3)
(375, 500, 3)

while if you specify greyscale, 而如果您指定灰度,

printshape(dolphinurl,0)
printshape(stinkbugweburl,0)
printshape(stinkbugweburl,0)

it'll print 它会打印

(320, 500)
(375, 500)
(375, 500)

In that sense it's up to the user to decide how they want to read in the image. 从这种意义上说,取决于用户来决定他们如何阅读图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM