Appendix 1. Code for capturing radiographs into 350 x 300 pixels rectangle
#load libraries and packages import numpy as np
import pandas as pd
import matplotlib.pyplot as plt import pydicom
import os import sys import pickle
from scipy import ndimage savefile = directory
#load file
df = pd.read_pickle(savefile) #load savefile df
# create dictionary to lookup images ext=directory
pt_list=os.listdir(ext)
#enter second nested file print(ext)
print(pt_list) d={}
for f in pt_list:
if f=='.DS_Store':
continue
pt_list2 = ext + '/' + f dir2 = os.listdir(pt_list2) extlist = []
for dcmfile in dir2:
if dcmfile=='.DS_Store':
continue
dcm_ext = pt_list2 + '/' + dcmfile extlist.append(dcm_ext)
d[f] = extlist
#find radiograph to crop and resize pydicom.dcmread(d['xx'][x])
#ptn p='xx'
#projection
x=#
ext=d[p][x]
print(ext)
#print('study exists of:', len(d[list(d.keys())[p]]), 'images') dcm=pydicom.dcmread(ext)
date=dcm.StudyDate time=dcm.StudyTime[:6]
image = dcm.pixel_array
print('date_time_stamp:',date, time) print(dcm.pixel_array.shape)
plt.imshow(image, cmap=plt.cm.bone) print(df.iloc[-5:,:])
def rotate_img(img, angl):
rotated_img = ndimage.rotate(img, angle = angl, reshape=False) return rotated_img
#dcm=pydicom.dcmread(ext).pixel_array angl = x
image = rotate_img(image, angl) plt.imshow(image, cmap=plt.cm.bone) plt.show()
def crop_dicom(img, y_start, x_start, len_y, len_x):
#pix_array=pydicom.dcmread(extension).pixel_array pix_array = img
print(pix_array.shape)
pix_crop=pix_array[y_start:y_start+len_y,x_start:x_start+len_x]
return pix_crop imt='pa'
if imt=='pa':
ly=350 lx=300 elif imt=='lat':
ly= 350 lx = 300 elif imt=='obl':
ly=350 lx=300 elif imt=='up':
ly=350
lx=250
crop_img=crop_dicom(image, y_start= 50, x_start=0, len_y=ly,len_x=lx) plt.imshow(crop_img, cmap=plt.cm.bone)
shape=crop_img.shape
print('date_time_stamp:',date, time) print(shape)
#save cropped radiographs
def append_data(img_array, ptno, shape, df, type_):
newrow=[ptno, shape, img_array, type_]
df.loc[len(df)]=newrow return df
df=append_data(crop_img, p, shape, df, imt) df.iloc[-5:,:]
#write to disk
df.to_pickle(savefile)