-
Notifications
You must be signed in to change notification settings - Fork 238
/
Registration2.java
325 lines (258 loc) · 9.26 KB
/
Registration2.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package edu.stanford.rsl.tutorial.dmip;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Map.Entry;
import edu.stanford.rsl.conrad.data.numeric.Grid1D;
import edu.stanford.rsl.conrad.data.numeric.Grid2D;
import edu.stanford.rsl.conrad.data.numeric.Grid3D;
import edu.stanford.rsl.conrad.geometry.transforms.AffineTransform;
import edu.stanford.rsl.conrad.numerics.SimpleMatrix;
import edu.stanford.rsl.conrad.utils.ImageUtil;
import edu.stanford.rsl.conrad.utils.VisualizationUtil;
import edu.stanford.rsl.conrad.numerics.SimpleVector;
import edu.stanford.rsl.jpop.FunctionOptimizer;
import edu.stanford.rsl.jpop.OptimizableFunction;
import edu.stanford.rsl.jpop.OptimizationOutputFunction;
import edu.stanford.rsl.jpop.FunctionOptimizer.OptimizationMode;
import ij.IJ;
import ij.ImageJ;
import ij.gui.PlotWindow;
/**
* Exercise 7 of Diagnostic Medical Image Processing (DMIP)
* Using Mutual Information to solve the registration problem
* @author Bastian Bier
*
*/
public class Registration2 {
public Grid2D reference = null;
public Grid2D image = null;
public Grid3D movingStack = null;
public int iteration = 0;
double[] saveParameters;
class CostFunction implements OptimizableFunction, OptimizationOutputFunction{
private TreeMap<Integer,Double> resultVisualizer;
private PlotWindow resultVisualizerPlot;
@Override
public void setNumberOfProcessingBlocks(int number) {
}
@Override
public int getNumberOfProcessingBlocks() {
return 1;
}
@Override
public double evaluate(double[] x, int block) {
int nrChanges = 0;
for (int i = 0; i < x.length; i++) {
if(saveParameters[i]!=x[i]){
nrChanges++;
}
}
// Define Rotation
SimpleMatrix r = new SimpleMatrix(2, 2);
double phi2 = x[0] * (2 * Math.PI) / 360;
r.setElementValue(0, 0, Math.cos(phi2));
r.setElementValue(0, 1, -Math.sin(phi2));
r.setElementValue(1, 0, Math.sin(phi2));
r.setElementValue(1, 1, Math.cos(phi2));
// Define translation
double t_x = x[1];
double t_y = x[2];
Grid2D im_tmp = new Grid2D(image);
// Perform rotation/translation
SimpleVector t = new SimpleVector(t_x, t_y);
AffineTransform affine = new AffineTransform(r, t);
im_tmp.applyTransform(affine);
if (nrChanges >= 3) {
movingStack.setSubGrid(iteration, im_tmp);
iteration++;
}
// Calculate the cost function
double cost = calculateMutualInformation(reference, im_tmp);
System.arraycopy(x, 0, saveParameters, 0, x.length);
return cost;
}
@Override
public void optimizerCallbackFunction(int currIterationNumber, double[] x, double currFctVal,
double[] gradientAtX) {
// Visualization of cost function value over time
if (this.resultVisualizer == null)
resultVisualizer = new TreeMap<Integer, Double>();
resultVisualizer.put(currIterationNumber, currFctVal);
if (resultVisualizerPlot != null)
resultVisualizerPlot.close();
Grid1D out = new Grid1D(resultVisualizer.size());
Iterator<Entry<Integer,Double>> it = resultVisualizer.entrySet().iterator();
while (it.hasNext()) {
Entry<Integer,Double> e = it.next();
out.setAtIndex(e.getKey(), e.getValue().floatValue());
}
resultVisualizerPlot = VisualizationUtil.createPlot(out.getBuffer()).show();
}
}
/**
* Method to calculate the Mutual Information
* @param ref reference image
* @param mov moving image
* @return negative mutual information
*/
private double calculateMutualInformation(Grid2D ref, Grid2D mov){
int histSize = 256;
// Step 1: Calculate joint histogram
SimpleMatrix jointHistogram = calculateJointHistogram(ref, mov);
// Step 2: Get histogram for a single image from the joint histogram
// a) for the first image
SimpleVector histo1 = new SimpleVector(histSize);
histo1 = getHistogramFromJointHistogram(jointHistogram);
// b) for the second image
SimpleVector histo2 = new SimpleVector(histSize);
SimpleMatrix jh_t = jointHistogram.transposed();
histo2 = getHistogramFromJointHistogram(jh_t);
// Step 3: Calculate the marginal entropies and the joint entropy
double entropy_jointHisto = 0;
double entropy_histo1 = 0;
double entropy_histo2 = 0;
for(int i = 0; i < histSize; i++)
{
if(histo1.getElement(i) != 0)
{
// TODO: calculate entropy for histogram 1
}
if(histo2.getElement(i) != 0)
{
// TODO: calculate entropy for histogram 2
}
}
for (int i = 0; i < histSize; i++)
{
for (int j = 0; j < histSize; j++)
{
if(jointHistogram.getElement(i, j) != 0)
{
// TODO: calculate entropy of the joint histogram
}
}
}
// make sure to consider the - in from of the sum (Entropy formula)
// TODO
// TODO
// TODO
// Step 4: Calculate the mutual information
// Note: The mutual information is high for a good match
// but we require a minimization problem --> the result is inverted to fit the optimizer
double mutual_information = 0;
// TODO: calculate the mutual information
return mutual_information * 1000;
}
/**
* Method to calculate the joint histogram of two images
* @param im1 image1
* @param im2 image2
* @return a SimpleMatrix corresponding to the joint histogram
*/
private SimpleMatrix calculateJointHistogram(Grid2D im1, Grid2D im2){
// Calculate joint histogram
int histSize = 256;
SimpleMatrix jH = new SimpleMatrix(histSize, histSize);
for (int i = 0; i < histSize; i++) {
for (int j = 0; j < histSize; j++) {
// TODO
}
}
// Divide by the number of elements in order to get probabilities
for (int i = 0; i < histSize; i++) {
for (int j = 0; j < histSize; j++) {
// TODO
}
}
return jH;
}
/**
* Method to calculate a histogram from a joint histogram
* @param jH The joint histogram
* @return a SimpleVector corresponding to the marginal histogram
*/
private SimpleVector getHistogramFromJointHistogram(SimpleMatrix jH){
// Calculate histogram from joint histogram
int histSize = 256;
SimpleVector hist = new SimpleVector(histSize);
hist.zeros();
for(int i = 0; i < histSize; i++)
{
for(int j = 0; j < histSize; j++)
{
// TODO: sum up over the columns
}
}
return hist;
}
private double[] performOptimization(){
FunctionOptimizer fo = new FunctionOptimizer();
fo.setDimension(3);
fo.setNdigit(6);
fo.setItnlim(50);
fo.setMsg(16);
fo.setInitialX(new double[]{0,0,0});
fo.setMaxima(new double[]{50,50,50});
fo.setMinima(new double[]{-50,-50,-50});
fo.setOptimizationMode(OptimizationMode.Function);
CostFunction cF = new CostFunction();
movingStack = new Grid3D(reference.getWidth(), reference.getHeight(), 1000, false);
iteration = 0;
saveParameters = new double[]{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY};
// Optimization visualized
ArrayList<OptimizationOutputFunction> visFcts = new ArrayList<OptimizationOutputFunction>();
visFcts.add(cF);
fo.setCallbackFunctions(visFcts);
double result[] = fo.optimizeFunction(cF);
return result;
}
public static void main(String[] args){
ImageJ ij = new ImageJ();
// Load images
// TODO Adjust paths
String filename1 = "C:/StanfordRepo/CONRAD/src/edu/stanford/rsl/tutorial/dmip/T1.png";
String filename2 = "C:/StanfordRepo/CONRAD/src/edu/stanford/rsl/tutorial/dmip/Proton.png";
Grid2D image1 = ImageUtil.wrapImagePlus(IJ.openImage(filename1)).getSubGrid(0);
Grid2D image2 = ImageUtil.wrapImagePlus(IJ.openImage(filename2)).getSubGrid(0);
image1.show("Input Image 1");
image2.show("Input Image 2");
// Set the Origin of the image in its center
// The default origin of an image is in its top left corner
// Default Origin: [0.0, 0.0]
int w = image1.getWidth();
int h = image1.getHeight();
image1.setOrigin(-(w-1) / 2 , -(h-1)/2);
image2.setOrigin(-(w-1) / 2 , -(h-1)/2);
image1.setSpacing(1);
image2.setSpacing(1);
// Blurred Images for the registration to avoid local minima during optimization
Grid2D image1_blurred = new Grid2D(image1);
Grid2D image2_blurred = new Grid2D(image2);
IJ.run(ImageUtil.wrapGrid(image1_blurred,""),"Gaussian Blur...", "sigma=4");
IJ.run(ImageUtil.wrapGrid(image2_blurred,""),"Gaussian Blur...", "sigma=4");
Registration2 reg2 = new Registration2();
reg2.reference = image1_blurred;
reg2.image = image2_blurred;
// Perform Optimization
double res[] = reg2.performOptimization();
// Stack for visualization purposes only
Grid3D optimizationStepsGrid = new Grid3D(reg2.reference.getWidth(), reg2.reference.getHeight(), reg2.iteration, false);
for (int i = 0; i < optimizationStepsGrid.getSize()[2]; i++) {
optimizationStepsGrid.setSubGrid(i, reg2.movingStack.getSubGrid(i));
}
optimizationStepsGrid.show("Optimization Steps");
// Transform image back
SimpleMatrix r = new SimpleMatrix(2,2);
Grid2D registeredImage = new Grid2D(image2);
double phi = (2 * Math.PI) / 360 * res[0];
r.setElementValue(0, 0, Math.cos(phi));
r.setElementValue(0, 1, -Math.sin(phi));
r.setElementValue(1, 0, Math.sin(phi));
r.setElementValue(1, 1, Math.cos(phi));
SimpleVector t2 = new SimpleVector(res[1], res[2]);
AffineTransform affine2 = new AffineTransform(r, t2);
registeredImage.applyTransform(affine2);
registeredImage.show("Registered Image");
}
}