imread()當你用 OpenCV 加載一個彩色圖像,并用 Matplotib 顯示它時會遇到一些困難。
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("C:\\Users\\SPC20-012\\Pictures\\girl.png")
plt.imshow(img, cmap = 'gray', interpolation=('bicubic'))
plt.xticks([]),plt.yticks([])
plt.show()
c使用下上述碼打開時,由于OpenCV是以BGR模式加載的,而Matplotib是RGB模式,是以讀取的圖檔顯示會異常,如下圖:
import cv2
from matplotlib import pyplot as plt
img = cv2.imread("C:\\Users\\SPC20-012\\Pictures\\girl.png")
RGBframe = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(RGBframe, cmap = 'gray', interpolation=('bicubic'))
plt.xticks([]),plt.yticks([])
plt.show()
修改後顯示正常: