-
Notifications
You must be signed in to change notification settings - Fork 1
/
DarkFieldErrorMeasures.java
190 lines (145 loc) · 5.01 KB
/
DarkFieldErrorMeasures.java
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package edu.stanford.rsl.science.darkfield.FlorianDarkField;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import edu.stanford.rsl.conrad.data.numeric.Grid3D;
import edu.stanford.rsl.conrad.data.numeric.NumericGridOperator;
import edu.stanford.rsl.conrad.numerics.SimpleMatrix;
import edu.stanford.rsl.conrad.numerics.SimpleOperators;
import edu.stanford.rsl.conrad.numerics.SimpleVector;
public class DarkFieldErrorMeasures {
public static enum DarkFieldNormType{
NORM_L1,
NORM_L2,
NORM_MAX
}
/**
* Calculates the angular distance of all reconstructed fiber directions
* @param A
* @param B
* @return
*/
public static Grid3D errorAngularDistanceGrid(DarkFieldTensorClass A, DarkFieldTensorClass B,DarkField3DTensorVolume mask){
// Check for inconsistency (different dimensions)
assert(A.imgSizeX == B.imgSizeX
&&A.imgSizeY == B.imgSizeY
&&A.imgSizeZ == B.imgSizeZ
): new Exception("Dimension of data is wrong.");
Grid3D angularDiffGrid = new Grid3D(A.imgSizeX, A.imgSizeY, A.imgSizeZ);
DarkFieldVectorField dirA = A.getFiberDirection();
DarkFieldVectorField dirB = B.getFiberDirection();
for(int x = 0; x < A.imgSizeX; x ++){
for(int y = 0; y < A.imgSizeY; y++){
for(int z = 0; z < A.imgSizeZ; z++){
if(mask.getAtIndex(x, y, z, 0) == 0){
angularDiffGrid.setAtIndex(x,y,z,0f);
} else {
SimpleVector vecA = dirA.getSimpleVectorAtIndex(x, y, z).normalizedL2();
SimpleVector vecB = dirB.getSimpleVectorAtIndex(x, y, z).normalizedL2();
// Calculate inner product
double inner = SimpleOperators.multiplyInnerProd(vecA,vecB);
// Take absolute value
inner = Math.abs(inner);
/*
* Calculate angle and set it to grid
*/
angularDiffGrid.setAtIndex(x,y,z,(float)Math.acos(inner));
}
}
}
}
return angularDiffGrid;
}
/**
* Calculates the average angular distance of all reconstructed fiber directions
* @param A
* @param B
* @return
*/
public static double errorAngularDistance(DarkFieldTensorClass A, DarkFieldTensorClass B, DarkField3DTensorVolume mask){
// Get number of non zero elements
double normFactor = NumericGridOperator.getInstance().normL1(mask);
Grid3D diff = errorAngularDistanceGrid(A,B,mask);
double norm = NumericGridOperator.getInstance().normL1(diff)/normFactor;
return norm;
}
/**
* Normalized residual norms
* (see. Vogel - Constrained ... eq. 21 on page 13)
* @param A - Normalized in respect to A
* @param B
* @param normType
* @return
*/
public static double errorSinogam(DarkField3DSinogram A, DarkField3DSinogram B,DarkFieldNormType normType){
DarkField3DSinogram diff = DarkField3DSinogram.sub(A,B);
if(normType == DarkFieldNormType.NORM_L1){
double residualNorm = diff.norm1();
double normalisedResidualNorm = residualNorm/A.norm1();
return normalisedResidualNorm;
} else if(normType == DarkFieldNormType.NORM_L2){
double residualNorm = diff.norm2();
double normalisedResidualNorm = residualNorm/A.norm2();
return normalisedResidualNorm;
}else{
return Double.NaN;
}
}
/**
* @param diff
* @param normFactor
* @param normType
* @return
*/
public static double errorSinogam(DarkField3DSinogram diff, double normFactor,DarkFieldNormType normType){
double residualNorm;
if(normType == DarkFieldNormType.NORM_L1){
residualNorm = diff.norm1();
} else if(normType == DarkFieldNormType.NORM_L2){
residualNorm = diff.norm2();
}else{
return Double.NaN;
}
double normalisedResidualNorm = residualNorm/normFactor;
return normalisedResidualNorm;
}
/**
* Normalised mean updates
* (see eq. 22 of Vogel - constrained XTT)
* @param A
* @param B - Normalized with respect to B
* @return
*/
public static double errorDarkFieldCoefficients(DarkField3DTensorVolume A, DarkField3DTensorVolume B,DarkFieldNormType normType ){
DarkField3DTensorVolume diff = DarkField3DTensorVolume.sub(A, B);
if(normType == DarkFieldNormType.NORM_L1){
return diff.norm1()/B.norm1();
} else if(normType == DarkFieldNormType.NORM_L2){
return diff.norm2()/B.norm2();
}else{
return Double.NaN;
}
}
/**
* @param folder
* @param fileName
* @param data
*/
public static void writeErrorToTxt(File folder, String fileName, SimpleMatrix data){
try {
File newTextFile = new File(folder.getParent() + "\\" + fileName);
FileWriter fw = new FileWriter(newTextFile);
for(int row = 0; row < data.getRows(); row++){
SimpleVector rowVec = data.getRow(row);
for( int col = 0; col < data.getCols(); col++){
fw.write(rowVec.getElement(col) + " ");
}
fw.write(System.getProperty( "line.separator" ));
}
fw.close();
} catch (IOException iox) {
//do stuff with exception
iox.printStackTrace();
}
}
}