gpt4 book ai didi

python - opencv加载错误的图像

转载 作者:行者123 更新时间:2023-12-02 17:31:54 24 4
gpt4 key购买 nike

灾难!
enter image description here

如您所见,图像没有完全正确加载。原本的:
enter image description here

编码:

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_to_image的实现:
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_UNCHANGED,但这也没有奏效。

请发送帮助

最佳答案

好吧,我们做到了

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

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

enter image description here

没有运气它会出现。但随后,查看 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 channel 颜色(alpha),所以按照常识,我们需要 cv2.COLOR_BGRA2RGBA 而不是 cv2.COLOR_BGR2RGB!

测试这个理论:
plt.figure("Correct")
plt.imshow(cv2.cvtColor(a, cv2.COLOR_BGRA2RGBA))
plt.show()

我们得到...

enter image description here

呜呜呜!

关于python - opencv加载错误的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55046100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com