Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python opencv加载的图像错误_Python_Opencv - Fatal编程技术网

Python opencv加载的图像错误

Python opencv加载的图像错误,python,opencv,Python,Opencv,灾难! 如您所见,图像加载不完全正确。原件: 守则: import cv2 import imutils a=imutils.url_to_image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png", readFlag=-1) cv2.imshow("goog", a) cv2.waitKey() 在imutils中实现url\u到\u图像: def url_to_im

灾难!

如您所见,图像加载不完全正确。原件:

守则:

import cv2
import imutils
a=imutils.url_to_image("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png", readFlag=-1)
cv2.imshow("goog", a)
cv2.waitKey()
在imutils中实现url\u到\u图像:

def url_to_image(url, readFlag=cv2.IMREAD_COLOR):
    # download the image, convert it to a NumPy array, and then read
    # it into OpenCV format
    resp = urlopen(url)
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    image = cv2.imdecode(image, readFlag)

    # return the image
    return image
我还尝试了readFlag=cv2.IMREAD_,但没有改变,但也没有奏效

请发送帮助

好的,我们做到了

所以我尝试了另一个版本的显示:

plt.figure("Correct")
plt.imshow(imutils.opencv2matplotlib(a))
plt.show()

看来运气不好。但是,通过查看opencv2matplotlib源代码,我们发现:

def opencv2matplotlib(image):
    # OpenCV represents images in BGR order; however, Matplotlib
    # expects the image in RGB order, so simply convert from BGR
    # to RGB and return
    return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
啊哈,但是我们有4通道颜色(alpha),所以根据常识我们需要cv2.color\u BGRA2RGBA而不是cv2.color\u BGR2RGB

检验这一理论:

plt.figure("Correct")
plt.imshow(cv2.cvtColor(a, cv2.COLOR_BGRA2RGBA))
plt.show()
我们得到

呜呜迪嘟

# import the necessary packages
import numpy as np
import urllib
import cv2

def url_to_image(url):
    # download the image, convert it to a NumPy array, and then read
    # it into OpenCV format
    resp = urllib.request.urlopen(url)
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)

    # return the image
    return image


# initialize the list of image URLs to download
url="http://i.dailymail.co.uk/i/pix/2015/09/01/18/2BE1E88B00000578-3218613-image-m-5_1441127035222.jpg"

print ("downloading %s" % (url))
image = url_to_image(url)
cv2.imshow("Image", image)
cv2.waitKey(0)
输出为:

为什么不使用urllibI?对此我有一个解决方案。您还需要它吗?在这种情况下,您不会使用matplotlib。而是使用SimpleIMShow(img)来显示图像?使用urllib。urlopen位于名称空间urllib.request下,还需要使用-1标志调用cv2.imdecode,否则它甚至无法正确写入文件此实现在目标url上失败。