灾害!如您所见,图像填充不正确。原本的:代码:
导入cv2import imutilsA = imutils.url_to_image(“HTTPS://www.google.com/images/branding/googlelogo/2x / …
好吧帮我们做到了
所以我尝试了另一个版本的显示:
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_BGRA2RGBA而不是cv2.COLOR_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)
输出是: