使用matplotlib绘制PNG文件时反转颜色 [英] Invert colors when plotting a PNG file using matplotlib

查看:386
本文介绍了使用matplotlib绘制PNG文件时反转颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib以及python显示PNG文件.对于此测试,我生成了以下图像:

I'm trying to display a PNG file using matplotlib and of course, python. For this test, I've generated the following image:

现在,我加载图像并将其转换为多维numpy矩阵:

Now, I load and transform the image into a multidimensional numpy matrix:

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

cube = cv2.imread('Graphics/Display.png')
plt.imshow(cube)
plt.ion()

当我尝试在matplotlib中绘制该图像时,颜色被反转:

When I try to plot that image in matplotlib, the colors are inverted:

如果矩阵没有任何修改,为什么绘图中的颜色是错误的?

If the matrix does not have any modifications, why the colors in the plot are wrong?

先谢谢了.

推荐答案

似乎您可能已经以某种方式用BGR切换了RGB.请注意,您的绿色被保留,但所有蓝色变为红色.如果cube具有形状(M,N,3),请尝试将cube[:,:,0]cube[:,:,2]交换.您可以像这样使用numpy来做到这一点:

It appears that you may somehow have RGB switched with BGR. Notice that your greens are retained but all the blues turned to red. If cube has shape (M,N,3), try swapping cube[:,:,0] with cube[:,:,2]. You can do that with numpy like so:

rgb = numpy.fliplr(cube.reshape(-1,3)).reshape(cube.shape)

OpenCV 文档:

注:对于彩色图像,解码后的图像将具有 频道以B G R顺序存储.

Note: In the case of color images, the decoded images will have the channels stored in B G R order.

这篇关于使用matplotlib绘制PNG文件时反转颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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