Login  Register

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

Posted by atawewe on Jun 02, 2014; 5:14pm
URL: http://imagej.273.s1.nabble.com/Generating-random-points-and-tallying-proportion-of-points-that-fall-within-object-tp5007988p5007991.html

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