Login  Register

Re: Generating random points and tallying proportion of points that fall within object

Posted by Kenneth Sloan on Jun 02, 2014; 6:24pm
URL: http://imagej.273.s1.nabble.com/Generating-random-points-and-tallying-proportion-of-points-that-fall-within-object-tp5007988p5007992.html

I think this point needs elaboration.

First - yes, it would be nice to get the macro right.  I’ll let someone
else debug it.

That said - why do you believe that actually measuring *all* of the pixels
enclosed in the region of interest is inferior to sampling?  Sampling is, of
course, an excellent way to proceed when the evaluation at each point is
time consuming or tedious - but is it necessary when you have access to the
actual *answer*?  One possibility, of course, is that you want to compare
against data gathered manually in other cases.

Secondarily - are you sure that random sampling is preferable to regular grid
sampling?  There is a huge literature on stereology which suggests that
sampling on a regular grid may be preferable to random sampling.  For one thing,
you avoid “clumping” (leading to highly correlated measurements) and you
must be very careful to validate the “randomness” of the pseudo-random number generator.

And finally…be *very* careful about using custom code (the PRNG, the macro, …) that
you have not tested and validated.  There is a lot of “contributed” code out there
that does not do what it claims to do.

Dart-throwing is a time-honored way to estimate areas which are difficult to measure
in any other way - BUT - in my experience, they are grossly inferior to actual
area measurements (where possible).  In this case, it seems at first glance that you
can directly measure the area of interest - so…why not do that?

In particular, if your input is a binary image where 255 == foreground and 0 == background,
then the area of the foreground is simply the mean intensity of the image (appropriately scaled
by the area covered by the image and the magic value 255).

Even if the “mean value” were not easy to extract from ImageJ, I would iterate over the
ENTIRE image and count the number of 255 pixels.  Computation is cheap - use it!
If you want “proportion of 255 pixels” then count the 255’s and count the 0’s.  

I really fail to see the need to restrict your attention to a mere 1000 “random” points.

Again - there may well be a justification, that you didn’t feel necessary to elaborate on.
That’s certainly fair.

--
Kenneth Sloan
[hidden email]
"La lutte elle-même vers les sommets suffit à remplir un coeur d'homme; il faut imaginer Sisyphe heureux."


On Jun 2, 2014, at 12:14 , atawewe <[hidden email]> wrote:

> Your points are valid, and what you describe sounds quite practical. However,
> I need to generate random points to get an unbiased estimate of the
> proportion of the image occupied by the objects of interest. I already got a
> solution to this bit, as another kind contributor wrote a macro for me that
> will generate 1000 random points. What I need to figure out now is how to
> include a line of code in the macro that gets the pixel value for each of
> those points and determine if they fall within the object of interest i.e.
> black or have a value of 255. As it is, my guess is that the macro is
> checking if the x and y coordinates equal 255 and so the output is always 0.
> Any thoughts on this?
>
> See macro below:
>
> // START OF MACRO
> // This macro assumes you have a binary image.
>
> // Number of points to generate
> n_points = 1000;
>
>
> name = getTitle(); // Name of the image
> getDimensions(x,y,z,c,t); // Size of the image
>
> // Initialize arrays that will contain point coordinates
> xcoords = newArray(n_points);
> ycoords = newArray(n_points);
>
> // Seed the random number generator
> random('seed', getTime());
>
> // Create n_points points in XY
> for (i=0; i<n_points; i++) {
>        xcoords[i] = round(random()*x);
>        ycoords[i] = round(random()*y);
> }
>
> // Overlay them on the image
> makeSelection("point", xcoords, ycoords);
>
> // Count points that have a value of 255
> prevRes = nResults;
> run("Measure");
> count = 0;
>
> for (i=0; i<n_points;i++) {
>        val = getResult("Mean",i+prevRes);
>        if (val == 255) {
>                count++;
>        }
> }
>
> // Output to log window.
> print("Image "+name+": "+count+" points out of "+n_points+" inside objects
> of interest");
>
> // END OF MACRO
>
>
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/Generating-random-points-and-tallying-proportion-of-points-that-fall-within-object-tp5007988p5007991.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