python - imshow 彩色图像,错误显示为蓝色

标签 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=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)

原图:

[ original ]

显示代码的蓝色图像:

[ blue ]

最佳答案

问题是opencv使用bgr颜色模式而matplotlib使用rgb颜色模式。因此,红色和蓝色 channel 被切换。

您可以通过证明 matplotlib 是 rgb 图像或使用 cv2.imshow 函数轻松解决该问题。

  1. BGR 到 RGB 的转换:

    for im in images:
        # convert bgr to rgb 
        rgb = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
        plt.imshow(rgb, cmap = plt.cm.Spectral)
    
  2. opencv的imshow函数:

    for im in images:
        # no color conversion needed, because bgr is also used by imshow
        cv2.imshow('image',im)
    

关于python - imshow 彩色图像,错误显示为蓝色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55897748/

相关文章:

python - 使用 openCV 通过网络摄像头制作实时草图

python - Bokeh 复选框仅在选中时更新

python - django 重置内容类型不匹配

python - 使用 python 3 将一些多条记录插入 mysql 数据库

python - 选择行,使特定列包含列表中的值

python - PRAW:get_submission 工作不一致

python - 从 BGR 转换为 HSV 并再次转换回来时,图像颜色看起来不正常

c++ - 在不知道类型 opencv 的情况下访问矩阵值

python - 为每个 Pandas 单元格设置相同的字典

python - 在 django/selenium Web 测试中创建预认证 session