How to measure part of pixels by lower and upper threshold

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

How to measure part of pixels by lower and upper threshold

Peter van Loon
Hi,

Years ago  I used the macro language in NIH image. It contained a handy function called SetDensitySlice(min,max). It was possible to make a “selection” by greyvalues without making a binary image. Calculations could be done on pixels with the min and max greyvalues defined  in SetDensitySlice .

Now I started using ImageJ. I don’t see an equivalent function in ImageJ. According to the manual it seems that SetThreshold (min,max) should do the same but if I use it with a subsequent measurement e.g. run (“measure”)  it still calculates from all greyvalues: 0..255, no matter what setThreshold settings I use.

I did a test with the well known “blobs” image:
macro "test   [4]" {
        getStatistics(nPixels, mean, min, max, stdDev);
        print ("mean1: ",mean);
  t1 = 100;
    t2 = 255;
    setThreshold(t1, t2);
    getStatistics(nPixels, mean, min, max, stdDev);
    print ("mean2: ",mean);
}

Mean1 and mean 2 are still the same: 103.3.

Is there an easy way to do this? I can calculate the mean by using getHistogram and then use a for loop to count the intensity and number of pixels => mean value. But I think there will be an easier way to do this and it is difficult to get statistics like standard deviation.

Thanks in advance!

Peter
Reply | Threaded
Open this post in threaded view
|

Re: How to measure part of pixels by lower and upper threshold

Olivier Burri
Peter van Loon wrote
I did a test with the well known “blobs” image:
macro "test   [4]" {
        getStatistics(nPixels, mean, min, max, stdDev);
        print ("mean1: ",mean);
  t1 = 100;
    t2 = 255;
    setThreshold(t1, t2);
    getStatistics(nPixels, mean, min, max, stdDev);
    print ("mean2: ",mean);
}

Mean1 and mean 2 are still the same: 103.3.
Hi Peter,

It does seem to be that getStatistics does not use the threshold to get its values (Does anyone else think that this should be the right behavior of GetStatistics?)

You can get around that by adding
run("Create Selection");
just after your setThreshold
and then the statistics you measure will indeed be correct (I think).

Best,

Oli
Reply | Threaded
Open this post in threaded view
|

Re: How to measure part of pixels by lower and upper threshold

Peter van Loon
Hi Olivier,

I tried the run("Create Selection") and indeed it works!!
Thanks a lot!

Regards,

Peter
Reply | Threaded
Open this post in threaded view
|

Re: How to measure part of pixels by lower and upper threshold

Gabriel Landini
In reply to this post by Peter van Loon
On Friday 02 Dec 2011 09:00:51 Peter van Loon wrote:
> Now I started using ImageJ. I don’t see an equivalent function in ImageJ.
> According to the manual it seems that SetThreshold (min,max) should do the
> same but if I use it with a subsequent measurement e.g. run (“measure”)  it
> still calculates from all greyvalues: 0..255, no matter what setThreshold
> settings I use.

I do not think it does.
The "M" (Measure) command works on the range set by the threshold.

However the getStatistics is a different thing. According to the documentation
found at:  http://imagej.nih.gov/ij/developer/macro/functions.html#G

getStatistics(area, mean, min, max, std, histogram)
Returns the area, average pixel value, minimum pixel value, maximum pixel
value, standard deviation of the pixel values and histogram of the active
image or selection. [...]

So this does not specify any threshold value but requires a selection if you
do not want the statistics of the whole image.

So the solution in your case is to convert the thresholded phase to a
selection

run("Create Selection"); and then call the getStatistics() function.

or alternatively set the threshold and call run("Measure");

Something I noted is that if one sets the threshold to 255:255 and press M
(there are no pixels with value 255 in that image) the minimum appears as 255
and the maximum as 248.

Similarly, if one sets threshold to 0:0, the Min grey level appears as 8 and
the maximum as 0.
Not sure if this is a bug or a feature to highlight that there are no pixels
with a Min or Max value?
Perhaps Min and Max could return NaN, same as the Mean greyscale.

Cheers
Gabriel
Reply | Threaded
Open this post in threaded view
|

Re: How to measure part of pixels by lower and upper threshold

Peter van Loon
Hi Gabriel,

I also tried the "Measure" command just after setThreshold and no run("Create Selection").
But in my example I have the same results as with getStatistics. It only works if I use run("Create Selection"). I didn't try the levels you used.

This is my code:
macro "test   [4]" {
        getStatistics(nPixels, mean, min, max, stdDev);
        print ("mean1a: ",mean);
    setThreshold(100, 255);
    run("Create Selection");
    getStatistics(nPixels, mean, min, max, stdDev);
    print ("mean1b: ",mean);
   
    run("Select None");
    setThreshold(0, 255);
    run("Measure");
    n=nResults;
    mean2a = getResult("Mean",n-1);
    print ("mean2a: ",mean2a);
    setThreshold(100, 255);
    run("Measure");
    n=nResults;
    mean2b = getResult("Mean",n-1);
    print ("mean2b: ",mean2b);
}

results in Log window:
mean1a:  103.2686
mean1b:  185.9298
mean2a:  103.2686
mean2b:  103.2686

If I disable the run("Select None"); so I keep the selection then I get the right results with "Measure".

Regards,

Peter
Reply | Threaded
Open this post in threaded view
|

Re: How to measure part of pixels by lower and upper threshold

Peter van Loon
In reply to this post by Gabriel Landini
Hi Gabriel,

Someone told me to use the "limit to threshold" in SetMeasurements. And it works now without using the "Create Selection"!

Regards,
Peter