Hi there,
I'm trying to figure out how to conduct the following particle analysis in the most efficient way: On a large number of similar images , I'd like to measure: 1) individual areas of all particles (in yellow in the example image) 2) area fraction of all particles within a larger region of interest (in red in the example image). In some images, I have only one ROI, in other images more then one ROIs. I'd like to write a little macro when one would manually draw the particles and ROIs around these particles and then the macro would output a file with particle sizes and another file with the fraction area (% fraction area of particles from the corresponding ROI area). It would be ideal to be able to add results from subsequent images to the same output files later on. Thanks for your suggestions! Linda |
Hi Linda,
Try the following macro. This should get you started with what you need. First, add all the particles and ROIs to the ROI Manager and then run this macro. The output is a Log file with particle name, followed by its area and area fraction. Hope it helps! Ved // --------- Macro starts here n = roiManager("count"); areaParticles = newArray(n); nameParticles = newArray(n); for(i=0; i<n; i++) { roiManager("Select", i); getStatistics(areaParticles[i]); nameParticles[i] = Roi.getName; } fraction = newArray(n); for(i=0; i<n; i++) fraction[i] = 0; // initialize fraction array for(i=0; i<n; i++) { for(j=0; j<n; j++) { if(j != i) { roiManager("Select", newArray(i,j)); roiManager("AND"); getStatistics(area); if(area == areaParticles[i]) fraction[i] = (100*areaParticles[i])/areaParticles[j]; } } } for(i=0; i<n; i++) { if(fraction[i] > 0) print(nameParticles[i]+": area = "+areaParticles[i]+", fraction = "+fraction[i]); } // --------- Macro ends here ___________________________________ Ved Sharma, PhD Department of Anatomy and Structural Biology Albert Einstein College of Medicine Bronx, New York -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by lindaP
Hi Ved,
Thanks a lot for offering your solution. I'm trying to understand the script. FYI, I added my interpretation of your code as comments (see below). The only part where I'm struggling is figuring out what happens during the double for loop. I don't understand the if(i !=j) condition. It seems to me that i always equals to j. Could you give me a hint on that? Thanks! Linda // --------- Macro starts here n = roiManager("count"); // number of ROIs areaParticles = newArray(n); // initialization of new arrays with the length corresponding to the number or ROIs nameParticles = newArray(n); // This part is completed before the next for loop is evaluated for(i=0; i<n; i++) { roiManager("Select", i); // selects ith roi getStatistics(areaParticles[i]); // Get statistics on ith roi. // What happens with the data (area, mean...) received from this call? // They don't seem to be stored anywhere. nameParticles[i] = Roi.getName; // saves the name of ith ROI in nameParticles array } // I understand this part fraction = newArray(n); for(i=0; i<n; i++) fraction[i] = 0; // initialize fraction array for(i=0; i<n; i++) { for(j=0; j<n; j++) { // This is when I get confused: if(j != i) { // When does this occur? During the first looping i=0 and j=0 or not? roiManager("Select", newArray(i,j)); // selects two ROIs roiManager("AND"); // calculates intersection of these ROIs getStatistics(area); // measures area of the intersection if(area == areaParticles[i]) // if this is true it means that the first ROI is encapsulated by the second one // or it is the same ROI fraction[i] = (100*areaParticles[i])/areaParticles[j]; //calculates fraction } } } for(i=0; i<n; i++) { if(fraction[i] > 0) // excludes fraction calculated by dividing the same ROIs print(nameParticles[i]+": area = "+areaParticles[i]+", fraction = "+fraction[i]); //prints out result } // --------- Macro ends here -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by lindaP
Hi Linda,
You are most welcome! I put "if(i!=j)" to exclude the intersection of an ROI with itself. The idea is to see the intersection of an ROI (let's say ROI1) with another ROI (say ROI2) in the list. If the intersection is the same as the area of ROI1, then ROI1 is inside ROI2 and then you go ahead and measure its area and area fraction. If you don't put the "if(i!=j)" statement and then when ROI1 intersects with ROI1, you get the same area as the area of ROI1, that will mean that ROI1 is inside ROI1, which I think is meaningless. That is why I excluded the self intersection of ROIs by using if(i!=j). Regarding your other comments: // What happens with the data (area, mean...) received from this call? // They don't seem to be stored anywhere. Check the description of getStatistics() function. You do not need to assign it to a variable. When you call getStatistics(bla, blabla), the area will get stored in the variable "bla" and the mean will get stored in the variable "blabla". BTW, I am curious if the macro worked for your images/ROIs and gave you the right numbers? Ved -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |