1. 程式人生 > >python中opencv讀取RGB影象儲存的矩陣,顏色通道順序是B,G,R。

python中opencv讀取RGB影象儲存的矩陣,顏色通道順序是B,G,R。



import numpy as np
np.random.seed(1337)  # for reproducibility
from keras.datasets import mnist
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
import h5py
from PIL import Image
import cv2
import matplotlib.pyplot as plt


img=np.zeros([2000,500,500,3])
for i in range(2):
    T = cv2.resize(cv2.imread('G:\\train\\cat.'+str(i)+'.jpg'),(500,500))
    img[i,:,:,:] = T
    if(i==1000):
        img = cv2.imread('G:\\train\\dog.' + str(i+1000) + '.jpg')

t=img[0,:,:,:]
r=Image.fromarray(t[:,:,0]).convert('L')
g=Image.fromarray(t[:,:,1]).convert('L')
b=Image.fromarray(t[:,:,2]).convert('L')
T=Image.merge("RGB",(b,g,r))
print(type(T),np.shape(T))
plt.imshow(T)
plt.show()