setting auto threshold in a macro

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

setting auto threshold in a macro

Crowell Elizabeth
Hello group members,

I have yet another question for you.
I am trying to write a macro (attached) that will select particles of a
defined size in my images (in this case, nuclei).  I make a projection
of my stack, try to auto threshold it with a dark background, make it
binary, and run analyze particles.

When I adjust the threshold manually, clicking the auto button with the
"dark background" box checked gives me exactly the result desired.  
Therefore I think my images are of sufficient quality to let me
threshold them automatically.

However, the auto threshold function is not functioning properly in the
macro.  The thresholding is not restrictive enough (it takes a wide
spectrum of gray values around the particles that just represent
noise).  When I put in a wait function and run the macro while batchmode
= false, I can see that in the threshold window, the "dark background"
box is never checked.  If I run the macro with the waitForUser line, the
image is thresholded correctly initially, and then changes to an
incorrect, non-restrictive threshold when the dialog box appears.  So
there is something really fishy going on!

How can I instruct ImageJ to adjust the threshold automatically with a
dark background?  Apparently, the line "setAutoThreshold("Default
dark");" is not sufficient.

Thank you in advance for your help,

--

Elizabeth CROWELL

----------------------------------------------------------------------
Membrane Traffic and Cell Division Research Group
Institut Pasteur
28 rue du Dr Roux
75015 PARIS, France

Tel :  01.44.38.94.07
Fax : 01.45.68.89.54
----------------------------------------------------------------------


// "BatchMeasureROIs1Auto"
//
// This macro batch processes all the files in a folder and any
// subfolders in that folder. It opens all the DAPI images and selects the
// nuclei.
// If no ROIs were selected in an image (due to automatic thresholding
// problems, for example) the macro prints out the name of the image
// in the log and moves on to the next image.

// The current settings are for selecting RPE-1 or LLCPK nuclei imaged at 60X.
// The macro does not work if there are images in the source directory.
// The images must be organized into subfolders (for ex. series1,
// series2, etc) and cannot be present at the same level as the subfolders.

// Modified from "BatchMeasureROIs1" macro by E. Crowell 26/7/2010.

   requires("1.33s");
   dir = getDirectory("Please choose a source directory.");
   setBatchMode(false);
   
   GetFiles(dir);
   
   function GetFiles(dir) {
      list = getFileList(dir);
      for (i=0; i<list.length; i++) {
          if (endsWith(list[i], "/"))
              GetFiles(""+dir+list[i]);
          else {
             if (endsWith(list[i], ".TIF")) {
                for (i=0; i<list.length; i++) {
                   if (endsWith(list[i], "DAPI.TIF")) {
                      path = dir+list[i];
                      GetROIs(path);
                   }
                }
             }
          }
      }
   }

   function GetROIs(path) {
        open(path);
        stack=getImageID();
        run("Z Project...", "start=1 stop="+nSlices+" projection=[Max Intensity]");
        selectImage(stack);
        close();
        ROIimage=getImageID();

/* run("8-bit");
        run("Median...", "radius=2");
        resetMinAndMax();
        run("Enhance Contrast", "saturated=0.5");
*/
      run("Threshold...");
                wait(1000);
                setAutoThreshold("Default dark");
                //waitForUser("Threshold", "Please adjust threshold and then click OK");
        run("Convert to Mask", "calculate");
        run("Fill Holes", "slice");
        run("Watershed", "slice");
        run("Analyze Particles...", "size=5500-30000 circularity=0.50-1.00 show=Nothing include summarize add");
        length = roiManager("count");
        print(length);
        if (length<1) {
                print(path);
        }
        selectImage(ROIimage);
        close();
    }
Reply | Threaded
Open this post in threaded view
|

Re: setting auto threshold in a macro

Rasband, Wayne (NIH/NIMH) [E]
On Jul 29, 2010, at 1:44 PM, Crowell Elizabeth wrote:

> Hello group members,
>
> I have yet another question for you.
> I am trying to write a macro (attached) that will select particles of a
> defined size in my images (in this case, nuclei).  I make a projection
> of my stack, try to auto threshold it with a dark background, make it
> binary, and run analyze particles.
>
> When I adjust the threshold manually, clicking the auto button with the
> "dark background" box checked gives me exactly the result desired.  
> Therefore I think my images are of sufficient quality to let me
> threshold them automatically.
>
> However, the auto threshold function is not functioning properly in the
> macro.  The thresholding is not restrictive enough (it takes a wide
> spectrum of gray values around the particles that just represent
> noise).  When I put in a wait function and run the macro while batchmode
> = false, I can see that in the threshold window, the "dark background"
> box is never checked.  If I run the macro with the waitForUser line, the
> image is thresholded correctly initially, and then changes to an
> incorrect, non-restrictive threshold when the dialog box appears.  So
> there is something really fishy going on!
>
> How can I instruct ImageJ to adjust the threshold automatically with a
> dark background?  Apparently, the line "setAutoThreshold("Default
> dark");" is not sufficient.
>
> Thank you in advance for your help,

What version of ImageJ are you using? There are two recent setAutoThreshold() bug fixes listed in the release notes (http://rsbweb.nih.gov/ij/notes.html).

v1.44c:
Fixed a bug in the setAutoThreshold() macro function that caused the threshold level to be one less than the level set by the "Threshold" widget when "Dark background" was enabled.

v1.43u:
Fixed a bug that caused the setAutoThreshold() macro function to not work correctly with 16 and 32 bit images that were not using the default display range.

-wayne
Reply | Threaded
Open this post in threaded view
|

Re: setting auto threshold in a macro

Crowell Elizabeth
Rasband, Wayne (NIH/NIMH) [E] a écrit :

> On Jul 29, 2010, at 1:44 PM, Crowell Elizabeth wrote:
>
>  
>> Hello group members,
>>
>> I have yet another question for you.
>> I am trying to write a macro (attached) that will select particles of a
>> defined size in my images (in this case, nuclei).  I make a projection
>> of my stack, try to auto threshold it with a dark background, make it
>> binary, and run analyze particles.
>>
>> When I adjust the threshold manually, clicking the auto button with the
>> "dark background" box checked gives me exactly the result desired.  
>> Therefore I think my images are of sufficient quality to let me
>> threshold them automatically.
>>
>> However, the auto threshold function is not functioning properly in the
>> macro.  The thresholding is not restrictive enough (it takes a wide
>> spectrum of gray values around the particles that just represent
>> noise).  When I put in a wait function and run the macro while batchmode
>> = false, I can see that in the threshold window, the "dark background"
>> box is never checked.  If I run the macro with the waitForUser line, the
>> image is thresholded correctly initially, and then changes to an
>> incorrect, non-restrictive threshold when the dialog box appears.  So
>> there is something really fishy going on!
>>
>> How can I instruct ImageJ to adjust the threshold automatically with a
>> dark background?  Apparently, the line "setAutoThreshold("Default
>> dark");" is not sufficient.
>>
>> Thank you in advance for your help,
>>    
>
> What version of ImageJ are you using? There are two recent setAutoThreshold() bug fixes listed in the release notes (http://rsbweb.nih.gov/ij/notes.html).
>
> v1.44c:
> Fixed a bug in the setAutoThreshold() macro function that caused the threshold level to be one less than the level set by the "Threshold" widget when "Dark background" was enabled.
>
> v1.43u:
> Fixed a bug that caused the setAutoThreshold() macro function to not work correctly with 16 and 32 bit images that were not using the default display range.
>
> -wayne
>  
Now I am using version 1.44d, but I still have the bugs.  I also added
back the lines I had commented that convert the image to 8-bit before
thresholding.  There is still the exact same behavior.  If it helps, I
have posted an example problematic image at the site below.  Only 2 out
of 6 images are properly thresholded by the macro.

http://www.woofiles.com/dl-201950-Zdk2DrwO-MAX1038w3DAPI.tif

--

Elizabeth CROWELL

----------------------------------------------------------------------
Membrane Traffic and Cell Division Research Group
Institut Pasteur
28 rue du Dr Roux
75015 PARIS, France

Tel :  01.44.38.94.07
Fax : 01.45.68.89.54
----------------------------------------------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: setting auto threshold in a macro

Rasband, Wayne (NIH/NIMH) [E]
On Jul 29, 2010, at 2:54 PM, Crowell Elizabeth wrote:

> Rasband, Wayne (NIH/NIMH) [E] a écrit :
>> On Jul 29, 2010, at 1:44 PM, Crowell Elizabeth wrote:
>>
>>
>>> Hello group members,
>>>
>>> I have yet another question for you.
>>> I am trying to write a macro (attached) that will select particles of a
>>> defined size in my images (in this case, nuclei).  I make a projection
>>> of my stack, try to auto threshold it with a dark background, make it
>>> binary, and run analyze particles.
>>>
>>> When I adjust the threshold manually, clicking the auto button with the
>>> "dark background" box checked gives me exactly the result desired.  
>>> Therefore I think my images are of sufficient quality to let me
>>> threshold them automatically.
>>>
>>> However, the auto threshold function is not functioning properly in the
>>> macro.  The thresholding is not restrictive enough (it takes a wide
>>> spectrum of gray values around the particles that just represent
>>> noise).  When I put in a wait function and run the macro while batchmode
>>> = false, I can see that in the threshold window, the "dark background"
>>> box is never checked.  If I run the macro with the waitForUser line, the
>>> image is thresholded correctly initially, and then changes to an
>>> incorrect, non-restrictive threshold when the dialog box appears.  So
>>> there is something really fishy going on!
>>>
>>> How can I instruct ImageJ to adjust the threshold automatically with a
>>> dark background?  Apparently, the line "setAutoThreshold("Default
>>> dark");" is not sufficient.
>>>
>>> Thank you in advance for your help,
>>>
>>
>> What version of ImageJ are you using? There are two recent setAutoThreshold() bug fixes listed in the release notes (http://rsbweb.nih.gov/ij/notes.html).
>>
>> v1.44c:
>> Fixed a bug in the setAutoThreshold() macro function that caused the threshold level to be one less than the level set by the "Threshold" widget when "Dark background" was enabled.
>>
>> v1.43u:
>> Fixed a bug that caused the setAutoThreshold() macro function to not work correctly with 16 and 32 bit images that were not using the default display range.
>>
>> -wayne
>>
> Now I am using version 1.44d, but I still have the bugs.  I also added
> back the lines I had commented that convert the image to 8-bit before
> thresholding.  There is still the exact same behavior.  If it helps, I
> have posted an example problematic image at the site below.  Only 2 out
> of 6 images are properly thresholded by the macro.
>
> http://www.woofiles.com/dl-201950-Zdk2DrwO-MAX1038w3DAPI.tif

The ImageJ 1.44e8 daily build fixes a bug that caused setAutoThreshold("Default dark") to sometimes not generate the expected threshold for an image with a sharp peak in its histogram.

-wayne


-wayne
Reply | Threaded
Open this post in threaded view
|

Re: setting auto threshold in a macro

Crowell Elizabeth
Rasband, Wayne (NIH/NIMH) [E] a écrit :

> On Jul 29, 2010, at 2:54 PM, Crowell Elizabeth wrote:
>
>  
>> Rasband, Wayne (NIH/NIMH) [E] a écrit :
>>    
>>> On Jul 29, 2010, at 1:44 PM, Crowell Elizabeth wrote:
>>>
>>>
>>>      
>>>> Hello group members,
>>>>
>>>> I have yet another question for you.
>>>> I am trying to write a macro (attached) that will select particles of a
>>>> defined size in my images (in this case, nuclei).  I make a projection
>>>> of my stack, try to auto threshold it with a dark background, make it
>>>> binary, and run analyze particles.
>>>>
>>>> When I adjust the threshold manually, clicking the auto button with the
>>>> "dark background" box checked gives me exactly the result desired.  
>>>> Therefore I think my images are of sufficient quality to let me
>>>> threshold them automatically.
>>>>
>>>> However, the auto threshold function is not functioning properly in the
>>>> macro.  The thresholding is not restrictive enough (it takes a wide
>>>> spectrum of gray values around the particles that just represent
>>>> noise).  When I put in a wait function and run the macro while batchmode
>>>> = false, I can see that in the threshold window, the "dark background"
>>>> box is never checked.  If I run the macro with the waitForUser line, the
>>>> image is thresholded correctly initially, and then changes to an
>>>> incorrect, non-restrictive threshold when the dialog box appears.  So
>>>> there is something really fishy going on!
>>>>
>>>> How can I instruct ImageJ to adjust the threshold automatically with a
>>>> dark background?  Apparently, the line "setAutoThreshold("Default
>>>> dark");" is not sufficient.
>>>>
>>>> Thank you in advance for your help,
>>>>
>>>>        
>>> What version of ImageJ are you using? There are two recent setAutoThreshold() bug fixes listed in the release notes (http://rsbweb.nih.gov/ij/notes.html).
>>>
>>> v1.44c:
>>> Fixed a bug in the setAutoThreshold() macro function that caused the threshold level to be one less than the level set by the "Threshold" widget when "Dark background" was enabled.
>>>
>>> v1.43u:
>>> Fixed a bug that caused the setAutoThreshold() macro function to not work correctly with 16 and 32 bit images that were not using the default display range.
>>>
>>> -wayne
>>>
>>>      
>> Now I am using version 1.44d, but I still have the bugs.  I also added
>> back the lines I had commented that convert the image to 8-bit before
>> thresholding.  There is still the exact same behavior.  If it helps, I
>> have posted an example problematic image at the site below.  Only 2 out
>> of 6 images are properly thresholded by the macro.
>>
>> http://www.woofiles.com/dl-201950-Zdk2DrwO-MAX1038w3DAPI.tif
>>    
>
> The ImageJ 1.44e8 daily build fixes a bug that caused setAutoThreshold("Default dark") to sometimes not generate the expected threshold for an image with a sharp peak in its histogram.
>
> -wayne
>
>
> -wayne
>  
Hello Wayne,
How do I access the daily build file?  I tried Help/Update ImageJ but it
reports that I am still running ImageJ v1.44d.
Thanks,

--

Elizabeth CROWELL

----------------------------------------------------------------------
Membrane Traffic and Cell Division Research Group
Institut Pasteur
28 rue du Dr Roux
75015 PARIS, France

Tel :  01.44.38.94.07
Fax : 01.45.68.89.54
----------------------------------------------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: setting auto threshold in a macro

Crowell Elizabeth
In reply to this post by Rasband, Wayne (NIH/NIMH) [E]
Yes, it is working now!!!
Thank you, Wayne


Rasband, Wayne (NIH/NIMH) [E] a écrit :

> On Jul 29, 2010, at 2:54 PM, Crowell Elizabeth wrote:
>
>  
>> Rasband, Wayne (NIH/NIMH) [E] a écrit :
>>    
>>> On Jul 29, 2010, at 1:44 PM, Crowell Elizabeth wrote:
>>>
>>>
>>>      
>>>> Hello group members,
>>>>
>>>> I have yet another question for you.
>>>> I am trying to write a macro (attached) that will select particles of a
>>>> defined size in my images (in this case, nuclei).  I make a projection
>>>> of my stack, try to auto threshold it with a dark background, make it
>>>> binary, and run analyze particles.
>>>>
>>>> When I adjust the threshold manually, clicking the auto button with the
>>>> "dark background" box checked gives me exactly the result desired.  
>>>> Therefore I think my images are of sufficient quality to let me
>>>> threshold them automatically.
>>>>
>>>> However, the auto threshold function is not functioning properly in the
>>>> macro.  The thresholding is not restrictive enough (it takes a wide
>>>> spectrum of gray values around the particles that just represent
>>>> noise).  When I put in a wait function and run the macro while batchmode
>>>> = false, I can see that in the threshold window, the "dark background"
>>>> box is never checked.  If I run the macro with the waitForUser line, the
>>>> image is thresholded correctly initially, and then changes to an
>>>> incorrect, non-restrictive threshold when the dialog box appears.  So
>>>> there is something really fishy going on!
>>>>
>>>> How can I instruct ImageJ to adjust the threshold automatically with a
>>>> dark background?  Apparently, the line "setAutoThreshold("Default
>>>> dark");" is not sufficient.
>>>>
>>>> Thank you in advance for your help,
>>>>
>>>>        
>>> What version of ImageJ are you using? There are two recent setAutoThreshold() bug fixes listed in the release notes (http://rsbweb.nih.gov/ij/notes.html).
>>>
>>> v1.44c:
>>> Fixed a bug in the setAutoThreshold() macro function that caused the threshold level to be one less than the level set by the "Threshold" widget when "Dark background" was enabled.
>>>
>>> v1.43u:
>>> Fixed a bug that caused the setAutoThreshold() macro function to not work correctly with 16 and 32 bit images that were not using the default display range.
>>>
>>> -wayne
>>>
>>>      
>> Now I am using version 1.44d, but I still have the bugs.  I also added
>> back the lines I had commented that convert the image to 8-bit before
>> thresholding.  There is still the exact same behavior.  If it helps, I
>> have posted an example problematic image at the site below.  Only 2 out
>> of 6 images are properly thresholded by the macro.
>>
>> http://www.woofiles.com/dl-201950-Zdk2DrwO-MAX1038w3DAPI.tif
>>    
>
> The ImageJ 1.44e8 daily build fixes a bug that caused setAutoThreshold("Default dark") to sometimes not generate the expected threshold for an image with a sharp peak in its histogram.
>
> -wayne
>
>
> -wayne
>  


--

Elizabeth CROWELL

----------------------------------------------------------------------
Membrane Traffic and Cell Division Research Group
Institut Pasteur
28 rue du Dr Roux
75015 PARIS, France

Tel :  01.44.38.94.07
Fax : 01.45.68.89.54
----------------------------------------------------------------------
Reply | Threaded
Open this post in threaded view
|

Re: setting auto threshold in a macro

mathildebd
In reply to this post by Crowell Elizabeth
Hi Elizabeth et al.,

Could you explain how you got this problem fixed? - What was the solution ? - As I have the exact same problem (ImageJ 1.49v).

Best wishes,
Mathilde