Wrong colours with cv2.imdecode (python opencv)

China☆狼群 提交于 2019-12-06 04:13:11

Thanks to Mark Setchell, it now works. I quote him:

matplotlib requires RGB ordering whereas OpenCV (perversely) uses BGR

Therefore, the correct code is

def url_to_image(url):
    resp = urllib.request.urlopen(url)
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    imageBGR = cv2.imdecode(image, cv2.IMREAD_COLOR)
    imageRGB = cv2.cvtColor(imageBGR , cv2.COLOR_BGR2RGB)
    return image
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!