def thresholdStackW(imp, low, high): i = 1 stack = imp.getStack() depth = imp.getNSlices() print "thresholdStackW: depth", depth width = stack.getProcessor(i).getWidth() height = stack.getProcessor(i).getHeight() winput = [None] w = Weaver.inline( """ byte[] input = (byte[]) winput.get(0); byte[] output = new byte[input.length]; for (int i=0; i<input.length; i++) { if (input[i] < low || input[i] > high){ output[i] = (byte)0; } else { output[i] = (byte)255; } } return output; """, {"winput":winput, "low":low, "high":high}) stackout = ImageStack(width, height) for k in range(1, depth+1): ip = stack.getProcessor(k) winput[0] = ip.getPixels() pixels = w.call() ipout = ByteProcessor(width, height) ipout.setPixels(pixels) stackout.addSlice(ipout) imp.setStack(stackout)
def Inline(arrays): return Weaver.inline( """ int[] pixelst = (int[])arrays.get(0); int[] pixelsd = (int[])arrays.get(1); double sum = 0; for (int i=0; i<pixelst.length; i++) { int t = pixelst[i]; int d = pixelsd[i]; int red = ((t >> 16)&0xff) - ((d >> 16)&0xff); int green = ((t >> 8)&0xff) - ((d >> 8)&0xff); int blue = (t&0xff) - (d&0xff); //sum += Math.sqrt(red * red + green * green + blue * blue); sum += red * red + green * green + blue * blue; } return sum; """, {"arrays": arrays})
def Inline(arrays): return Weaver.inline( """ int[] pixelst = (int[])arrays.get(0); int[] pixelsd = (int[])arrays.get(1); double sum = 0; for (int i=0; i<pixelst.length; i++) { int t = pixelst[i]; int d = pixelsd[i]; int red = ((t >> 16)&0xff) - ((d >> 16)&0xff); int green = ((t >> 8)&0xff) - ((d >> 8)&0xff); int blue = (t&0xff) - (d&0xff); //sum += Math.sqrt(red * red + green * green + blue * blue); sum += red * red + green * green + blue * blue; } return sum; """, {"arrays" : arrays})
imap(FloatType.getRealFloat, hsc)) # ~1.5x faster than for t in hsc: s += t.getRealFloat #intensities2.append(float(s) / size) t2 = System.currentTimeMillis() print "Elapsed time:", (t2 - t1), "ms" w = Weaver.inline( """ double sum = 0; //final net.imglib2.Cursor<net.imglib2.type.numeric.real.FloatType> c = (net.imglib2.Cursor<net.imglib2.type.numeric.real.FloatType>) hsc; final Cursor<FloatType> c = (Cursor<FloatType>) hsc; while (c.hasNext()) { c.fwd(); sum += c.get().getRealFloat(); } // Fails, for some reason //for (final net.imglib2.type.numeric.real.FloatType t : c) { // sum += t.getRealFloat(); //} return sum; """, {"hsc": hsc}, Double, True, [FloatType, Cursor]) t3 = System.currentTimeMillis() for i in xrange(100): for peak in peaks: hsc.updateCenter([int(peak[0]), int(peak[1]), int(peak[2])]) #s = sum(imap(FloatType.getRealFloat, hsc)) s = w.call()
print event def update_roi_mappings(): global roi_in global roi_ni for i in range(roimgr.getCount()): name = roimgr.getName(i) roi_in.append(name) roi_ni[name] = i wcode = Weaver.inline( """ javax.swing.AbstractListModel m = new javax.swing.AbstractListModel() { String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; public int getSize() { return strings.length; } public Object getElementAt(int i) { return strings[i]; } }; return m; """, {}, javax.swing.AbstractListModel) print "Weaver created!" listmodel = wcode.call() print "Weaver called!" roimgr = RoiManager.getInstance() if roimgr is None: print "No ROIs defined." sys.exit(0) roi_ni = {} roi_in = [] update_roi_mappings()
from fiji.scripting import Weaver from ij import IJ from ij import ImagePlus from ij import ImageStack # The currently open image, an 8-bit stack imp = IJ.openImage("http://imagej.net/images/bat-cochlea-volume.zip") slices = [None, None] w = Weaver.inline( """ byte[] pix1 = (byte[]) slices.get(0); byte[] pix2 = (byte[]) slices.get(1); byte[] xor = new byte[pix1.length]; for (int i=0; i<pix1.length; i++) { xor[i] = (byte)(pix1[i] ^ pix2[i]); } return xor; """, {"slices" : slices}) stack = imp.getStack() stackXOR = ImageStack(stack.width, stack.height) for i in range(2, imp.getNSlices()+1): # Put the pixel arrays into the pre-made list slices[0] = stack.getPixels(i-1) slices[1] = stack.getPixels(i) # Invoke native code stackXOR.addSlice( str(i-1), w.call() )