// This simple macro applies a 3D mask to a 4D (hyperstack) image. // In fact, the 3D mask does not need to be a binary image: // all pixels in that image with a non-zero value will not modify // the corresponding pixel in the 4D image. // Author: Jose Maria Mateos - jmmateos@mce.hggm.es Dialog.create("Instructions"); Dialog.addMessage("Hyperstack must be called \"4D\". 3D Mask must be called \"3D\""); Dialog.show(); selectWindow("3D"); getDimensions(width, height, channels, slices, frames) mask = newArray(width*height*slices); // No 3D array support, need my own indexing // Fill in mask data for (s = 1; s <=slices; s++) { setSlice(s); for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { mask[(s-1)*height*width + width*i + j] = getPixel(i, j); } } } selectWindow("4D"); Stack.getDimensions(a, b, c, d, frames); // Don't need a, b, c, or d. // Apply mask to image for (f = 1; f <= frames; f++) { Stack.setFrame(f); for (s = 1; s <= slices; s++) { Stack.setSlice(s); for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { if (mask[(s-1)*height*width + width*i + j] == 0) { setPixel(i, j, 0); } } } } }