Skip to content

Instantly share code, notes, and snippets.

@EndingCredits
Created January 23, 2018 14:22
Show Gist options
  • Save EndingCredits/37d63a7681a5f7e2301252d3243646af to your computer and use it in GitHub Desktop.
Save EndingCredits/37d63a7681a5f7e2301252d3243646af to your computer and use it in GitHub Desktop.
Builds lung dataset from mat file
import scipy.io as sio
import numpy as np
import scipy.misc
mat_contents = sio.loadmat('LungTrainingData.mat')
#matrix is of the form [x,y,n]
#['CT2', 'Right2', 'Left2', '__header__', '__globals__', '__version__', 'Trachea2']
images = mat_contents['CT2']
left_lung = mat_contents['Left2']
right_lung = mat_contents['Right2']
trachea = mat_contents['Trachea2']
m = np.max(images)
n = np.min(images)
print m
print n
shuffled = range(513)
np.random.shuffle(shuffled)
for i in range(513):
valid = i>500
t_v = 'validation' if valid else 'test'
ind = shuffled[i]
scipy.misc.toimage(images[:,:,ind], cmin=n, cmax=m).save('data/' + t_v + '/img' + str(i) + '.png')
left = left_lung[:,:,ind]
right = right_lung[:,:,ind]
trach = trachea[:,:,ind]
ann = left + right * 2 + trach * 3
scipy.misc.toimage(ann, cmin=0, cmax=255).save('data/' + t_v + '/annotation' + str(i) + '.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment