OpenCV giving wrong color to colored images on loading
Question:
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.
Here is the 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()
Answers:
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.
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.
RGB_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
And then use that in your plot.
As an alternative to the previous answer, you can use (slightly faster)
img = cv2.imread('lena_caption.png')[...,::-1]
%timeit [cv2.cvtColor(cv2.imread(f), cv2.COLOR_BGR2RGB) for f in files]
231 ms ± 3.08 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit [cv2.imread(f)[...,::-1] for f in files]
220 ms ± 1.81 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
If you try to read an image using OpenCV, it will use BGR as the default. So you have to use a different approach to read an Image. I have made the required changes to your code to get the desired output has been given below.
import cv2
import numpy as np
from numpy import array, arange, uint8
from matplotlib import pyplot as plt
img = cv2.cvtColor(cv2.imread('lena_caption.png'), cv2.COLOR_BGR2RGB)
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()
Simple one-line solution
np.flip(img, axis=-1)
This can convert both ways. From RGB to BGR, and from BGR to RGB.
You may also want to try cv2.IMREAD_UNCHANGED(). See more here to see how it differs from IMREAD_COLOR:
https://www.geeksforgeeks.org/python-opencv-cv2-imread-method/
after reading the image, reverting the order of bgr matrix to rgb by reading the matrix from right to left:
x = cv2.imread('./image.jpg')
x=x[:,:,::-1]
plt.imshow(x)
If you are using PyCharm in dark mode you will find out that changing from BGR to RGB is not enough. This is because PyCharm has a setting called "Invert image outputs for dark themes"
Disabling Preferences | Languages & Frameworks | Jupyter | Invert
image outputs for dark themes should help
Invert image is ON:
Invert image is OFF:
Please note that the code is the same in both examples. Only the setting in PyCharm is different.
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.
Here is the 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()
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.
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.
RGB_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
And then use that in your plot.
As an alternative to the previous answer, you can use (slightly faster)
img = cv2.imread('lena_caption.png')[...,::-1]
%timeit [cv2.cvtColor(cv2.imread(f), cv2.COLOR_BGR2RGB) for f in files]
231 ms ± 3.08 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit [cv2.imread(f)[...,::-1] for f in files]
220 ms ± 1.81 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
If you try to read an image using OpenCV, it will use BGR as the default. So you have to use a different approach to read an Image. I have made the required changes to your code to get the desired output has been given below.
import cv2
import numpy as np
from numpy import array, arange, uint8
from matplotlib import pyplot as plt
img = cv2.cvtColor(cv2.imread('lena_caption.png'), cv2.COLOR_BGR2RGB)
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()
Simple one-line solution
np.flip(img, axis=-1)
This can convert both ways. From RGB to BGR, and from BGR to RGB.
You may also want to try cv2.IMREAD_UNCHANGED(). See more here to see how it differs from IMREAD_COLOR:
https://www.geeksforgeeks.org/python-opencv-cv2-imread-method/
after reading the image, reverting the order of bgr matrix to rgb by reading the matrix from right to left:
x = cv2.imread('./image.jpg')
x=x[:,:,::-1]
plt.imshow(x)
If you are using PyCharm in dark mode you will find out that changing from BGR to RGB is not enough. This is because PyCharm has a setting called "Invert image outputs for dark themes"
Disabling Preferences | Languages & Frameworks | Jupyter | Invert
image outputs for dark themes should help
Invert image is ON:
Invert image is OFF:
Please note that the code is the same in both examples. Only the setting in PyCharm is different.