我正在使用pyfeats库,通过图像及其各自的ROI遮罩提取放射性特征。我正在提取形状特征和GLRLM特征。形状特征是使用shape_parameters函数提取的,该函数提供SHAPE_XcoordMax、SHAPE_YcoordMax、SHAPE_area、SHAPE_perimeter和SHAPE_perimeter2perArea值。glrlm_features返回GLRLM_ShortRunEmphasis、GLRLM_LongRunEmphasis、以及其他10个属性值,代码如下:
import os
import numpy as np
import cv2
import matplotlib.pyplot as plt
from pyfeats import *
import pandas as pd
from scipy import ndimage as ndi
#%%
# define image and mask folder paths
image_folder = 'images'
mask_folder = 'masks'
# get list of image names
image_names = [f for f in os.listdir(image_folder) if f.endswith('.png')]
# create an empty dictionary to store the features for each image
features_dict = {}
# iterate through each image and its corresponding mask
for img_name in image_names:
# Load image and resize to 224 x 224
img_path = os.path.join(image_folder, img_name)
image = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
image = cv2.resize(image, (224, 224))
# Load mask and resize to 224 x 224
mask_name = img_name
mask_path = os.path.join(mask_folder, mask_name)
mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
mask = cv2.resize(mask, (224, 224))
#compute perimeter
mask //= 255
kernel = np.ones((5,5))
C= ndi.convolve(mask, kernel, mode='constant', cval=0)
perimeter = np.where( (C>=11) & (C<=15 ), 255, 0)
# extract features: Texture
features = {}
features['A_GLRLM'] = glrlm_features(image, mask, Ng=256)
features['A_Shape_Parameters'] = shape_parameters(image, mask, perimeter, pixels_per_mm2=1)
# add features to dictionary
features_dict[img_name] = features
#%%
# convert features dictionary to a pandas DataFrame and save to CSV file
计算完这些特征后,我希望将这些定量值保存到CSV文件中,并将其保存在各自的列标题下(例如,SHAPE_XcoordMax、SHAPE_YcoordMax、SHAPE_area等)沿着将图像文件名保存在单独的列中。图像和蒙版具有相同的文件名,扩展名为.png。如何将这些文件名和特征保存到CSV文件中?
1条答案
按热度按时间vmdwslir1#
我找到了问题的答案:我为每张图片创建了一个新的字典行,并将文件名和所有特征添加到字典行中。然后,我将每一行附加到feature_rows列表中。最后,我从特征行列表中创建了一个PandasDataFrame,并将其保存到CSV文件中,如下所示: