Skip to content

Instantly share code, notes, and snippets.

@ctrueden
Last active August 29, 2015 14:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ctrueden/fe00e8dd2034f37273f0 to your computer and use it in GitHub Desktop.
ImageJ: colorize an image and rotate it
import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.plugin.PlugIn;
import ij3d.ImageJ3DViewer;
public class Colorized_Rotation implements PlugIn {
@Override
public void run(String arg) {
// convert to grayscale image with RGB type
ImagePlus imp = IJ.getImage();
IJ.run(imp, "8-bit", "");
IJ.run(imp, "RGB Color", "");
// colorize the stack
ImageStack stack = imp.getImageStack();
int size = stack.getSize();
IJ.showStatus("Colorizing");
for (int n=0; n<size; n++) {
IJ.showProgress(n, size);
double weight = (double) n / (size - 1);
int[] pix = (int[]) stack.getProcessor(n + 1).getPixels();
for (int p=0; p<pix.length; p++) {
int value = pix[p] & 0xff;
int r = (int) ((1 - weight) * value);
int g = 0;
int b = (int) (weight * value);
pix[p] = (r << 16) | (g << 8) | b;
}
}
IJ.showStatus("");
imp.updateAndDraw();
// visualize in 3D with rotation
IJ.run("3D Viewer", "");
ImageJ3DViewer.setCoordinateSystem("false");
ImageJ3DViewer.add(imp.getTitle(), "None", imp.getTitle(), "0", "true", "true", "true", "1", "0");
ImageJ3DViewer.startAnimate();
}
}
@dscho
Copy link

dscho commented May 23, 2014

I believe you wanted to use imp.getName() twice in line 37... ;-)

@ctrueden
Copy link
Author

@dscho Thanks! I changed it to imp.getTitle() in the relevant places.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment