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
|
package ij.process;
import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.gui.Roi;
import ij.plugin.filter.ThresholdToSelection;
/*
* This plugin takes a binary stack as input, where some slices are
* labeled (i.e. contain white regions), and some are not. The unlabaled
* regions are interpolated by weighting the signed integer distance
* transformed labeled slices.
*
* from:
* http://fiji.sc/cgi-bin/gitweb.cgi?p=fiji.git;a=blob_plain;f=src-plugins/VIB-lib/vib/BinaryInterpolator.java;h=f6a610659ad624d13f94639bc5c0149712071f9f;hb=refs/heads/master
*/
public class BinaryInterpolator {
int[][] idt;
int w, h;
public void run(ImagePlus image, Roi[] rois) {
w = image.getWidth();
h = image.getHeight();
ImageStack stack = new ImageStack(w, h);
int firstIndex = -1, lastIndex = -1;
for(int i = 1; i < rois.length; i++) {
if(rois[i] != null) {
firstIndex = (firstIndex == -1) ? i : firstIndex;
lastIndex = i;
}
}
if (firstIndex == -1) {
IJ.error("There must be at least one selection in order to interpolate.");
return;
}
for(int i = firstIndex; i <= lastIndex; i++) {
ByteProcessor bp = new ByteProcessor(w, h);
if(rois[i] != null) {
bp.copyBits(rois[i].getMask(),
rois[i].getBounds().x,
rois[i].getBounds().y,
ij.process.Blitter.ADD);
}
stack.addSlice("", bp);
}
run(stack);
ImagePlus roiImage = new ImagePlus("bla", stack);
ThresholdToSelection ts = new ThresholdToSelection();
ts.setup("", roiImage);
for(int i = firstIndex; i <= lastIndex; i++) {
ImageProcessor bp = stack.getProcessor(1);
stack.deleteSlice(1);
int threshold = 255;
bp.setThreshold(threshold, threshold, ImageProcessor.NO_LUT_UPDATE);
ts.run(bp);
rois[i] = roiImage.getRoi();
}
}
public void run(ImageStack stack) {
int sliceCount = stack.size();
if (sliceCount < 3) {
IJ.error("Too few slices to interpolate!");
return;
}
IJ.showStatus("getting signed integer distance transform");
w = stack.getWidth();
h = stack.getHeight();
idt = new int[sliceCount][];
int first = sliceCount, last = -1;
for (int z = 0; z < sliceCount; z++) {
idt[z] = getIDT(stack.getProcessor(z + 1).getPixels());
if (idt[z] != null) {
if (z < first)
first = z;
last = z;
}
}
if (first == last || last < 0) {
IJ.error("Not enough to interpolate");
return;
}
IJ.showStatus("calculating weights");
int current = 0, next = first;
for (int z = first; z < last; z++) {
if (z == next) {
current = z;
for (next = z + 1; idt[next] == null; next++);
continue;
}
byte[] p =
(byte[])stack.getProcessor(z + 1).getPixels();
for (int i = 0; i < w * h; i++)
if (0 <= idt[current][i] * (next - z)
+ idt[next][i] * (z - current))
p[i] = (byte)255;
IJ.showProgress(z - first + 1, last - z);
}
}
/*
* The following calculates the signed integer distance transform.
* Distance transform means that each pixel is assigned the distance
* to the boundary.
* IDT means that the distance is not the Euclidean, but the minimal
* sum of neighbour distances with 3 for horizontal and neighbours,
* and 4 for diagonal neighbours (in 3d, the 3d diagonal neighbour
* would be 5).
* Signed means that the outside pixels have a negative sign.
*/
class IDT {
int[] result;
IDT() {
result = new int[w * h];
int infinity = (w + h) * 9;
for (int i = 0; i < result.length; i++)
result[i] = infinity;
}
int init(byte[] p) {
int count = 0;
for (int j = 0; j < h; j++)
for (int i = 0; i < w; i++) {
int idx = i + w * j;
if (isBoundary(p, i, j)) {
result[idx] = 0;
count++;
} else if (isJustOutside(p, i, j))
result[idx] = -1;
}
return count;
}
final void idt(int x, int y, int dx, int dy) {
if (x + dx < 0 || y + dy < 0 ||
x + dx >= w || y + dy >= h)
return;
int value = result[x + dx + w * (y + dy)];
int distance = (dx == 0 || dy == 0 ? 3 : 4);
value += distance * (value < 0 ? -1 : 1);
if (Math.abs(result[x + w * y]) > Math.abs(value))
result[x + w * y] = value;
}
void propagate() {
for (int j = 0; j < h; j++)
for (int i = 0; i < w; i++) {
idt(i, j, -1, 0);
idt(i, j, -1, -1);
idt(i, j, 0, -1);
}
for (int j = h - 1; j >= 0; j--)
for (int i = w - 1; i >= 0; i--) {
idt(i, j, +1, 0);
idt(i, j, +1, +1);
idt(i, j, 0, +1);
}
for (int i = w - 1; i >= 0; i--)
for (int j = h - 1; j >= 0; j--) {
idt(i, j, +1, 0);
idt(i, j, +1, +1);
idt(i, j, 0, +1);
}
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++) {
idt(i, j, -1, 0);
idt(i, j, -1, -1);
idt(i, j, 0, -1);
}
}
}
int[] getIDT(Object pixels) {
IDT idt = new IDT();
if (idt.init((byte[])pixels) == 0)
return null;
idt.propagate();
return idt.result;
}
final boolean isBoundary(byte[] pixels, int x, int y) {
if (pixels[x + w * y] == 0)
return false;
if (x <= 0 || pixels[x - 1 + w * y] == 0)
return true;
if (x >= w - 1 || pixels[x + 1 + w * y] == 0)
return true;
if (y <= 0 || pixels[x + w * (y - 1)] == 0)
return true;
if (y >= h - 1 || pixels[x + w * (y + 1)] == 0)
return true;
if (x <= 0 || y <= 0 || pixels[x - 1 + w * (y - 1)] == 0)
return true;
if (x <= 0 || y >= h - 1 || pixels[x - 1 + w * (y + 1)] == 0)
return true;
if (x >= w - 1 || y <= 0 || pixels[x + 1 + w * (y - 1)] == 0)
return true;
if (x >= w - 1 || y >= h - 1 ||
pixels[x + 1 + w * (y + 1)] == 0)
return true;
return false;
}
final boolean isJustOutside(byte[] pixels, int x, int y) {
if (pixels[x + w * y] != 0)
return false;
if (x > 0 && pixels[x - 1 + w * y] != 0)
return true;
if (x < w - 1 && pixels[x + 1 + w * y] != 0)
return true;
if (y > 0 && pixels[x + w * (y - 1)] != 0)
return true;
if (y < h - 1 && pixels[x + w * (y + 1)] != 0)
return true;
if (x > 0 && y > 0 && pixels[x - 1 + w * (y - 1)] != 0)
return true;
if (x > 0 && y < h - 1 && pixels[x - 1 + w * (y + 1)] != 0)
return true;
if (x < w - 1 && y > 0 && pixels[x + 1 + w * (y - 1)] != 0)
return true;
if (x < w - 1 && y < h - 1 &&
pixels[x + 1 + w * (y + 1)] != 0)
return true;
return false;
}
}
|