opencv 使用Python将要素属性保存到CSV文件

92dk7w1h  于 2023-03-03  发布在  Python
关注(0)|答案(1)|浏览(169)

我正在使用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文件中?

vmdwslir

vmdwslir1#

我找到了问题的答案:我为每张图片创建了一个新的字典行,并将文件名和所有特征添加到字典行中。然后,我将每一行附加到feature_rows列表中。最后,我从特征行列表中创建了一个PandasDataFrame,并将其保存到CSV文件中,如下所示:

# create an empty list to store the feature rows
feature_rows = []

# 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))
    image = image.astype(float) / 255.0  # rescale to (0, 1)

    # 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))
    mask = mask.astype(float) / 255.0  # rescale to (0, 1)
    
    #compute perimeter
    contours, _ = cv2.findContours(np.uint8(mask * 255), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    perimeter = cv2.arcLength(contours[0], closed=True)    

    # extract features
    features = {}
    features['A_GLRLM'] = glrlm_features(image, mask, Ng=256)
    features['A_Shape_Parameters'] = shape_parameters(image, mask, perimeter, pixels_per_mm2=1)    

    # create a dictionary to store the features for this image
    row = {'filename': img_name}

    # add the shape parameters to the row
    for i, name in enumerate(features['A_Shape_Parameters'][1]):
        row[name] = features['A_Shape_Parameters'][0][i]

    # add the GLRLM features to the row
    for i, name in enumerate(features['A_GLRLM'][1]):
        row[name] = features['A_GLRLM'][0][i]

    # add the row to the list of feature rows
    feature_rows.append(row)

# create a pandas DataFrame from the list of feature rows
df = pd.DataFrame(feature_rows)

# save the DataFrame to a CSV file
df.to_csv('radiomic_features.csv', index=False)

相关问题