Posted by
Rasband, Wayne (NIH/NIMH) [E] on
URL: http://imagej.273.s1.nabble.com/How-to-color-differently-each-slice-of-a-stack-or-each-ImageProcessor-tp5000664p5000686.html
On Nov 5, 2012, at 1:45 PM, thehobbit wrote:
> Hi, thanks for the replay.
> Probably I did not explain the problem well, sorry, my english is not that
> good.
> I have problems to draw on each single slice using JAVA.
> When I draw some ROI on an image, all other images on the stack suffer the
> same modification, but I need only to draw on that specific slice.
You need to use an Overlay. Here is a JavaScript example that adds a differently colored transparent ROI to each image in a stack:
imp = IJ.openImage("
http://imagej.nih.gov/ij/images/mri-stack.zip");
n = imp.getStackSize();
width = imp.getWidth();
alpha = 0.4;
ran = new Random();
overlay = new Overlay();
x=0; y=0; w=50; h=50;
for (i=1; i<=n; i++) {
imp.setSlice(i);
roi = new OvalRoi(x, y, w, h);
roi.setPosition(i);
r = ran.nextDouble();
g = ran.nextDouble();
b = ran.nextDouble();
roi.setFillColor(new Color(r,g,b,alpha));
overlay.add(roi);
x+=width/n; y+=width/n;
}
imp.setOverlay(overlay);
imp.setSlice(1);
imp.show();
-wayne
> I try to write here an example:
>
>
> //This is a function I found on the internet and which I modified to my own
> needs
> //the value of the variable imp is passed as IJ.getImage();
>
> void labeltmp(ImagePlus imp, int x, int y) {
> Roi roi = imp.getRoi();
> if (roi != null) {
> if (!(roi instanceof ShapeRoi))
> roi = new ShapeRoi(roi);
> ShapeRoi roiShape = (ShapeRoi) roi;
> roiShape.or(getBrushRoi(x, y, brushWidth));
> } else
> roi = getBrushRoi(x, y, brushWidth);
>
> roi.setFillColor(fillColor);
> imp.setRoi(roi);
> imp.updateAndDraw();
>
> }
>
> ShapeRoi getBrushRoi(int x, int y, int width) {
> return new ShapeRoi(new OvalRoi(x - width / 2, y - width / 2, width,
> width));
> }
>
> This function works well, and draws a roi like a brush tool.
> The problem, as explained before, is that this type of solution draws a Roi
> on all the images of the stack, even if I'm working on the 30th slice of 60.
> I tried several functions like roi.setPosition(...) ecc.
> I don't know if there is something wrong with using IJ.getImage(), or if
> there are some functions that get a single slice and automatically set the
> modifications on the ImageStack.
>
> I hope the problem is clear now.
> Thank you for your support.
>
> Ferdinando
>
>
>
>
> --
> View this message in context:
http://imagej.1557.n6.nabble.com/How-to-color-differently-each-slice-of-a-stack-or-each-ImageProcessor-tp5000664p5000680.html> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list:
http://imagej.nih.gov/ij/list.html--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html