-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAlignment_Roi.java
More file actions
459 lines (374 loc) · 13.9 KB
/
Alignment_Roi.java
File metadata and controls
459 lines (374 loc) · 13.9 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
Copyright (c) 2010 Michael Mohn and Jannik Meyer, Ulm University
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/>.
*/
import ij.*;
import ij.io.*;
import ij.gui.*;
import ij.process.*;
import ij.plugin.filter.*;
import java.awt.*;
import java.io.*;
import java.util.*;
public class Alignment_Roi implements PlugInFilter {
ImagePlus imp;
int stackSize;
int selectedSlice; // index of selected slice when plugin is started
Rectangle roiRect; // Roi
int roiWidth;
int roiHeight;
double[][] refImage; // pixel array for error computation
int[] correctionX; // array for corrections in x direction
int[] correctionY; // array for corrections in y direction
// plugin parameters
int range; // range of possible translations that are checked
double power; // loading of error
int firstSlice; // index of first corrected slice
int lastSlice; // index of last corrected slice
String adjustTo; // contains answer to "adjust to" choice
boolean correctPrevious; // whether slices before firstSlice should be corrected
boolean correctFollowing; // same for slices after lastSlice
boolean prevSlice; // for compare to previous slice mode
double minerror; // min. of computed errors
int bestXcorr; // correction with least error sum
int bestYcorr;
int refSlice; // index of start slice / slice for refImage
boolean saveFile; // -> save in MultiStackReg File
boolean doTranslate; // -> apply corrections
public int setup(String arg, ImagePlus imp) {
this.imp = imp;
return DOES_ALL;
}
public void run(ImageProcessor ip) {
try {
// get Roi
roiRect = imp.getRoi().getBounds();
int roiX = (int) roiRect.getX();
int roiY = (int) roiRect.getY();
roiWidth = (int) roiRect.getWidth();
roiHeight = (int) roiRect.getHeight();
//IJ.showMessage(roiWidth + " x " + roiHeight + " at " + roiX + ", " + roiY); // uncomment to check Roi
// get number of slices and currently selected slice
stackSize = imp.getStack().getSize();
selectedSlice = imp.getSlice();
//
// dialog for plugin parameters
//
GenericDialog gd = new GenericDialog("Alignment");
gd.addNumericField("Range (px): +-", 5, 0); // range of checked corrections
gd.addNumericField("Error exponent", 2.0, 2); // error loading
// plugin mode: compare all slices with selected slice or with their neighbor
String[] choices = {"selected slice", "previous slice"};
gd.addChoice("Compare with...", choices, "selected slice");
// range of corrected slices
gd.addNumericField("Correct translation from slice", 1, 0);
gd.addNumericField("to", stackSize, 0);
// set reference slice with correction (0, 0): first slice, last slice or current slice
String[] choices2 = {"first slice of range", "last slice of range", "currently selected slice"};
gd.addChoice("Adjust to...", choices2, "first slice of range");
// correct head and tail options
gd.addCheckbox("Correct previous slices", false);
gd.addCheckbox("Correct following slices", false);
// save MultiStackReg file option
gd.addCheckbox("Save MultiStackReg File", false);
// apply translations option
gd.addCheckbox("Apply translations", true);
// show dialog
gd.showDialog();
if (gd.wasCanceled()) {
IJ.error("Plugin canceled!");
return;
}
// get values from dialog
range = (int) gd.getNextNumber();
power = gd.getNextNumber(); // error loading
if (gd.getNextChoiceIndex() == 1) prevSlice = true; else prevSlice = false; // plugin mode
// range of slices
firstSlice = (int) gd.getNextNumber();
lastSlice = (int) gd.getNextNumber();
// correct some errors in user input
if (firstSlice > lastSlice) { // correct invalid range: swap first and last slice
IJ.showMessage("Hint", "Range of slices is invalid. Range will be turned the other way round.");
int temp;
temp = firstSlice; // swap first and last slice
firstSlice = lastSlice;
lastSlice = temp;
}
if (firstSlice < 1) { // if start of range is invalid
firstSlice = 1;
IJ.showMessage("Hint", "Range of slices is invalid. Beginning of range is corrected to first slice.");
}
if (lastSlice > stackSize) { // if end of range is invalid
lastSlice = stackSize;
IJ.showMessage("Hint", "Range of slices is invalid. End of range is corrected to last possible slice.");
}
adjustTo = gd.getNextChoice(); // adjust to... option
correctPrevious = gd.getNextBoolean(); // correct slices before range
correctFollowing = gd.getNextBoolean(); // correct slices after range
saveFile = gd.getNextBoolean(); // save to MultiStackReg file checkbox
doTranslate = gd.getNextBoolean(); // whether translations are applied
// cancel plugin if results would never be used
if (!(saveFile || doTranslate)) {
IJ.error("Please choose at least 'Apply translations' or file output. Plugin canceled.");
return;
}
// avoid problems with invalid refSlice, adjustSlice
if (selectedSlice > lastSlice || selectedSlice < firstSlice) {
IJ.error(
"Error: currently selected slice is beyond entered range. Set Roi in a slice which has to be corrected and restart Plugin.");
return;
}
IJ.showStatus("Computing corrections. Please wait...");
//
// arrays for x and y correcions
//
correctionX = new int[stackSize];
correctionY = new int[stackSize];
//
// compute corrections
//
// use firstSlice as first reference slice in previous slice mode
if (prevSlice) imp.setSlice(firstSlice);
// copy values from reference image to 2d array
refImage = new double[roiWidth][roiHeight];
for (int i = 0; i < roiWidth; i++) {
for (int j = 0; j < roiHeight; j++) {
refImage[i][j] = ip.getPixelValue(roiX + i, roiY + j);
}
}
if (prevSlice) { // prevSlice mode
refSlice = selectedSlice;
correctionX[0] = 0;
correctionY[0] = 0;
if (refSlice < lastSlice) // avoid exception when refSlice == lastSlice
for (int slice = refSlice + 1; slice <= lastSlice; slice++) { // for every slice do...
computeBestCorr(ip, slice, roiX, roiY);
if (slice == refSlice + 1) { // first iteration: use correction to initialize bestXcorr, bestYcorr
correctionX[slice-1] = bestXcorr;
correctionY[slice-1] = bestYcorr;
}
else {
correctionX[slice-1] = correctionX[slice-2] + bestXcorr; // absolute correction of slice
correctionY[slice-1] = correctionY[slice-2] + bestYcorr; // uses correction of previous slice
}
roiX -= bestXcorr; // Roi adjustment: Roi moves, corrections are relative to previous slice
roiY -= bestYcorr;
// copy current image to refImage
for (int i = 0; i < roiWidth; i++) {
for (int j = 0; j < roiHeight; j++) {
refImage[i][j] = ip.getPixelValue(roiX + i, roiY + j);
}
}
}
// reset Roi (begin at refSlice again)
roiX = (int) roiRect.getX();
roiY = (int) roiRect.getY();
// reset refImage
imp.setSlice(refSlice);
for (int i = 0; i < roiWidth; i++) {
for (int j = 0; j < roiHeight; j++) {
refImage[i][j] = ip.getPixelValue(roiX + i, roiY + j);
}
}
// same procedure as above in other direction:
if (refSlice > firstSlice) // avoid exception when refSlice == firstSlice
for (int slice = refSlice -1; slice >= firstSlice; slice--) { // for every slice do...
computeBestCorr(ip, slice, roiX, roiY);
if (slice == firstSlice - 1) {
correctionX[slice-1] = bestXcorr;
correctionY[slice-1] = bestYcorr;
}
else {
correctionX[slice-1] = correctionX[slice] + bestXcorr;
correctionY[slice-1] = correctionY[slice] + bestYcorr;
}
roiX -= bestXcorr;
roiY -= bestYcorr;
// copy current image to refImage
for (int i = 0; i < roiWidth; i++) {
for (int j = 0; j < roiHeight; j++) {
refImage[i][j] = ip.getPixelValue(roiX + i, roiY + j);
}
}
}
}
else { // compare with selected slice mode
refSlice = selectedSlice;
correctionX[refSlice-1] = 0; // correction of refSlice is (0, 0)
correctionY[refSlice-1] = 0;
if (refSlice > 1)
for (int slice = refSlice-1; slice >= firstSlice; slice--) { // go down starting from selected slice
computeBestCorr(ip, slice, roiX, roiY);
roiX -= bestXcorr;
roiY -= bestYcorr;
if (slice == refSlice-1) {
correctionX[slice-1] = bestXcorr;
correctionY[slice-1] = bestYcorr;
}
else {
correctionX[slice-1] = correctionX[slice] + bestXcorr;
correctionY[slice-1] = correctionY[slice] + bestYcorr;
}
}
// reset Roi (beginning from refSlice again)
roiX = (int) roiRect.getX();
roiY = (int) roiRect.getY();
if (refSlice < lastSlice)
for (int slice = refSlice+1; slice <= lastSlice; slice++) { // go up starting from selected slice
computeBestCorr(ip, slice, roiX, roiY);
roiX -= bestXcorr;
roiY -= bestYcorr;
if (slice == refSlice+1) {
correctionX[slice-1] = bestXcorr;
correctionY[slice-1] = bestYcorr;
}
else {
correctionX[slice-1] = correctionX[slice-2] + bestXcorr;
correctionY[slice-1] = correctionY[slice-2] + bestYcorr;
}
}
}
//
// adjust to another slice than refSlice
//
int adjustSlice = firstSlice; // default initialization
// use choice "Adjust to..."
if (adjustTo.equals("last slice of range")) adjustSlice = lastSlice;
if (adjustTo.equals("currently selected slice")) adjustSlice = selectedSlice;
// use offset of adjustSlice
int offsetX = correctionX[adjustSlice-1];
int offsetY = correctionY[adjustSlice-1];
for (int i = firstSlice; i <= lastSlice; i++) {
correctionX[i-1] -= offsetX;
correctionY[i-1] -= offsetY;
}
//
// correct previous and correct following slices
//
if (correctPrevious) {
for (int i = 1; i < firstSlice; i++) {
correctionX[i-1] = correctionX[firstSlice-1];
correctionY[i-1] = correctionY[firstSlice-1];
}
}
if (correctFollowing) {
for (int i = lastSlice; i <= stackSize; i++) {
correctionX[i-1] = correctionX[lastSlice-1];
correctionY[i-1] = correctionY[lastSlice-1];
}
}
//
// save MultiStackReg file
//
if (saveFile) {
SaveDialog sd = new SaveDialog("Save MultiStackReg File...", "translations", ".txt");
String directory = sd.getDirectory();
String fileName = sd.getFileName();
if (fileName == null) return;
try {
FileWriter fw = new FileWriter(directory + fileName);
fw.write("MultiStackReg Transformation File\n");
fw.write("File Version 1.0\n");
fw.write("0\n"); // no two stack align (MultiStackReg), otherwise: 1
int x0 = ip.getWidth() / 2;
int y0 = ip.getHeight() / 2;
int[] x = new int[stackSize];
int[] y = new int[stackSize];
if (refSlice > 1)
for (int i = refSlice-1; i >= 1; i--) {
x[i-1] = x0 - correctionX[i-1] + correctionX[i];
y[i-1] = y0 - correctionY[i-1] + correctionY[i];
}
if (refSlice < lastSlice)
for (int i = refSlice+1; i <= stackSize; i++) {
x[i-1] = x0 - correctionX[i-1] + correctionX[i-2];
y[i-1] = y0 - correctionY[i-1] + correctionY[i-2];
}
if (refSlice > 1)
for (int i = refSlice-1; i >= 1; i--) {
fw.write("TRANSLATION\n");
fw.write("Source img: " + i + " Target img: " + refSlice + "\n");
fw.write(x[i-1] + "\t" + y[i-1] + "\n");
fw.write("0.0\t0.0\n0.0\t0.0\n");
fw.write("" + "\n");
fw.write(x0 + "\t" + y0 + "\n");
fw.write("0.0\t0.0\n0.0\t0.0\n");
fw.write("" + "\n");
}
if (refSlice < lastSlice)
for (int i = refSlice+1; i <= stackSize; i++) {
fw.write("TRANSLATION\n");
fw.write("Source img: " + i + " Target img: " + refSlice + "\n");
fw.write(x[i-1] + "\t" + y[i-1] + "\n");
fw.write("0.0\t0.0\n0.0\t0.0\n");
fw.write("" + "\n");
fw.write(x0 + "\t" + y0 + "\n");
fw.write("0.0\t0.0\n0.0\t0.0\n");
fw.write("" + "\n");
}
fw.close();
} catch (IOException e) {IJ.showMessage("Saving MultiStackReg File failed.");}
}
//
// apply corrections
//
if (doTranslate) {
imp.unlock();
imp.killRoi();
IJ.showStatus("Translating Images...");
for (int i = 1; i <= stackSize; i++) {
imp.setSlice(i);
IJ.run("Translate...", "x=" + correctionX[i - 1] + " y=" + correctionY[i - 1] +
" interpolation=None slice");
}
IJ.showStatus("");
imp.setRoi(roiRect);
}
IJ.showStatus("");
imp.setRoi(roiRect);
} catch (Exception e) {
IJ.showMessage("Error! Check Image and Roi.");
IJ.showStatus("");
}
} // end of run method
public double computeError(ImageProcessor ip, int xzero, int yzero) { // computes error for given translations xzero, yzero
double errorsum = 0;
for (int x = xzero; x < xzero + roiWidth; x++) {
for (int y = yzero; y < yzero + roiHeight; y++) {
double error = Math.pow(Math.abs(ip.getPixelValue(x, y) - refImage[x-xzero][y-yzero]), power);
errorsum += error;
}
}
return errorsum;
}
public void computeBestCorr(ImageProcessor ip, int slice, int roiX, int roiY) { // computes best correction for a slice
double minerror = 0;
bestXcorr = 0;
bestYcorr = 0;
imp.setSlice(slice);
for (int xtrans = - range; xtrans <= range; xtrans++) { // check all possible translations
for (int ytrans = - range; ytrans <= range; ytrans++) {
double error = computeError(ip, roiX + xtrans, roiY + ytrans);
if ((xtrans == - range) && (ytrans == - range)) { // true for 1st iteration
minerror = error;
bestXcorr = -xtrans;
bestYcorr = -ytrans;
}
if (error < minerror) { // found new min. error
minerror = error;
bestXcorr = -xtrans;
bestYcorr = -ytrans;
}
}
}
}
}