cv2.imdecode颜色错误(python opencv) [英] Wrong colours with cv2.imdecode (python opencv)

查看:1311
本文介绍了cv2.imdecode颜色错误(python opencv)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试显示位于此处的图像:



我使用以下代码:

  import matplotlib.pyplot as plt 
import numpy as np
import urllib
import cv2

url ='http://skyservice.pha.jhu。 edu / DR12 / ImgCutout / getjpeg.aspx?ra = 118.70299999999999& dec = 45.721000000000004& width = 10& height = 10& scale = 0.6'

def url_to_image(url):
resp = urllib.request.urlopen(URL)
image = np.asarray(bytearray(resp.read()),dtype = uint8)
image = cv2.imdecode(image,cv2.IMREAD_COLOR)
返回图像

img = url_to_image(url)
plt.imshow(img)

并显示以下内容:





所有颜色太蓝了我尝试了各种可能性来更改 cv2.IMREAD_COLOR 的值,这些值可以在手册中,StackOverflow或网络上的其他地方找到,例如-1、0、1, cv2.COLOR_BGR2RGB ,...但是我一直无法获得正确的颜色。我尝试了 cv2.COLOR_BGR2GRAY ,它甚至没有显示为灰度。我什至尝试此答案,但是 cv2.CV_LOAD_IMAGE_COLOR 似乎不再存在...



是否存在正确的值 cv2.imdecode()标志或plt.imshow()的特殊颜色图,它会为我提供初始颜色吗?

方案

感谢Mark Setchell,现在可以使用了。我引用了他:


matplotlib 需要 RGB 排序,而OpenCV(相反)使用 BGR


,正确的代码是

  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)
返回图像


I try to display the image located here: http://skyservice.pha.jhu.edu/DR12/ImgCutout/getjpeg.aspx?ra=118.70299999999999&dec=45.721000000000004&width=10&height=10&scale=0.6

The image looks like this:

I use this code:

import matplotlib.pyplot as plt
import numpy as np
import urllib
import cv2

url = 'http://skyservice.pha.jhu.edu/DR12/ImgCutout/getjpeg.aspx?ra=118.70299999999999&dec=45.721000000000004&width=10&height=10&scale=0.6'

def url_to_image(url):
    resp = urllib.request.urlopen(url)
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    return image

img = url_to_image(url)
plt.imshow(img)

And it displays this:

Where all colours are too blue. I have tried various possibilities to change cv2.IMREAD_COLOR with values found in the manual, on StackOverflow or elsewhere on the net, like -1, 0, 1, cv2.COLOR_BGR2RGB, ... but I haven't been able to get the right colours. I have tried cv2.COLOR_BGR2GRAY, it didn't even show in gray scales. I even tried this answer, but cv2.CV_LOAD_IMAGE_COLORdoesn't seem to exist anymore...

Is there a correct value of cv2.imdecode() flag, or a special colormap of plt.imshow(), which would give me the initial colours?

解决方案

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

这篇关于cv2.imdecode颜色错误(python opencv)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆