spam-moveLabels 3.5 KB
Newer Older
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
#!/usr/bin/env python

"""
This python script deforms a labelled image based on a deformation field using SPAM functions
Copyright (C) 2020 SPAM Contributors

This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
more details.

You should have received a copy of the GNU General Public License along with
this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from __future__ import print_function

import spam.helpers
import spam.label as ltk
import spam.DIC
import spam.deformation
import numpy
import sys, os
import tifffile
#import matplotlib.pyplot as plt
# Added from rotation_correlation-style parallelisation
import multiprocessing
import argparse
import progressbar


# Define argument parser object
parser = argparse.ArgumentParser(description="spam-moveLabels "+spam.helpers.optionsParser.GLPv3descriptionHeader +\
                                             "This script applies discretely measured deformation functions (Phi) coming from 'spam-ddic' to a labelled image, "+\
                                             "thus generating the deformed labelled image.\n\nWarning: since we're moving labels, "+\
                                             "nearest neighbour interpolation must be used, and thus the shapes of the labels will be slightly damaged",
                                 formatter_class=argparse.RawTextHelpFormatter)

# Parse arguments with external helper function
args = spam.helpers.optionsParser.moveLabelsParser(parser)


print("+-----------------------------+")
49
print("|    SPAM - Label mover       |")
50
51
52
53
54
55
56
57
58
print("+-----------------------------+")

print("\nCurrent Settings:")
argsDict = vars(args)
for key in sorted(argsDict):
    print("\t{}: {}".format(key, argsDict[key]))

if args.RETURN_STATUS_THRESHOLD is None:
    DVC = spam.helpers.readCorrelationTSV(args.TSVFile.name, readConvergence=False)
59
    RS = None
60
61
else:
    DVC = spam.helpers.readCorrelationTSV(args.TSVFile.name, readConvergence=True)
62
    RS = DVC['returnStatus']
63
64
65
66
67
68
69
70
71
72
73
74
75
76

# Read labelled image
lab = tifffile.imread( args.LabFile.name )


#if args.GREY_FILE is not None:
    #greyTmp = tifffile.imread(args.GREY_FILE)
    #grey = numpy.zeros((labShapeOrig[0]+2*args.OVERALL_MARGIN,
                        #labShapeOrig[1]+2*args.OVERALL_MARGIN,
                        #labShapeOrig[2]+2*args.OVERALL_MARGIN), dtype=greyTmp.dtype)
    #grey[slicePadToNonPad] = greyTmp

labOut = spam.label.moveLabels(lab,
                               DVC['PhiField'],
77
                               returnStatus=RS,
78
79
80
81
82
83
84
85
86
87
88
89
90
                               margin=args.MARGIN,
                               PhiCOM=args.PHICOM,
                               threshold=args.THRESH,
                               labelDilate=args.LABEL_DILATE,
                               numberOfThreads=args.NUMBER_OF_PROCESSES)

#if args.GREY_FILE is None:
print("\nSaving labelled image with displaced grains...", end='')
tifffile.imsave( args.OUT_DIR+"/"+args.PREFIX+".tif", labOut.astype(lab.dtype) )
#else:
    #print("\nSaving binary image with displaced grains...", end='')
    #tifffile.imsave( args.OUT_DIR+"/"+args.PREFIX+".tif", labOut[slicePadToNonPad].astype('<u1')*255 )
print("done.")