#!/usr/bin/env python

# This script belongs to https://github.com/

import os,glob
import argparse

import numpy as np
import SimpleITK as sitk

DATA_DIRECTORY = '/home/zack/Data/LUNA16/'

def mhd2raw(data_file):
    itkimg = sitk.ReadImage(data_file)
    img=sitk.GetArrayFromImage(itkimg)
    img = np.transpose(img,(1,2,0))
    return img

def mhd2raw_disk(data_dir):

    for i in xrange(10):
        os.chdir(data_dir + "subset" + str(i))
        if not os.path.exists(data_dir + "subset" + str(i) + "raw"):
            os.mkdir(data_dir + "subset" + str(i) + "raw")
        for file in glob.glob("*.mhd"):
            img = mhd2raw(file)
            np.save(os.path.join(data_dir + "subset" + str(i) + "raw",file),img)

    os.chdir(data_dir + "seg-lungs-LUNA16")

    if not os.path.exists(data_dir + "seg-lungs-LUNA16" + "raw"):
        os.mkdir(data_dir + "seg-lungs-LUNA16" + "raw")
    for file in glob.glob("*.mhd"):
        img = mhd2raw(file)
        np.save(os.path.join(data_dir + "seg-lungs-LUNA16" + "raw", file), img)


def main():

    parser = argparse.ArgumentParser()

    parser = argparse.ArgumentParser(description="mdh to raw file converter")

    parser.add_argument("--data-dir", type=str, default=DATA_DIRECTORY,
                        help="Path to the directory containing the LUNA16 dataset.")


    args = parser.parse_args()
    mhd2raw_disk(args.data_dir)


if __name__ == '__main__':
    main()

@@ -391,16 +391,58 @@
 ge(10):%0A
+        print %22converting subset %22+str(i)%0A
         
@@ -538,24 +538,25 @@
 + str(i) + %22
+b
 raw%22):%0A     
@@ -599,24 +599,25 @@
 + str(i) + %22
+b
 raw%22)%0A      
@@ -686,38 +686,41 @@
 le)%0A            
-np.sav
+img.tofil
 e(os.path.join(d
@@ -746,24 +746,25 @@
 + str(i) + %22
+b
 raw%22,file),i
@@ -765,12 +765,8 @@
 ile)
-,img
 )%0A%0A 
@@ -807,16 +807,45 @@
 LUNA16%22)
+%0A    print %22seg-lungs-LUNA16%22
 %0A%0A    if
@@ -893,24 +893,25 @@
 -LUNA16%22 + %22
+b
 raw%22):%0A     
@@ -951,24 +951,25 @@
 -LUNA16%22 + %22
+b
 raw%22)%0A    fo
@@ -1038,14 +1038,17 @@
     
-np.sav
+img.tofil
 e(os
@@ -1091,16 +1091,17 @@
 A16%22 + %22
+b
 raw%22, fi
@@ -1107,13 +1107,8 @@
 ile)
-, img
 )%0A%0A%0A

fix to write numpy array to binray file