Plugin - assigning roi measurements to a variable
Posted by dplatten on Jul 11, 2007; 8:52am
URL: http://imagej.273.s1.nabble.com/Plugin-assigning-roi-measurements-to-a-variable-tp3698886.html
I'm sure that this is straightforward, but I am new to Java and writing
ImageJ plugins. I would be extremely grateful for a little help.
I have written a macro that creates a signal-to-noise map of the open image
(see the bottom of this message). I am a medical physicist working in a
hospital - this routine is useful for checking the response of digital x-ray
detectors. However, I'd like to use it with 10x10 pixel samples, but this
takes ages. I have therefore started to translate the macro into a plugin.
This is where I am having trouble. I need to assign the std_dev measured in
an roi to a variable. An example of how to do this would be great. I want to
avoid displaying and updating the results table if possible. Here's a
(non-working) code snippet:
ip.setRoi(x, y, sub_sample_size, sub_sample_size);
Analyzer ana = new Analyzer();
int measurements = Analyzer.getMeasurements();
measurements = (ana.STD_DEV);
IJ.write(Integer.toString(measurements));
This just returns the value 4.
Many thanks for any help,
David Platten
Clinical Scientist
Northampton General Hospital
Billing Road
Northampton NN1 5BD
tel +44 (0)1604 54 4369
//-----------------------------------------------------------
macro "Signal to noise map" {
// Calculates the signal to noise of sub-samples
// over an image.
// Displays the results as a new image.
// SNR taken as mean pixel value in ROI divided
// by standard deviation in the region.
// David Platten, July 2007
// Ask the user for the size of the sub-samples.
Dialog.create("Signal to noise image");
Dialog.addMessage("Click OK to analyze, or cancel to exit (!).");
Dialog.addNumber("Sub-sample size:", 50);
Dialog.show();
sub_sample_size = Dialog.getNumber();
// Find the dimensions of the image and then
// determine the number of sub-samples that
// will go into it.
width = getWidth;
height = getHeight;
i_max = floor(width/sub_sample_size) + 1;
j_max = floor(height/sub_sample_size) + 1;
// Create an array to hold the results
results = newArray(i_max * j_max);
// Loop through the image, stepping through in
// increments equal to the sub-sample size.
i = 0;
j = 0;
max_snr = 0; // Used to scale the results image
for(x=0; x<width; x+=sub_sample_size) {
j = 0;
for(y=0; y<height; y+=sub_sample_size) {
// Specify the width, height and position of the ROI, then
// measure the standard deviation and mean within it.
// SNR is calculated as mean / standard deviation and
// written into the appropriate element of the results array.
run("Specify...", "width="+sub_sample_size+"
height="+sub_sample_size+" x="+x+" y="+y);
run("Set Measurements...", "mean standard decimal=5");
run("Measure");
mean = getResult("Mean", nResults-1);
stdev = getResult("StdDev", nResults-1);
results[i + (j*i_max)] = mean / stdev;
// Check to see if this is a new maximum snr. This is
// used to scale the results at the end.
if(results[i + (j*i_max)] > max_snr) max_snr = results[i + (j*i_max)];
j++;
}
i ++;
}
// Create new image to display the results.
// At the moment this is an 8-bit image, 256 shades of grey.
image_title = "SNR, " + sub_sample_size + " pixel sub-samples";
newImage(image_title, "8-bit Black", i_max, j_max, 1);
// Step through the results array, scaling the values using
// the known maximum figure and then allocating the value to
// the appropriate pixel.
for(i=0;i<i_max; i++) {
for(j=0;j<j_max; j++) {
temp = round(results[i + (j*i_max)] / max_snr * 256);
setPixel(i, j, temp);
}
}
// Resize the results image to have the same dimensions
// as the original and then enhance the contrast (an
// auto window and level).
run("Size...", "width="+width+" height="+height+" constrain");
run("Enhance Contrast", "saturated=0.5");
}
//-----------------------------------------------------------