|
a |
|
b/src/processLabels.py |
|
|
1 |
""" |
|
|
2 |
Copyright (c) 2016, 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, 2016. |
|
|
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 label images") |
|
|
46 |
print(" --- argv 2: Folder to save corrected label images") |
|
|
47 |
print(" --- argv 3: Number of expected classes (including background)") |
|
|
48 |
print(" --- argv 4: Image type") |
|
|
49 |
print(" ------------- 0: nifti format") |
|
|
50 |
print(" ------------- 1: matlab format") |
|
|
51 |
|
|
|
52 |
def getImageImageList(imagesFolder): |
|
|
53 |
if os.path.exists(imagesFolder): |
|
|
54 |
imageNames = [f for f in os.listdir(imagesFolder) if isfile(join(imagesFolder, f))] |
|
|
55 |
|
|
|
56 |
imageNames.sort() |
|
|
57 |
|
|
|
58 |
return imageNames |
|
|
59 |
|
|
|
60 |
def checkAnotatedLabels(argv): |
|
|
61 |
# Number of input arguments |
|
|
62 |
# 1: Folder containing label images |
|
|
63 |
# 2: Folder to save corrected label images |
|
|
64 |
# 3: Number of expected classes (including background) |
|
|
65 |
# 4: Image type |
|
|
66 |
# 0: nifti format |
|
|
67 |
# 1: matlab format |
|
|
68 |
# Do some sanity checks |
|
|
69 |
|
|
|
70 |
if len(argv) < 4: |
|
|
71 |
printUsage(1) |
|
|
72 |
sys.exit() |
|
|
73 |
|
|
|
74 |
imagesFolder = argv[0] |
|
|
75 |
imagesFolderdst = argv[1] |
|
|
76 |
numClasses = int(argv[2]) |
|
|
77 |
imageType = int(argv[3]) |
|
|
78 |
|
|
|
79 |
imageNames = getImageImageList(imagesFolder) |
|
|
80 |
printFileNames = False |
|
|
81 |
|
|
|
82 |
for i_d in xrange(0, len(imageNames)) : |
|
|
83 |
if imageType == 0: |
|
|
84 |
imageFileName = imagesFolder + '/' + imageNames[i_d] |
|
|
85 |
[imageData,img_proxy] = load_nii(imageFileName, printFileNames) |
|
|
86 |
else: |
|
|
87 |
imageFileName = imagesFolder + '/' + imageNames[i_d] |
|
|
88 |
imageData = load_matlab(imageFileName, printFileNames) |
|
|
89 |
|
|
|
90 |
labelsOrig = np.unique(imageData) |
|
|
91 |
|
|
|
92 |
if (len(labelsOrig) != numClasses): |
|
|
93 |
print(" WARNING!!!!! Number of expected clases ({}) is different to found labels ({}) ".format(numClasses,len(labelsOrig))) |
|
|
94 |
|
|
|
95 |
# Correct labels |
|
|
96 |
labelCorrectedImage = np.zeros(imageData.shape,dtype=np.int8) |
|
|
97 |
for i_l in xrange(0,len(labelsOrig)): |
|
|
98 |
idx = np.where(imageData == labelsOrig[i_l]) |
|
|
99 |
labelCorrectedImage[idx] = i_l |
|
|
100 |
|
|
|
101 |
|
|
|
102 |
print(" ... Saving labels...") |
|
|
103 |
nameToSave = imagesFolderdst + '/' + imageNames[i_d] |
|
|
104 |
if imageType == 0: # nifti |
|
|
105 |
imageTypeToSave = np.dtype(np.int8) |
|
|
106 |
saveImageAsNifti(labelCorrectedImage, |
|
|
107 |
nameToSave, |
|
|
108 |
imageFileName, |
|
|
109 |
imageTypeToSave) |
|
|
110 |
else: # Matlab |
|
|
111 |
# Cast to int8 for saving purposes |
|
|
112 |
saveImageAsMatlab(labelCorrectedImage.astype('int8'),nameToSave) |
|
|
113 |
|
|
|
114 |
|
|
|
115 |
print " ****************************************** PROCESSING LABELS DONE ******************************************" |
|
|
116 |
|
|
|
117 |
|
|
|
118 |
if __name__ == '__main__': |
|
|
119 |
checkAnotatedLabels(sys.argv[1:]) |