File size: 2,295 Bytes
fd601de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import glob
import os
from CTevaluate import *

def singleevaluate(file_path,  window_width = 150, window_level = 30):
    # Load the NIfTI CT image data using nibabel.
    if file_path.endswith('.nii.gz'):
        ct_image_nifti = nib.load(file_path)
        ct_image_data = ct_image_nifti.get_fdata()
        ct_image_nifti = nib.load(file_path)
        ct_image_data = ct_image_nifti.get_fdata()
    elif file_path.endswith('.nrrd'):
        ct_image_data, header = nrrd.read(file_path)
    
    ct_data_shape=ct_image_data.shape

    #plot_ct_value_distribution(ct_image_data)
    ct_image_data = ct_windowing(ct_image_data, window_width, window_level)
    #plot_ct_value_distribution(ct_image_data)
    
    # cut roi
    center_x = ct_image_data.shape[0] // 2
    center_y = ct_image_data.shape[1] // 2
    ct_image_roi=extract_roi(ct_image_data, center_x=center_x, center_y=center_y, length=300, width=300)
    ct_image_roi_mean=np.mean(ct_image_roi)

    # Calculate contrast and standard deviation of CT values.
    contrast = calculate_contrast(ct_image_data)
    std_deviation = calculate_standard_deviation(ct_image_data)
    return ct_image_roi_mean, contrast, std_deviation, ct_data_shape

def batchevaluate(dataset_path, format='.nii.gz', save_path='', nii_name='test'):
    for patient_data in glob.glob(dataset_path + "/*"):
        if patient_data.endswith(format):
            patient_name=os.path.basename(os.path.normpath(patient_data))
            print('-------------', patient_name, '-------------')
            ct_image_roi_mean, contrast, std_deviation, ct_data_shape = singleevaluate(patient_data)
            with open(os.path.join(save_path, f'{nii_name}.txt'), 'a') as f:
                f.write('-------------'+patient_name+'-------------\n')
                f.write('Mean of CT values in ROI: '+str(ct_image_roi_mean)+'\n')
                f.write('Contrast of CT image: '+str(contrast)+'\n')
                f.write('Standard Deviation of CT values: '+str(std_deviation)+'\n')
                f.write('Size of CT image: '+str(ct_data_shape)+'\n')
def main():
    dataset_path=r'D:\Data\dataNeaotomAlpha\NIFTI23072115'
    batchevaluate(dataset_path=dataset_path, format='.nii.gz', save_path=dataset_path, nii_name='evaluate')

if __name__=="__main__":
    main()