OpenCV (BGR mode) and Matplotlib (RGB mode)
Color image loaded by OpenCV is in BGR mode. But Matplotlib displays in RGB mode. So color images will not be displayed correctly in Matplotlib if image is read with OpenCV. Please see the exercises for more details.
OpenCV 加载的彩色图像处于 BGR 模式。但 Matplotlib 以 RGB 模式显示。因此,如果使用 OpenCV 读取图像,则 Matplotlib 中的彩色图像将无法正确显示。有关详细信息,请参阅练习。
There is some problem when you try to load color image in OpenCV and display it in Matplotlib.
当您尝试在 OpenCV 中加载彩色图像并在 Matplotlib 中显示它时,会出现问题。
There is a slight difference in pixel ordering in OpenCV and Matplotlib. OpenCV follows BGR order, while matplotlib likely follows RGB order.
OpenCV 和 Matplotlib 中的像素排序略有不同。OpenCV 遵循 BGR 顺序,而 matplotlib 可能遵循 RGB 顺序。
So when you display an image loaded in OpenCV using pylab functions, you may need to convert it into RGB mode. Below method demonstrate it:
因此,当您使用 pylab 函数显示在 OpenCV 中加载的图像时,您可能需要将其转换为 RGB 模式。下面的方法演示了它:
1. Example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
import cv2
import numpy as np
import matplotlib.pyplot as plt
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))
print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")
img_bgr = cv2.imread('/home/strong/sunergy_moonergy/object_counter/parking_system.jpg')
b, g, r = cv2.split(img_bgr)
img_rgb = cv2.merge([r, g, b])
plt.subplot(121)
plt.title("BGR image")
plt.imshow(img_bgr) # distorted color
plt.subplot(122)
plt.title("RGB image")
plt.imshow(img_rgb) # true color
plt.show()
cv2.imshow('BGR image', img_bgr) # true color
cv2.imshow('RGB image', img_rgb) # distorted color
cv2.waitKey(0)
cv2.destroyAllWindows()