import ij.*; import ij.plugin.filter.PlugInFilter; import ij.process.*; import java.awt.*; /* Swap quadrants to convert from native FHT order to order with the low frequencies in the center, or back. Somewhat like the swapQuadrantes in Wayne Rasband's ImageJ, but this one is inteded to work in 3D and is not restricted to square images or even dimensions. Bob Dougherty May 3, 2006 */ public class Swap_Quadrants_3D implements PlugInFilter { private ImagePlus imp; public int setup(String arg, ImagePlus imp) { this.imp = imp; return DOES_ALL; } public void run(ImageProcessor ip) { swapQuadrantsStack(imp); swapSlices(imp); imp.updateAndDraw(); } public void swapSlices(ImagePlus imp){ int numSlices = imp.getStackSize(); if(numSlices ==1)return; ImageStack stack = imp.getStack(); int size = numSlices/2; int offset = (numSlices + 1)/2; for (int s1 = 1; s1 <= size; s1++){ int s2 = s1 + offset; //Swap slices s1 and s2 stack.addSlice(stack.getSliceLabel(s1), stack.getProcessor(s1),s2); stack.addSlice(stack.getSliceLabel(s2), stack.getProcessor(s2),s1); stack.deleteSlice(s1); stack.deleteSlice(s2); } imp.setStack(null,stack); } public void swapQuadrantsStack(ImagePlus imp){ int numSlices = imp.getStackSize(); ImageStack stack = imp.getStack(); for (int iSlice = 1; iSlice <= numSlices; iSlice++){ swapQuadrants(stack.getProcessor(iSlice)); } } public void swapQuadrants(ImageProcessor ip) { int w = ip.getWidth(); int h = ip.getHeight(); int sizeW = w/2; int sizeH = h/2; int offsetW = (w+1)/2; int offsetH = (h+1)/2; //Horizontal swap ImageProcessor t1, t2; ip.setRoi(offsetW,0,sizeW,h); t1 = ip.crop(); ip.setRoi(0,0,sizeW,h); t2 = ip.crop(); ip.insert(t1,0,0); ip.insert(t2,offsetW,0); //Vertical sqap ip.setRoi(0,0,w,sizeH); t1 = ip.crop(); ip.setRoi(0,offsetH,w,sizeH); t2 = ip.crop(); ip.insert(t1,0,offsetH); ip.insert(t2,0,0); ip.resetRoi(); } }