我正在尝试阅读" .jpg" python中的图像使用cv2.imread()成为一个numpy数组。代码对我来说似乎很好但是当我使用plt.imshow()显示图像时,我看到在将图像复制到数组后图像无法正确显示。不知道我在这里做错了什么。我错过了什么?
以下是代码:
import cv2
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# Get current working directory
cwd = os.getcwd()
print(cwd)
# Read the csv file into a data frame
driving_log_df = pd.read_csv('driving_log.csv')
print(driving_log_df.shape)
X_data_set = np.empty([len(driving_log_df['Center'])*3, 32, 32, 3])
Y_data_set = np.empty(len(driving_log_df['Center'])*3)
print(X_data_set.shape)
print(Y_data_set.shape)
# Path to images
images_path = cwd + "/IMG"
print(images_path)
# Index
index = 0
for file in os.listdir(images_path):
image = cv2.imread(os.path.join(images_path, file), cv2.IMREAD_COLOR)
image = cv2.resize(image, (32, 32))
# OpenCV reads images in the BGR format, convert them into RGB
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Copy the image
X_data_set[index] = np.copy(image_rgb)
# Display images
if(index==0):
figure1 = plt.figure()
plt.imshow(image_rgb)
figure2 = plt.figure()
plt.imshow(X_data_set[0])
index = index + 1
还附有正在显示的图像。
答案 0 :(得分:0)
cv2中的轴顺序是bgr。所以你需要np.transpose到rgb。