【问题标题】:Quick way to convert image 3 channels depth to 1 channel in Python?在 Python 中将图像 3 通道深度转换为 1 通道的快速方法?
【发布时间】:2020-08-06 18:40:38
【问题描述】:

我正在处理 fer2013 数据集,正如您在代码中看到的,输出为 48、48、3),这是 3 个通道,不能在 Conv2D 层中使用。我希望它只是 1 个频道部门 - (48、48、1)。那么有没有办法将 x_train 和 x_test 转换为 1 通道深度(灰度通道)?

输出应该是:(28709, 48, 48, 1) (3589, 48, 48, 1) 而不是 (28709, 48, 48, 3) (3589, 48, 48, 3)

数据集链接:https://drive.google.com/file/d/1HH8gqzFOBavHxHbKbwjozu2QzlPlo6RW/view?usp=sharing

谢谢!

import os
import cv2

def prepare_Data(path="Facial_expression/fer2013",
             image_shape = (48, 48)):

TRAINING_DATA_PATH = os.path.join(path, 'Training')
TESTING_DATA_PATH = os.path.join(path, 'PrivateTest')

x_train = [] # is the training data set
y_train = [] # is the set of labels to all the data in x_train
x_test = []
y_test = []
label_id = 0

num_classes = len(os.listdir(TRAINING_DATA_PATH)) # get number of classes 7
for label in os.listdir(TRAINING_DATA_PATH): # get label in training path

    # Read training data
    for img_file in os.listdir(os.path.join(TRAINING_DATA_PATH, label)):
        img = cv2.imread(os.path.join(TRAINING_DATA_PATH, label, img_file)) # read all image in training path
        img = cv2.resize(img, image_shape) # resize all image with size = 48 x 48
        x_train.append(img) # append all training image in x_train

        y = np.zeros(num_classes) # create one hot vector with dimension of 7
        # print(y)
        y[label_id] = 1
        y_train.append(y)
    # Read testing data
    for img_file in os.listdir(os.path.join(TESTING_DATA_PATH, label)):
        img = cv2.imread(os.path.join(TESTING_DATA_PATH, label, img_file))
        img = cv2.resize(img, image_shape)

        x_test.append(img)
        y = np.zeros(num_classes)
        y[label_id] = 1
        y_test.append(y)
    label_id += 1

return np.array(x_train), np.array(y_train), np.array(x_test), np.array(y_test)

x_train, y_train,x_test, y_test = prepare_Data()

print("Number of images in Training set:", len(x_train))
print("Number of images in Test set:", len(x_test))
print(x_train.shape)
print(x_test.shape)
print(y_train.shape)
print(y_test.shape)
'Output'
Number of images in Training set: 28709
Number of images in Test set: 3589
(28709, 48, 48, 3)
(3589, 48, 48, 3)
(28709, 7)
(3589, 7)

【问题讨论】:

  • 不确定您需要的格式,但您可以使用 #RRGGBB 格式将通道转换为单个十六进制值
  • 您好,这取决于您在寻找什么样的功能。因此,如果您转换为 1 个通道,您将获得输入的灰度版本。一种通用的方法是取三个渠道的平均值。而不是调整大小,你应该执行np.mean(img, axis=2)
  • @DJSchaffner 我希望 foder 中的所有图像只有 1 个单一通道,即灰度
  • @AnuragReddy 谢谢你,让我试试那个。
  • @AnuragReddy 不知何故,输出仍然执行 3 个通道部门。 :( 应该是 : (28709, 48, 48, 1) (3589, 48, 48, 1) 而不是 (28709, 48, 48, 3) (3589, 48, 48, 3)

标签: python image-scaling


【解决方案1】:

经过一天的研究,我发现我需要将 28709 个图像重塑为 (48,48,1) 形状,但当前图像的形状是 (48,48,3),即 3 个通道。首先,我需要通过gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 将所有图像转换为灰度图像,然后使用 numpy.reshape 将所有图像重塑为 1 个通道。

TRAINING_DATA_PATH = os.path.join(path, 'Training')
TESTING_DATA_PATH = os.path.join(path, 'PrivateTest')

x_train = [] 
y_train = [] 
x_test = []
y_test = []

label_id = 0


num_classes = len(os.listdir(TRAINING_DATA_PATH)) 
for label in os.listdir(TRAINING_DATA_PATH):

    # Read training data
    for img_file in os.listdir(os.path.join(TRAINING_DATA_PATH, label)):
        img = cv2.imread(os.path.join(TRAINING_DATA_PATH, label, img_file)) 
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # ADD THIS
        img = cv2.resize(gray, image_shape)
        
        x_train.append(img) 
        y = np.zeros(num_classes) 
        y[label_id] = 1
        y_train.append(y)
    # Read testing data
    for img_file in os.listdir(os.path.join(TESTING_DATA_PATH, label)):
        img = cv2.imread(os.path.join(TESTING_DATA_PATH, label, img_file))
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # ADD THIS
        img = cv2.resize(gray, image_shape)
        x_test.append(img)
        
        y = np.zeros(num_classes)
        y[label_id] = 1
        y_test.append(y)
    label_id += 1
x_train = np.array(x_train) # ADD THIS
x_train = x_train.reshape(x_train.shape[0],48,48, 1) # ADD THIS

x_test = np.array(x_test) # ADD THIS
x_test = x_test.reshape(x_test.shape[0], 48, 48,1) # ADD THIS

return np.array(x_train), np.array(y_train), np.array(x_test), np.array(y_test) 

Number of images in Training set: 28709
Number of images in Test set: 3589
(28709, 48, 48, 1)
(3589, 48, 48, 1)
(28709, 7)
(3589, 7)

【讨论】:

    猜你喜欢
    • 2020-08-08
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 2018-10-19
    • 2013-01-25
    相关资源
    最近更新 更多