OpenCV giving wrong color to colored images on loading
<p>I'm loading in a color image in Python OpenCV and plotting the same. However, the image I get has it's colors all mixed up. </p>

<p>Here is the code:</p>

<pre><code>import cv2
import numpy as np
from numpy import array, arange, uint8 
from matplotlib import pyplot as plt


img = cv2.imread('lena_caption.png', cv2.IMREAD_COLOR)
bw_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

images = []
images.append(img)
images.append(bw_img)

titles = ['Original Image','BW Image']

for i in xrange(len(images)):
    plt.subplot(1,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])

plt.show()
</code></pre>

<p>Here is the original image:
<a href="http://i.stack.imgur.com/o9G7e.png" rel="nofollow"><img src="http://i.stack.imgur.com/o9G7e.png" alt="enter image description here"></a></p>

<p>And here is the plotted image:
<a href="http://i.stack.imgur.com/KagWq.png" rel="nofollow"><img src="http://i.stack.imgur.com/KagWq.png" alt="enter image description here"></a></p>

<p>OpenCV uses BGR as its default colour order for images, matplotlib uses RGB. When you display an image loaded with OpenCv in matplotlib the channels will be back to front. </p>

<p>The easiest way of fixing this is to use OpenCV to explicitly convert it back to RGB, much like you do when creating the greyscale image.</p>

<pre><code>    RGB_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
</code></pre>

<p>And then use that in your plot.</p>

python image opencv colors