Login  Register

Re: Copy volume from within stack to new stack

Posted by Michael Doube on Jan 19, 2009; 6:07pm
URL: http://imagej.273.s1.nabble.com/Copy-volume-from-within-stack-to-new-stack-tp3693965p3693987.html

OK. To answer my own question, I had to move a line

  short[] roiPixels = new short[roiWidth*roiHeight];

 From outside to inside the

for (int z = startZ; z <= startZ+roiDepth; z++)

loop.

Can anyone tell me why this makes a difference?

Mike

Michael Doube wrote:

> Hi all
>
> I think my brain turned to mush over New Years because I can't get this
> simple task to work.  I have a stack, coordinates of a start position
> and a volume of interest's width, height and depth.  I want to copy the
> source stack's pixel values into a new stack which has the same
> dimensions of the volume of interest. This code results in a new stack
> ("Target") containing the last slice of the volume of interest on all of
> its slices:
>
> Cheers,
>
> Mike
>
> //copy box of pixels that contains sphere (i.e. is centred on centroid
> //and has 2*radius sides)
> int startX = (int)Math.round((centroid[0]-radius)/vW);
> int startY = (int)Math.round((centroid[1]-radius)/vH);
> int startZ = (int)Math.round((centroid[2]-radius)/vD);
> int roiWidth = (int)Math.round(2*radius/vW);
> int roiHeight = (int)Math.round(2*radius/vH);
> int roiDepth = (int)Math.round(2*radius/vD);
> ImageStack targetStack = new ImageStack(roiWidth,roiHeight);
> short[] roiPixels = new short[roiWidth*roiHeight];
> for (int z = startZ; z <= startZ+roiDepth; z++){
> imp.setSlice(z);
> int nRows = 0;
> for (int y = startY; y < startY+roiHeight; y++){
> int index = nRows*roiWidth;
> int nCols = 0;
> for (int x = startX; x < startX+roiWidth; x++){
> roiPixels[index+nCols] = (short)ip.getPixel(x,y);
> nCols++;
> }
> nRows++;
> }
> targetStack.addSlice("slice "+z, roiPixels);
> }
> ImagePlus target = new ImagePlus("Target", targetStack);
> target.show();