I have over 300 images I need to process to obtain an unbiased estimate of the proportion of each image that is occupied by the object of interest. For each image, the objects of interest are black in color, mostly spherical in shape, but range in sizes, while the background of the image is white. The challenge for me is that I need to generate 1000 random points on each image, and determine the proportion of points that fall within the objects of interest in the image. I will appreciate information on how to achieve this using imageJ. After searching online, I really couldn't find any tips on where to start from or how to go about generating random points on an image and estimating whether they fall within the object of interest or not in imageJ.
Thanks in advance. |
Hi,
The simplest thing I can think of is as follows: You have your image which you can threshold to get a mask (a BW image that has values of either 0 or 255). Generate random points using this macro which also counts how many are inside your object. // 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 Olivier Burri Engineer - Image Processing & Software Development EPFL - SV - PTECH - PTBIOP > -----Original Message----- > From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of > atawewe > Sent: lundi 2 juin 2014 15:55 > To: [hidden email] > Subject: Generating random points and tallying proportion of points that fall > within object > > I have over 300 images I need to process to obtain an unbiased estimate of the > proportion of each image that is occupied by the object of interest. For each > image, the objects of interest are black in color, mostly spherical in shape, but > range in sizes, while the background of the image is white. The challenge for me > is that I need to generate 1000 random points on each image, and determine the > proportion of points that fall within the objects of interest in the image. I will > appreciate information on how to achieve this using imageJ. After searching > online, I really couldn't find any tips on where to start from or how to go about > generating random points on an image and estimating whether they fall within > the object of interest or not in imageJ. > > Thanks in advance. > > > > -- > View this message in context: http://imagej.1557.x6.nabble.com/Generating- > random-points-and-tallying-proportion-of-points-that-fall-within-object- > tp5007986.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 |
Thanks for helping to resolve this challenge!
Your solution does work, because it generates 1000 random points. But I think there's a problem because the output always says: "0 points out of 1000 inside objects of interest". I have tried it out on 3 different images and get the same output. I have no experience writing macros, but reading through the one you generously wrote to solve my problem, I think the problem is that the code is searching for points for which the x and y coordinates equal 255. My guess is that there needs to be a line of code that extracts the pixel value or the color of that point to determine if it equals 255. |
Hi,
the macro presented by Olivier Burri works fine. I used run("Blobs (25K)"); setAutoThreshold("Default dark"); setOption("BlackBackground", false); run("Convert to Mask"); to create an appropriate image that only contains 0 and 255. But I think the random points generator needs to be optimised. There is a small chance to create a point twice. The change decreases with the size of the image, but it should not be neglected. It is more important to replace xcoords[i] = round(random()*x); ycoords[i] = round(random()*y); by xcoords[i] = round(random()*(x-1)); ycoords[i] = round(random()*(y-1)); For example the width of "Blobs" is 256, but the most right pixel has the x-coordinate 255. With "random()" that generates numbers between 0 and 1 you need "round(random()*(x-1))" to generate x-coordinates between 0 and 255. Best regards Michael Am 02.06.2014 18:23, schrieb atawewe: > Thanks for helping to resolve this challenge! > Your solution does work, because it generates 1000 random points. But I > think there's a problem because the output always says: "0 points out of > 1000 inside objects of interest". I have tried it out on 3 different images > and get the same output. > I have no experience writing macros, but reading through the one you > generously wrote to solve my problem, I think the problem is that the code > is searching for points for which the x and y coordinates equal 255. My > guess is that there needs to be a line of code that extracts the pixel value > or the color of that point to determine if it equals 255. > > > > > > > > > > > -- > View this message in context: http://imagej.1557.x6.nabble.com/Generating-random-points-and-tallying-proportion-of-points-that-fall-within-object-tp5007986p5007989.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 |
Thanks for looking at this issue for me.
I generated the same image as you did with your code, and ran the macro that Olivier wrote, which generates 1000 random points and tallies the number of points that fall within the object of interest. But this is the output I got: Image blobs.gif: 0 points out of 1000 inside objects of interest This is obviously wrong because I can see many points with the value of 255. Any thoughts on why I am getting the wrong output? |
Have a look at your Results table. I think there is no column called
'Mean'. Go to 'Analyze > Set Measurements...' and select 'Mean gray values'. If I uncheck this selection, I get the same result ('Image blobs.gif: 0 points out of 1000 inside objects of interest') as you get. Best regards Michael Am 06.06.2014 00:49, schrieb atawewe: > Thanks for looking at this issue for me. > I generated the same image as you did with your code, and ran the macro that > Olivier wrote, which generates 1000 random points and tallies the number of > points that fall within the object of interest. But this is the output I > got: Image blobs.gif: 0 points out of 1000 inside objects of interest > This is obviously wrong because I can see many points with the value of 255. > Any thoughts on why I am getting the wrong output? > > > > > -- > View this message in context: http://imagej.1557.x6.nabble.com/Generating-random-points-and-tallying-proportion-of-points-that-fall-within-object-tp5007986p5008052.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 |
Hi,
Thanks for figuring out the problem. Just as you suggested, when I activated the mean gray value tab under set measurements, then I got sensible results e.g. 61 points out of 1000 in object of interest. Thanks to everyone who contributed to helping me get a solution! |
In reply to this post by Olivier Burri
Hi everyone!
Following up this topic, I have a similar work to do. I have to generate an exactly number of points within a given mask. Could you please help me to modify the macro you wrote to do what I need? Thanks!
Marcos Felipe Martins Silva
Krasnow Institute for Advanced Study, GMU
|
Problem solved!
Thank you all,
Marcos Felipe Martins Silva
Krasnow Institute for Advanced Study, GMU
|
Free forum by Nabble | Edit this page |