Invert colors when plotting a PNG file using matplotlib

17,895

Solution 1

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)

From the OpenCV documentation:

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

Solution 2

Try:

plt.imshow(cv2.cvtColor(cube, cv2.COLOR_BGR2RGB))

Solution 3

As others have pointed out, the problem is that numpy arrays are in BGR format, but matplotlib expects the arrays to be ordered in a different way.

You are looking for scipy.misc.toimage:

import scipy.misc
rgb = scipy.misc.toimage(cube)

Alternatively, you can use scipy.misc.imshow().

Share:
17,895
Edgar Andrés Margffoy Tuay
Author by

Edgar Andrés Margffoy Tuay

Systems and Computing Engineering student at Uniandes. Currently interested on Computer Vision, Neural Networks and general purpose Scientific Computing, also I'm a Python enthusiast with motivation to learn new things and programming languages, besides, I also like to implement new programming languages and Compilers.

Updated on July 26, 2022

Comments

  • Edgar Andrés Margffoy Tuay
    Edgar Andrés Margffoy Tuay almost 2 years

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

    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()
    

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

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

    Thanks in advance.

  • Mark Ransom
    Mark Ransom over 11 years
    Good workaround, but the question still remains: why did it happen?
  • pyan
    pyan almost 8 years
    Nice elegant solution!
  • Ryan Soklaski
    Ryan Soklaski almost 7 years
    There is an easier way to flip the color channels: cube = cube[..., ::-1]
  • Kaaf
    Kaaf over 4 years
    @Mark Ransom OpenCV is quite old, and these color orders were chosen way back then before RGB became the de facto standard order, BGR made more sense since it is in alphabetical order, and was widely used at the time.
  • Kaaf
    Kaaf over 4 years
    @Mark Ransom Well, a lot of products used BGR back then, RGB was not as universal as it is today, products like the Game Gear, the wonderSwan and more importantly for this subject, camera makers, image software providers and Windows itself all used BGR, and I don't need to explain to you why the last three were an important decision point for a library like OpenCV. For more details see this: learnopencv.com/why-does-opencv-use-bgr-color-format:
  • Mark Ransom
    Mark Ransom over 4 years
    @Kaaf thanks for the link. I knew that BGR was used by Windows, but that wasn't until the '80's. I still don't have a single example of it being used before then.
  • Mr-Programs
    Mr-Programs about 4 years
    wonder if there isnt a cmap kwarg solution to this, such as cmap=bgr