python - 将 RGB 图像加载为 ndarray,并绘制颜色变化的图

标签 python image opencv matplotlib plot

我尝试从本地文件夹加载并绘制多个图像(jpg),发现绘制图像的颜色发生了变化。 OpenCV和Matplotlib之间的颜色 channel 校正已经完成。

这是怎么发生的?如何校正颜色? 谢谢。

import cv2
from matplotlib import pyplot as plt
import numpy as np
import os

folder = 'New_Web_Image'
img_list = np.empty([0,32,32,3])
for file in os.listdir(folder):
    img = cv2.imread(os.path.join(folder, file)) 
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 
    img = cv2.resize(img, (32,32), interpolation = cv2.INTER_AREA)

    #plt.imshow(img)  
    #plt.show()#If I plot the image here, the image show right color

    img_list = np.append(img_list, [img[:, :, :]], axis=0) 
    print(img_list.shape) #lists shape check right


plt.imshow(img_list[0]) 
plt.show() #If I plor the image from the lists, the color changed

这是循环中的图像结果:
images plot after resize
这是来自 ndarray“列表”的图像:
images plot from img_list[0]

最佳答案

这不是色彩校正。 OpenCV 将图层排序为 BGR,而不是我们通常期望的 RGB。只要您还留在 OpenCV 世界,那就没问题。但是通过 cv2.imread() 加载到 matplotlib.pyplot 的 anding 和图像超出了这个世界,这就是你需要的原因

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

首先对图层进行重新排序。

还有许多其他有趣(并且可能有用)的转换是可能的。请参阅http://docs.opencv.org/3.2.0/df/d9d/tutorial_py_colorspaces.html

关于python - 将 RGB 图像加载为 ndarray,并绘制颜色变化的图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45626400/

相关文章:

java - 轻松创建 "loading"动画

python - Pycharm 无法识别 cv2 但仍然可用

python - 如何在不卸载的情况下解决导入cv2上的ImportError

python - 使用 pip 安装 Senticnet 时出现 Unicode 解码错误

python - 鸭子类型(duck typing)以允许元组、列表或可以像一个一样对待的东西

html - 使图像相对于其父容器具有静态位置?

javascript - 从二维数组中获取img src和img维度并显示在body中

c++ - OpenCV 2.4.2 错误 "OpenCV Error: Bad flag"

python - 在 Python 中使用 Datumbox 进行情感分析

python - 使用 geopandas 生成形状文件时引发 ValueError