我有两个文件夹来自城市天际线两个不同的白天(白天和黑夜)。我想读取相应文件夹中不同颜色空间的所有图像,然后我想计算所有颜色通道的统计数据。然后我想创建一个包含所有统计信息的pandas数据框。
为了防止不必要的重复代码,我试图使用字典。目前,我能够打印出所有颜色空间x通道x统计组合的所有统计数据。但是我在概念上没有把这些东西变成带有行(单独的图像)和列(文件名,color_space x channel x statistic)的pandas DataFrame。
我将不胜感激任何帮助。
import os
import numpy as np
import matplotlib.pyplot as plt
import cv2
import pandas as pd
dictionary_of_color_spaces = {
'RGB': cv2.COLOR_BGR2RGB, # Red, Green, Blue
'HSV': cv2.COLOR_BGR2HSV, # Hue, Saturation, Value
'HLS': cv2.COLOR_BGR2HLS, # Hue, Lightness, Saturation
'YUV': cv2.COLOR_BGR2YUV, # Y = Luminance , U, V = Chrominance color components
}
dictionary_of_channels = {
'channel_1': 0,
'channel_2': 1,
'channel_3': 2,
}
dictionary_of_statistics = {
'min': np.min,
'max': np.max,
'mean': np.mean,
'median': np.median,
'std': np.std,
}
# get filenames inside training folders for day and night
path_training_day = './day_night_images/training/day/'
path_training_night = './day_night_images/training/night/'
filenames_training_day = [file for file in os.listdir(path_training_day)]
filenames_training_night = [file for file in os.listdir(path_training_night)]
for filename in filenames_training_day:
image = cv2.imread(path_training_day + filename)
for color_space in dictionary_of_color_spaces:
image = cv2.cvtColor(image, dictionary_of_color_spaces[color_space])
for channel in dictionary_of_channels:
for statistic in dictionary_of_statistics:
print(dictionary_of_statistics[statistic](image[:,:,dictionary_of_channels[channel]]))
在不改变代码大小的情况下,我能想到的最简单的事情是:
row
):row
转换为pd.Series对象,使用row
作为值,数据帧的列作为索引,filename
作为其名称;最重要的细节是使df列名称正确,即与填充row
变量的值的顺序相同。当我们在列名称中为列名创建组合时,重要的是我们从最里面的循环移动到最外面的循环,以便稍后当我们将row
附加到df时匹配的值。
这应该工作:
import os
import numpy as np
import matplotlib.pyplot as plt
import cv2
import pandas as pd
dictionary_of_color_spaces = {
'RGB': cv2.COLOR_BGR2RGB, # Red, Green, Blue
'HSV': cv2.COLOR_BGR2HSV, # Hue, Saturation, Value
'HLS': cv2.COLOR_BGR2HLS, # Hue, Lightness, Saturation
'YUV': cv2.COLOR_BGR2YUV, # Y = Luminance , U, V = Chrominance color components
}
dictionary_of_channels = {
'channel_1': 0,
'channel_2': 1,
'channel_3': 2,
}
dictionary_of_statistics = {
'min': np.min,
'max': np.max,
'mean': np.mean,
'median': np.median,
'std': np.std,
}
# creates column names in the same order as loops below
cols = [f'{s}_{c}_{cs}' for s in dictionary_of_statistics for c in dictionary_of_channels for cs in dictionary_of_color_spaces]
# creates empty df
df = pd.DataFrame(column=cols)
# get filenames inside training folders for day and night
path_training_day = './day_night_images/training/day/'
path_training_night = './day_night_images/training/night/'
filenames_training_day = [file for file in os.listdir(path_training_day)]
filenames_training_night = [file for file in os.listdir(path_training_night)]
for filename in filenames_training_day:
row = [] # row for the current image - to be populated with stat values
image = cv2.imread(path_training_day + filename)
for color_space in dictionary_of_color_spaces:
image = cv2.cvtColor(image, dictionary_of_color_spaces[color_space])
for channel in dictionary_of_channels:
for statistic in dictionary_of_statistics:
row.append(dictionary_of_statistics[statistic](image[:,:,dictionary_of_channels[channel]]))
row_series = pd.Series(row, index=cols, name=filename)
df = df.append(row_series)
此代码将每个图像的文件名转换为最终df中每行的索引。如果您不想这样,请将索引转换为新列(df['filename'] = df.index
)并在之后使用pandas.reset_index(pd = pd.reset_index(drop=True)
。