Diff of /src/generateROI.py [000000] .. [e9ece0]

Switch to unified view

a b/src/generateROI.py
1
""" 
2
Copyright (c) 2017, Jose Dolz .All rights reserved.
3
Redistribution and use in source and binary forms, with or without modification,
4
are permitted provided that the following conditions are met:
5
    1. Redistributions of source code must retain the above copyright notice,
6
       this list of conditions and the following disclaimer.
7
    2. Redistributions in binary form must reproduce the above copyright notice,
8
       this list of conditions and the following disclaimer in the documentation
9
       and/or other materials provided with the distribution.
10
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
12
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
14
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
15
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
16
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
17
    OTHER DEALINGS IN THE SOFTWARE.
18
Jose Dolz. Dec, 2017.
19
email: jose.dolz.upv@gmail.com
20
LIVIA Department, ETS, Montreal.
21
"""
22
23
import sys
24
import pdb
25
from os.path import isfile, join
26
import os
27
import numpy as np
28
import nibabel as nib
29
import scipy.io as sio
30
31
from LiviaNet.Modules.IO.loadData import load_nii
32
from LiviaNet.Modules.IO.loadData import load_matlab
33
from LiviaNet.Modules.IO.saveData import saveImageAsNifti
34
from LiviaNet.Modules.IO.saveData import saveImageAsMatlab
35
36
# NOTE: Only has been tried on nifti images. However, it should not give any error for Matlab images. 
37
""" To print function usage """
38
def printUsage(error_type):
39
    if error_type == 1:
40
        print(" ** ERROR!!: Few parameters used.")
41
    else:
42
        print(" ** ERROR!!: ...") # TODO
43
        
44
    print(" ******** USAGE ******** ")
45
    print(" --- argv 1: Folder containing mr images")
46
    print(" --- argv 2: Folder to save corrected label images")
47
    print(" --- argv 3: Image type")
48
    print(" ------------- 0: nifti format")
49
    print(" ------------- 1: matlab format")
50
51
def getImageImageList(imagesFolder):
52
    if os.path.exists(imagesFolder):
53
       imageNames = [f for f in os.listdir(imagesFolder) if isfile(join(imagesFolder, f))]
54
55
    imageNames.sort()
56
57
    return imageNames
58
    
59
def checkAnotatedLabels(argv):
60
    # Number of input arguments
61
    #    1: Folder containing label images
62
    #    2: Folder to save corrected label images
63
    #    3: Image type
64
    #             0: nifti format
65
    #             1: matlab format  
66
    # Do some sanity checks
67
    
68
    if len(argv) < 3:
69
        printUsage(1)
70
        sys.exit()
71
    
72
    imagesFolder = argv[0]
73
    imagesFolderdst  = argv[1]
74
    imageType = int(argv[2])
75
    
76
    imageNames = getImageImageList(imagesFolder)
77
    printFileNames = False
78
    
79
    for i_d in xrange(len(imageNames)) :
80
        if imageType == 0:
81
            imageFileName = imagesFolder + '/' + imageNames[i_d]
82
            [imageData,img_proxy] = load_nii(imageFileName, printFileNames)
83
        else:
84
            imageFileName = imagesFolder + '/' + imageNames[i_d]
85
            imageData = load_matlab(imageFileName, printFileNames)
86
87
        # Find voxels different to 0
88
        # NOTE: I assume voxels equal to 0 are outside my ROI (like in the skull stripped datasets)
89
        idx = np.where(imageData > 0 )
90
91
        # Create ROI and assign those indexes to 1
92
        roiImage = np.zeros(imageData.shape,dtype=np.int8)
93
        roiImage[idx] = 1
94
        
95
        print(" ... Saving roi...")
96
        nameToSave =  imagesFolderdst + '/ROI_' + imageNames[i_d]
97
        if imageType == 0: # nifti
98
            imageTypeToSave = np.dtype(np.int8)
99
            saveImageAsNifti(roiImage,
100
                             nameToSave,
101
                             imageFileName,
102
                             imageTypeToSave)
103
        else: # Matlab
104
            # Cast to int8 for saving purposes
105
            saveImageAsMatlab(labelCorrectedImage.astype('int8'),nameToSave)
106
107
            
108
    print " ******************************************  PROCESSING LABELS DONE  ******************************************"
109
  
110
   
111
if __name__ == '__main__':
112
   checkAnotatedLabels(sys.argv[1:])