matplotlib imshow distorting colors

dberga picture dberga · May 31, 2018 · Viewed 13.2k times · Source

I have tried to use the imshow function from matplotlib.pyplot and it works perfectly to show grayscale images. When I tried to represent rgb images, it changes the colors, showing a more blue-ish color.

See an example:

import cv2
import matplotlib.pyplot as plt
lena=cv2.imread("lena.jpg")
plt.imshow(lena)
plt.show()

The resulting image is something like this

While the original image is this

If it is something related to the colormap, there is any way to make it work with rgb images?

Answer

AGN Gazer picture AGN Gazer · May 31, 2018

This worked for me:

plt.imshow(lena[:,:,::-1]) # RGB-> BGR

Same idea but nicer and more robust approach is to use "ellipsis" proposed by @rayryeng:

plt.imshow(lena[...,::-1])