2017-08-11 78 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