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?
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])
OpenCV represents the images in BGR as opposed to the RGB we expect. Since it is in the reverse order, you tend to see the blue color in images. Try using the following line (below comment in code) for converting from BGR to RGB:
import cv2
import matplotlib.pyplot as plt
lena=cv2.imread("lena.jpg")
#plt.imshow(lena)
#plt.axis("off")
#Converts from BGR to RGB
plt.imshow(cv2.cvtColor(lena, cv2.COLOR_BGR2RGB))
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With