2017-08-11 51 views
0

我嘗試從本地文件夾加載和繪製幾個圖像(jpg),並發現繪圖圖像顏色發生了變化。 cv2和matplotlib之間的顏色通道校正已完成。將RGB圖像加載爲ndarray並繪製出顏色變化

它是怎麼發生的?如何改正顏色? 謝謝。

import cv2 
from matplotlib import pyplot as plt 
import numpy as np 
import os 

folder = 'New_Web_Image' 
img_list = np.empty([0,32,32,3]) 
for file in os.listdir(folder): 
    img = cv2.imread(os.path.join(folder, file)) 
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 
    img = cv2.resize(img, (32,32), interpolation = cv2.INTER_AREA) 

    #plt.imshow(img) 
    #plt.show()#If I plot the image here, the image show right color 

    img_list = np.append(img_list, [img[:, :, :]], axis=0) 
    print(img_list.shape) #lists shape check right 


plt.imshow(img_list[0]) 
plt.show() #If I plor the image from the lists, the color changed 

這裏是圖像導致循環:
images plot after resize
這裏是ndarray 「名單」 的形象:
images plot from img_list[0]

回答

0

要halfly回答我的問題,我已經糾正顏色由

  • 用ndarray輸出首先加載圖像,
  • 然後變色&大小,並繪製出圖像

更新代碼:

import cv2 
from matplotlib import pyplot as plt 
import numpy as np 
import os 

# Load the images 
folder = 'New_Web_Image' 
img_list = [] 
for file in os.listdir(folder): 
    img = cv2.imread(os.path.join(folder, file)) 
    if img is not None: 
     img_list.append(img) 
img_list = np.asarray(img_list)  

# Plot the images 
n = img_list.shape[0] 
fig, axs = plt.subplots(1, n, figsize=(20,5), dpi=80) 
for i in range(n): 
    img = img_list[i] 
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 
    img = cv2.resize(img, (32,32), interpolation = cv2.INTER_AREA) 

    axs[i].imshow(img) 
plt.show() 

另一半的問題,說:「怎麼沒在上面的代碼中的顏色變化?「目前還不清楚我。事先誰建議

感謝。

0

這不是一個顏色校正。OpenCV的訂單層爲BGR,而不是RGB我們通常想到的,只要你」再有OpenCV的世界停留,這應該是罰款,但與和圖像經由外部世界cv2.imread()matplotlib.pyplot步加載,這就是爲什麼你需要

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 

,以獲得第一層重新排序。

其他一些有趣的(也可能有用的)轉換是可能的。見http://docs.opencv.org/3.2.0/df/d9d/tutorial_py_colorspaces.html