Python imshow彩色图像,错误显示为蓝色
我正在尝试使用opencv读取和显示tiff图像。 我在imread(-1,0,1,2)中尝试了不同的阅读模式 下面代码的结果仅在图像着色时错误地显示为蓝色Python imshow彩色图像,错误显示为蓝色,python,python-3.x,opencv,matplotlib,cv2,Python,Python 3.x,Opencv,Matplotlib,Cv2,我正在尝试使用opencv读取和显示tiff图像。 我在imread(-1,0,1,2)中尝试了不同的阅读模式 下面代码的结果仅在图像着色时错误地显示为蓝色 import numpy as np import cv2 import matplotlib.pyplot as plt def readImagesAndTimes(): # List of exposure times times = np.array([ 1/30.0, 0.25, 2.5, 15.0 ], dtype=n
import numpy as np
import cv2
import matplotlib.pyplot as plt
def readImagesAndTimes():
# List of exposure times
times = np.array([ 1/30.0, 0.25, 2.5, 15.0 ], dtype=np.float32)
# List of image filenames
filenames = ["img01.tif", "img02.tif", "img03.tif", "img04.tif", "img05.tif"]
images = []
for filename in filenames:
im = cv2.imread("./data/hdr_images/" + filename, -1)
images.append(im)
return images, times
images, times = readImagesAndTimes()
for im in images:
print(im.shape)
plt.imshow(im, cmap = plt.cm.Spectral)
原始图像:
[]
显示代码的蓝色图像:
[]问题在于opencv使用
bgr
颜色模式,matplotlib使用rgb
颜色模式。因此,红色和蓝色通道被切换
您可以通过向matplotlib验证rgb图像或使用cv2.imshow
函数轻松解决该问题
for im in images:
# convert bgr to rgb
rgb = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
plt.imshow(rgb, cmap = plt.cm.Spectral)
for im in images:
# no color conversion needed, because bgr is also used by imshow
cv2.imshow('image',im)
我会在一天内试用它,并让你知道它是否有效,尽管如此,还是要感谢lotrgb=cv2.cvtColor(im,cv2.COLOR\u BGR2RGB)plt.imshow(rgb,cmap=plt.cm.spectrum)当cv2.imshow('image',im im)在pycharm中打开一个从jupyter运行的新窗口时,它仍然有效。仍然非常感谢您的帮助和有效的解决方案。PS您使用的是什么cv2版本?不客气。我正在使用opencv 3.3.1-dev