applying macro to all slices in a stack

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

applying macro to all slices in a stack

Kenneth Sloan-2
Beginner questions about macros:

I have a macro: foo.ijm which processes a single image, takes a measurement, and displays the results in the Results window.

I need help with two modifications:

a) if the current image is a stack - I want to run the macro for every slice in the stack, producing
one line in the Results window for each slice (I assume the lines will be labelled with the slice labels?)
b) (probably trivial, but...as long as I'm here) write the contents of the Results window to a file.

Ordinarily, I would write this in Java, but it looks simple enough that a macro seems appropriate - but I am a complete beginner at writing macros.


Also...I'm *really* interested in the percentages of white/black pixels after thresholding.  I'm assuming that "Percent Area" is the correct measurement?  And...which percentage is reported - pct WHITE or pct BLACK?  Again - ordinarily I would simply write the Java code to count the pixels, but that's probably overkill for this application.

So...what I need is help with:

<apply to every slice> foo.ijm
<write Results to file>

Ideally, I'd like to write the Results to the "current directory" (presumably the directory the stack was read from?), so the interaction would be:

a) import an image sequence
b) apply macro



--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: applying macro to all slices in a stack

Gabriel Landini
On Tuesday, 9 June 2020 22:42:09 BST you wrote:
> a) if the current image is a stack - I want to run the macro for every slice
> in the stack, producing one line in the Results window for each slice (I
> assume the lines will be labelled with the slice labels?) b) (probably
> trivial, but...as long as I'm here) write the contents of the Results
> window to a file.

Hi Kenneth,
I would consider your images to be always a stack, with the case of single
images as a stack with only one slice.
The reserved variable nSlices tells you how many slices there are.

for(i=0;i<nSlices;i++){
setSlice(i+1);
  // do whatever here  \
}

As for the Results Table, there is also a reserved variable nResults that
tells you how much data (rows) there are in the Table, so you can add the new
results in the 'next' row (can't remember if it starts at 0 or at 1).

> Also...I'm *really* interested in the percentages of white/black pixels
> after thresholding.  I'm assuming that "Percent Area" is the correct
> measurement?  And...which percentage is reported - pct WHITE or pct BLACK?
> Again - ordinarily I would simply write the Java code to count the pixels,
> but that's probably overkill for this application.

Easiest is to get the histogram and obtain the number you want from there:
(assuming a greyscale):
 nBins = 256;
 getHistogram(values, counts, nBins);

counts[0] is the number of black pixels and counts[255] are the white ones

Hope that helps a bit.

Cheers

Gabriel

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: applying macro to all slices in a stack

Kenneth Sloan-2
Gabriel-

Thanks very much for the prompt, and useful, reply!

--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html