gpt4 book ai didi

python |自动数据帧生成

转载 作者:行者123 更新时间:2023-12-01 08:13:34 25 4
gpt4 key购买 nike

我有两个文件夹,其中包含两个不同白天(白天和夜晚)的城市天际线图像。我想读取相应文件夹中不同颜色空间的所有图像,然后我想计算所有颜色 channel 的统计数据。然后我想创建一个包含所有统计数据的 pandas 数据框。

为了防止不必要的重复代码,我尝试使用字典。目前,我可以打印颜色空间 x channel x 统计数据的所有组合的所有统计数据。但从概念上讲,我无法将这些东西放入包含行(单独的图像)和列(文件名、颜色空间 x channel x 统计数据)的 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]]))

最佳答案

在不更改大部分代码的情况下,我能想到的最简单的事情是:

  • 创建一个空的 df,其列都是统计数据 x channel x color_space 的组合(通过列表理解即可轻松完成);
  • 对于每个图像,将所有统计信息附加到一个变量():
  • row 转换为 pd.Series 对象,使用 row 作为值,使用数据帧的列作为索引,使用 filename 作为名称;
  • 将该行附加到您的空 df 中。

最重要的细节是确保 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).

关于 python |自动数据帧生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55087855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com