[c7c3a1]: / src / processLabels.py

Download this file

120 lines (98 with data), 4.5 kB

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