Dear List,
I have been having an intermittent problem with using Analyze Particles masks in a macro. My code usually works but every once in a while I get an unexpected result: the contents of the entire mask gets "cleared". I use analyze particles with a size limit and a circularity limit on a thresholded image with "Show" set to mask and "Clear Results" checked. The relevant code is below. It goes through the "Results Table" and "Clears" every entry that has a lower aspect ratio than a specific value. As the Results Table entries are cleared the associated particle is removed from the mask. I can watch the particles that don't meet my shape criteria disappear one by one as the macro executes. run("Analyze Particles...", "size="+size40+"-Infinity circularity=0.00-"+circ40+" show=Masks clear record"); for (n=0; n<nResults; n++) { X40c = getResult('XStart',n); Y40c = getResult('YStart',n); aspectRatio40 = getResult('AR',n); doWand(X40c,Y40c); if (aspectRatio40<aspra40) { run("Clear"); } } -------------------------- Every once in a while (~4 times out of 150 image files) the macro will clear an entire mask. When watching the screen I can see the particles removed as expected but suddenly the entire mask is cleared. The macro seems to continue to run through the Results Table unaware of what it's done to the mask. It seems that only the mask is affected. The debug window at the time of the unexpected clearing is below. I do not see an obvious indication of a problem. The debug window shows the following after it clears the entire content of the mask: aspra40 3 n 1820 X40c 2786 Y40c 9117 aspectRatio40 1.6813 --------------- Is this a bug in ImageJ or am I doing something wrong? Thank you, Dan Daniel W. McDonald, Co-founder and President, Phenotype Screening Corporation, 4028 Papermill Road, Suite 10, Knoxville, TN 37909 (865) 385-8641 cell (865) 694-9459 office [hidden email] daniel.w.mcdonald1 on Skype www.phenotypescreening.com -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hi Dan,
The problem you describe is not a bug. I had the same problems while coding the macro of the Advanced Particle Analyzer and some other macros relying on the elimination of binary features from an image. I am actually wondering that your macro still recognizes "XStart" since in the table now the heading is only X and Y (might be due to reverse compatibility of those variables) The problem is (most likely): in some cases you will have the cetroid of a particle actually outside the particle itself if the latter is bend, curved or has holes. In such a case your getResult("X",n); gives you an position outside the particle and leads to the fact that doWand(X, Y); selects not the particle but rather the background. This is fatal in such a case because you select the complete background including all particles (with potentially the exception of edge touching particles) and delete the whole area. One workaround which mostly circumvents this problem is the use of the particles "territories". You can do this by converting a copy of the image into a voronoi map (>Process>Binary>Voronoi). Here, your centroids will always be inside the region which correspond to your particles. 1.) You can now use either this voronoi map directly, apply your wand selection, transfer the selection to the image with the binary particles and delete everything inside the current selection (might take longer due to constant switching between images) or 2.) You first convert the voronoi map into a binary image, invert it to have all regions in white and the lines in black. Then run all your elimination processes on the map (by doWand --> Clear) and finally copy your original particles image over this map with the >Edit >Paste Control set to "Transparent-white". This keeps only the particles with underlying white voronoi regions. If you feel those explanations are too confuse, feel free to contact me again. You can also have a look into the source code of the BioVoxxel Toolbox ( http://www.biovoxxel.de/BioVoxxel%20Macro%20Toolset/BioVoxxel_Toolbox.ijm). There the first macro is the Advanced Particle Analyzer which uses basically method 2 described above. Potentially the tool also helps you already in doing your analysis without the need to code it from scratch. hope this comments help you a little bit. regards, Jan 2014-04-03 20:20 GMT+02:00 Dan McDonald <[hidden email]>: > Dear List, > > > > I have been having an intermittent problem with using Analyze Particles > masks in a macro. My code usually works but every once in a while I get an > unexpected result: the contents of the entire mask gets "cleared". > > > > I use analyze particles with a size limit and a circularity limit on a > thresholded image with "Show" set to mask and "Clear Results" checked. > > > > The relevant code is below. It goes through the "Results Table" and > "Clears" every entry that has a lower aspect ratio than a specific value. > As the Results Table entries are cleared the associated particle is removed > from the mask. I can watch the particles that don't meet my shape criteria > disappear one by one as the macro executes. > > > > run("Analyze Particles...", "size="+size40+"-Infinity > circularity=0.00-"+circ40+" show=Masks clear record"); > > for (n=0; n<nResults; n++) { > > X40c = getResult('XStart',n); > > Y40c = getResult('YStart',n); > > aspectRatio40 = getResult('AR',n); > > doWand(X40c,Y40c); > > if (aspectRatio40<aspra40) { > > run("Clear"); > > } > > } > > > -------------------------- > > > > Every once in a while (~4 times out of 150 image files) the macro will > clear > an entire mask. When watching the screen I can see the particles removed > as > expected but suddenly the entire mask is cleared. The macro seems to > continue to run through the Results Table unaware of what it's done to the > mask. It seems that only the mask is affected. The debug window at the > time of the unexpected clearing is below. I do not see an obvious > indication of a problem. > > > > The debug window shows the following after it clears the entire content of > the mask: > > aspra40 3 > > n 1820 > > X40c 2786 > > Y40c 9117 > > aspectRatio40 1.6813 > > --------------- > > > > Is this a bug in ImageJ or am I doing something wrong? > > > > Thank you, > > > > Dan > > > > Daniel W. McDonald, > > Co-founder and President, > > Phenotype Screening Corporation, > > 4028 Papermill Road, Suite 10, > > Knoxville, TN 37909 > > (865) 385-8641 cell > > (865) 694-9459 office > > [hidden email] > > daniel.w.mcdonald1 on Skype > > www.phenotypescreening.com > > > > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- CEO: Dr. rer. nat. Jan Brocher phone: +49 (0)6234 917 03 39 mobile: +49 (0)176 705 746 81 e-mail: [hidden email] info: [hidden email] inquiries: [hidden email] web: www.biovoxxel.de -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Dan McDonald
On Thursday 03 Apr 2014 14:20:54 you wrote:
> I have been having an intermittent problem with using Analyze Particles > masks in a macro. My code usually works but every once in a while I get an > unexpected result: the contents of the entire mask gets "cleared". > run("Analyze Particles...", "size="+size40+"-Infinity > circularity=0.00-"+circ40+" show=Masks clear record"); > > for (n=0; n<nResults; n++) { > X40c = getResult('XStart',n); > Y40c = getResult('YStart',n); > aspectRatio40 = getResult('AR',n); > doWand(X40c,Y40c); > if (aspectRatio40<aspra40) { > run("Clear"); > } > } > -------------------------- If the image you are analysing is binary (i.e. 2 colours), then what I usually do is the floodfill with the background colour(with the 8 neighbours connectivity option) the XStart YStart pixel of the region I want to delete. That is not only less time consuming (you avoid setting ROIs) but you won't encounter the problem you describe. I guess that for some strange reason the wand gets a confusing pixel configuration and chooses the background instead of foreground. Also in the code above you do not need "doWand" for all the regions, but only for those with <aspra40. Anyway if you follow my suggestion above, it will work. Ask if you do not know how to do that. Cheers Gabriel -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by gankaku
Dear Jan and Dan,
On 04.04.2014, 9:03 AM, BioVoxxel wrote: > The problem you describe is not a bug. I had the same problems while coding > the macro of the Advanced Particle Analyzer and some other macros relying > on the elimination of binary features from an image. > I am actually wondering that your macro still recognizes "XStart" since in > the table now the heading is only X and Y (might be due to reverse > compatibility of those variables) I think you are confusing two things here: "XStart" and "YStart" are recorded exactly like this in the Results table upon checking the "Record starts" option in the particle analyzer [1]. "X" and "Y" are recorded if you checked "Centroid" in the Set Measurements dialog [2]. XStart and YStart serve exactly the purpose of providing the coordinates of a pixel that is *inside* the given particle/ROI. Therefore I believe this is rather an issue of the macro mode, since I've had no problems with Dan's strategy of recording XStart and YStart and then using the wand tool when doing this in Javascript or Java. > > The problem is (most likely): in some cases you will have the cetroid of a > particle actually outside the particle itself if the latter is bend, curved > or has holes. In such a case your getResult("X",n); gives you an position > outside the particle and leads to the fact that doWand(X, Y); selects not > the particle but rather the background. This is fatal in such a case > because you select the complete background including all particles (with > potentially the exception of edge touching particles) and delete the whole > area. I agree that using the centroid coordinates might not always given the desired results, as you describe. Using XStart and YStart however should work. > > One workaround which mostly circumvents this problem is the use of the > particles "territories". You can do this by converting a copy of the image > into a voronoi map (>Process>Binary>Voronoi). Here, your centroids will > always be inside the region which correspond to your particles. > > 1.) You can now use either this voronoi map directly, apply your wand > selection, transfer the selection to the image with the binary particles > and delete everything inside the current selection (might take longer due > to constant switching between images) > or > 2.) You first convert the voronoi map into a binary image, invert it to > have all regions in white and the lines in black. Then run all your > elimination processes on the map (by doWand --> Clear) and finally copy > your original particles image over this map with the >Edit >Paste Control > set to "Transparent-white". This keeps only the particles with underlying > white voronoi regions. > That's an interesting strategy as well, thanks for sharing. Cheers, Jan [1]: http://imagej.nih.gov/ij/docs/guide/146-30.html#sub:Analyze-Particles... [2]: http://imagej.nih.gov/ij/docs/guide/146-30.html#toc-Subsection-30.7 > > 2014-04-03 20:20 GMT+02:00 Dan McDonald <[hidden email]>: > >> Dear List, >> >> >> >> I have been having an intermittent problem with using Analyze Particles >> masks in a macro. My code usually works but every once in a while I get an >> unexpected result: the contents of the entire mask gets "cleared". >> >> >> >> I use analyze particles with a size limit and a circularity limit on a >> thresholded image with "Show" set to mask and "Clear Results" checked. >> >> >> >> The relevant code is below. It goes through the "Results Table" and >> "Clears" every entry that has a lower aspect ratio than a specific value. >> As the Results Table entries are cleared the associated particle is removed >> from the mask. I can watch the particles that don't meet my shape criteria >> disappear one by one as the macro executes. >> >> >> >> run("Analyze Particles...", "size="+size40+"-Infinity >> circularity=0.00-"+circ40+" show=Masks clear record"); >> >> for (n=0; n<nResults; n++) { >> >> X40c = getResult('XStart',n); >> >> Y40c = getResult('YStart',n); >> >> aspectRatio40 = getResult('AR',n); >> >> doWand(X40c,Y40c); >> >> if (aspectRatio40<aspra40) { >> >> run("Clear"); >> >> } >> >> } >> >> >> -------------------------- >> >> >> >> Every once in a while (~4 times out of 150 image files) the macro will >> clear >> an entire mask. When watching the screen I can see the particles removed >> as >> expected but suddenly the entire mask is cleared. The macro seems to >> continue to run through the Results Table unaware of what it's done to the >> mask. It seems that only the mask is affected. The debug window at the >> time of the unexpected clearing is below. I do not see an obvious >> indication of a problem. >> >> >> >> The debug window shows the following after it clears the entire content of >> the mask: >> >> aspra40 3 >> >> n 1820 >> >> X40c 2786 >> >> Y40c 9117 >> >> aspectRatio40 1.6813 >> >> --------------- >> >> >> >> Is this a bug in ImageJ or am I doing something wrong? >> >> >> >> Thank you, >> >> >> >> Dan >> -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by Gabriel Landini
Hi Jan, hi Gabriel,
@ Jan Thanks for pointing that out regarding my confusion about X and XStart. I actually missed this point. I agree that this should not miss any particle using doWand(x,y); Thus, this might really point to differences between performing this in a macro or a plugin/script. @ Gabriel: Thanks for the hint with the floodFill(). Something I actually could have thought about from beginning on. Sometimes one is just blind for the obvious solution. In theory, this would improve actions if used in the Advanced Particle Analyzer with big images containing many particles. After some trials: I implemented the floodFill in the Advanced Particle Analyzer. Unfortunately, I ended up with several particles which were not eliminated while they actually should be due to the shape descriptor limitations keyed in. So, without changing anything else in the code except the method for particle elimination as described earlier by the floodFill()-method the particle analysis performs unreliably. It's a little bit weird and I still have no clue why this might be. Potentially, still a mistake in the code I missed. When I take the actual values from the results table and use it to do the floodFill manually on those coordinates it works. Just for those interested and using the Advanced Particle Analyzer: Until I can solve that issue with the flood filler, I will keep the voronoi region elimination method in the Advanced Particle Analyzer for the moment to not influence results created with it so far. Time performance thus might not be as optimal as it could be. Cheers, Jan 2014-04-04 10:47 GMT+02:00 Gabriel Landini <[hidden email]>: > On Thursday 03 Apr 2014 14:20:54 you wrote: > > I have been having an intermittent problem with using Analyze Particles > > masks in a macro. My code usually works but every once in a while I get > an > > unexpected result: the contents of the entire mask gets "cleared". > > > run("Analyze Particles...", "size="+size40+"-Infinity > > circularity=0.00-"+circ40+" show=Masks clear record"); > > > > for (n=0; n<nResults; n++) { > > X40c = getResult('XStart',n); > > Y40c = getResult('YStart',n); > > aspectRatio40 = getResult('AR',n); > > doWand(X40c,Y40c); > > if (aspectRatio40<aspra40) { > > run("Clear"); > > } > > } > > -------------------------- > > If the image you are analysing is binary (i.e. 2 colours), then what I > usually > do is the floodfill with the background colour(with the 8 neighbours > connectivity option) the XStart YStart pixel of the region I want to > delete. > That is not only less time consuming (you avoid setting ROIs) but you won't > encounter the problem you describe. > > I guess that for some strange reason the wand gets a confusing pixel > configuration and chooses the background instead of foreground. > > Also in the code above you do not need "doWand" for all the regions, but > only > for those with <aspra40. Anyway if you follow my suggestion above, it will > work. Ask if you do not know how to do that. > > Cheers > > Gabriel > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- CEO: Dr. rer. nat. Jan Brocher phone: +49 (0)6234 917 03 39 mobile: +49 (0)176 705 746 81 e-mail: [hidden email] info: [hidden email] inquiries: [hidden email] web: www.biovoxxel.de -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
On Friday 04 Apr 2014 12:08:26 you wrote:
> @ Gabriel: > Thanks for the hint with the floodFill(). Something I actually could have > thought about from beginning on. Sometimes one is just blind for the > obvious solution. > In theory, this would improve actions if used in the Advanced Particle > Analyzer with big images containing many particles. > > After some trials: > I implemented the floodFill in the Advanced Particle > Analyzer. Unfortunately, I ended up with several particles which were not > eliminated while they actually should be due to the shape descriptor > limitations keyed in. There must be something else. It does work. Are you sure you are matching the connectivity? (in this case "8")? E.g. floodFill(XStart[i],YStart[i],"8-connected"); -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hi Gabriel,
That seemed to be the problem. After explicitly stating "8-connected" as an argument it seems to work (at least for a few different test images) in comparison to the "old" method. Thanks a lot! Jan 2014-04-04 13:08 GMT+02:00 Gabriel Landini <[hidden email]>: > On Friday 04 Apr 2014 12:08:26 you wrote: > > @ Gabriel: > > Thanks for the hint with the floodFill(). Something I actually could have > > thought about from beginning on. Sometimes one is just blind for the > > obvious solution. > > In theory, this would improve actions if used in the Advanced Particle > > Analyzer with big images containing many particles. > > > > After some trials: > > I implemented the floodFill in the Advanced Particle > > Analyzer. Unfortunately, I ended up with several particles which were not > > eliminated while they actually should be due to the shape descriptor > > limitations keyed in. > > There must be something else. It does work. Are you sure you are matching > the > connectivity? (in this case "8")? E.g. > > floodFill(XStart[i],YStart[i],"8-connected"); > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- CEO: Dr. rer. nat. Jan Brocher phone: +49 (0)6234 917 03 39 mobile: +49 (0)176 705 746 81 e-mail: [hidden email] info: [hidden email] inquiries: [hidden email] web: www.biovoxxel.de -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Dear Jan, Jan and Gabriel,
Thank you so much for your prompt and incredibly helpful replies. It appears the source of the problem remains a mystery but "doWand()" executed within a macro seems the most likely culprit. Implementing Gabriel's "floodFill()" approach in my macro worked on all of my 3% images that did not work using "doWand()." It was the most straightforward way to integrate a new approach into my code. I plan to run this fix over several hundred new cases and will report back if a few cases show up not working properly with "floodFill()." Thanks again, Dan -----Original Message----- From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of BioVoxxel Sent: Friday, April 04, 2014 7:58 AM To: [hidden email] Subject: Re: Intermittent Problem With Analyze Particles Masks Hi Gabriel, That seemed to be the problem. After explicitly stating "8-connected" as an argument it seems to work (at least for a few different test images) in comparison to the "old" method. Thanks a lot! Jan 2014-04-04 13:08 GMT+02:00 Gabriel Landini <[hidden email]>: > On Friday 04 Apr 2014 12:08:26 you wrote: > > @ Gabriel: > > Thanks for the hint with the floodFill(). Something I actually could > > have thought about from beginning on. Sometimes one is just blind > > for the obvious solution. > > In theory, this would improve actions if used in the Advanced > > Particle Analyzer with big images containing many particles. > > > > After some trials: > > I implemented the floodFill in the Advanced Particle Analyzer. > > Unfortunately, I ended up with several particles which were not > > eliminated while they actually should be due to the shape descriptor > > limitations keyed in. > > There must be something else. It does work. Are you sure you are > matching the connectivity? (in this case "8")? E.g. > > floodFill(XStart[i],YStart[i],"8-connected"); > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- CEO: Dr. rer. nat. Jan Brocher phone: +49 (0)6234 917 03 39 mobile: +49 (0)176 705 746 81 e-mail: [hidden email] info: [hidden email] inquiries: [hidden email] web: www.biovoxxel.de -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |