automate bkg substract from roi

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

automate bkg substract from roi

F Javier Díez Guerra
Dear ImageJ listers,

I wish everybody a better new year 2006.

My question:
Does anybody know of a macro or plugin to automate background substraction
using  mean gray levels calculated from user-defined roi's in a stack? I
have searched in the list archive and found no specific reference or
comment to it.

The purpose is to automate the following manual procedure:

open an image stack (already corrected for uneven illumination)
draw several roi's within dark background areas (must be done manually)
import the roi's in multimeasure plugin
do multimeasure
average the roi mean gray values obtained for each image in the stack
use this number  to process > Math > substract each image in the stack
save the new stack as Vg stack-bkg.tif
repeat again

The stacks contain images obtained from the same microscope field (cultured
cells) captured from different fluorescence channels (red, green).

I appreciate your help.

Best regards,


F Javier Diez-Guerra, PhD
Profesor Titular
Centro de Biologia Molecular Severo Ochoa
Facultad de Ciencias, Universidad Autónoma
Ctra Colmenar Viejo Km 15
Cantoblanco, 28049 Madrid
SPAIN

phone:  +34 91 4978051
Fax:      +34 91 4978087
e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: automate bkg substract from roi

Wayne Rasband
Here is a macro that subtracts the mean gray level calculated from
user-defined selections that have been added to the ROI Manager. It
works with either a single image or a stack.

     n = roiManager("count");
     if (n==0)
         exit("This macro requires at least one ROI Manager entry");
     sum = 0;
     for (i=0; i<n; i++) {
         roiManager("select", i);
         getStatistics(area, mean);
         sum += mean;
     }
     average = sum/n;
     run("Select None");
     run("Subtract...", "stack value="+average);

-wayne

On Jan 1, 2006, at 4:29 PM, F Javier Diez Guerra wrote:

> Dear ImageJ listers,
>
> I wish everybody a better new year 2006.
>
> My question:
> Does anybody know of a macro or plugin to automate background
> substraction using  mean gray levels calculated from user-defined
> roi's in a stack? I have searched in the list archive and found no
> specific reference or comment to it.
>
> The purpose is to automate the following manual procedure:
>
> open an image stack (already corrected for uneven illumination)
> draw several roi's within dark background areas (must be done manually)
> import the roi's in multimeasure plugin
> do multimeasure
> average the roi mean gray values obtained for each image in the stack
> use this number  to process > Math > substract each image in the stack
> save the new stack as Vg stack-bkg.tif
> repeat again
>
> The stacks contain images obtained from the same microscope field
> (cultured cells) captured from different fluorescence channels (red,
> green).
>
> I appreciate your help.
>
> Best regards,
>
>
> F Javier Diez-Guerra, PhD
> Profesor Titular
> Centro de Biologia Molecular Severo Ochoa
> Facultad de Ciencias, Universidad Autónoma
> Ctra Colmenar Viejo Km 15
> Cantoblanco, 28049 Madrid
> SPAIN
>
> phone:  +34 91 4978051
> Fax:      +34 91 4978087
> e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: automate bkg substract from roi

F Javier Díez Guerra
At 21:19 03/01/2006, you wrote:

>Here is a macro that subtracts the mean gray level calculated from
>user-defined selections that have been added to the ROI Manager. It works
>with either a single image or a stack.
>
>     n = roiManager("count");
>     if (n==0)
>         exit("This macro requires at least one ROI Manager entry");
>     sum = 0;
>     for (i=0; i<n; i++) {
>         roiManager("select", i);
>         getStatistics(area, mean);
>         sum += mean;
>     }
>     average = sum/n;
>     run("Select None");
>     run("Subtract...", "stack value="+average);
>
>-wayne


Wayne,

Thanks very much for the "bkg substract from roi" macro. It works very well
with single images. However, in stacks it takes the value of bkg obtained
from the first (or current) image and substracts it to the whole set of
images in the stack. Since the images within the stack share the background
roi's areas but have different background levels, the background correction
is only valid for the first (or current) image of the stack.

Is it possible to automate the process so that each image of the stack is
corrected by its own calculated level of background? Alternatively, the
stack can be first converted to images, substract background with the macro
to each image individually and then convert the images back to stack.

Best regards


F Javier Diez-Guerra, PhD
Profesor Titular
Centro de Biologia Molecular Severo Ochoa
Facultad de Ciencias, Universidad Autónoma
Ctra Colmenar Viejo Km 15
Cantoblanco, 28049 Madrid
SPAIN

phone:  +34 91 4978051
Fax:      +34 91 4978087
e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: automate bkg substract from roi

Wayne Rasband
Here is another background subtraction macro. With this one, each image
in the stack is corrected by its own calculated background level. Note
that the selection used to calculate the background level can be a
composite (discontinuous) selection created by holding down the shift
key.

     if (selectionType==-1)
        exit("This macro requires an area selection");
     for (i=1; i<=nSlices; i++) {
         setSlice(i);
         getStatistics(area, mean);
         run("Select None");
         run("Subtract...", "value="+mean);
         run("Restore Selection");
     }

-wayne


On Jan 3, 2006, at 4:46 PM, F Javier Diez Guerra wrote:

> At 21:19 03/01/2006, you wrote:
>> Here is a macro that subtracts the mean gray level calculated from
>> user-defined selections that have been added to the ROI Manager. It
>> works with either a single image or a stack.
>>
>>     n = roiManager("count");
>>     if (n==0)
>>         exit("This macro requires at least one ROI Manager entry");
>>     sum = 0;
>>     for (i=0; i<n; i++) {
>>         roiManager("select", i);
>>         getStatistics(area, mean);
>>         sum += mean;
>>     }
>>     average = sum/n;
>>     run("Select None");
>>     run("Subtract...", "stack value="+average);
>>
>> -wayne
>
>
> Wayne,
>
> Thanks very much for the "bkg substract from roi" macro. It works very
> well with single images. However, in stacks it takes the value of bkg
> obtained from the first (or current) image and substracts it to the
> whole set of images in the stack. Since the images within the stack
> share the background roi's areas but have different background levels,
> the background correction is only valid for the first (or current)
> image of the stack.
>
> Is it possible to automate the process so that each image of the stack
> is corrected by its own calculated level of background? Alternatively,
> the stack can be first converted to images, substract background with
> the macro to each image individually and then convert the images back
> to stack.
>
> Best regards
>
>
> F Javier Diez-Guerra, PhD
> Profesor Titular
> Centro de Biologia Molecular Severo Ochoa
> Facultad de Ciencias, Universidad Autónoma
> Ctra Colmenar Viejo Km 15
> Cantoblanco, 28049 Madrid
> SPAIN
>
> phone:  +34 91 4978051
> Fax:      +34 91 4978087
> e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: automate bkg substract from roi

F Javier Díez Guerra
In reply to this post by F Javier Díez Guerra
Wayne,

Thanks again for the macro to substract bkg for roi.

This has come when I was finishing a different approach (more inefficient).
I have no programming experience but had managed to get running a macro
that should also do the same work, although it is locked for stacks with
only 3 images, since I did not find the way to nest two "for()" loops. The
result was:

     n = roiManager("count");
     if (n==0)
         exit("This macro requires at least one ROI Manager entry");
         setSlice(1);
         sum = 0;
         for (j=0; j<n; j++) {
             roiManager("select", j);
             getStatistics(area, mean);
             sum += mean;
         }
         average = sum/n;
         print(average);
         run("Select None");
         run("Subtract...", "image  value="+average);
         setSlice(2);
         sum = 0;
         for (j=0; j<n; j++) {
             roiManager("select", j);
             getStatistics(area, mean);
             sum += mean;
         }
         average = sum/n;
         print(average);
         run("Select None");
         run("Subtract...", "image  value="+average);
         setSlice(3);
         sum = 0;
         for (j=0; j<n; j++) {
             roiManager("select", j);
             getStatistics(area, mean);
             sum += mean;
         }
         average = sum/n;
         print(average);
         run("Select None");
         run("Subtract...", "image  value="+average);

As you see this is a simple copy/paste of the same routine for each image
in the stack.

Well, this has taken me some time of playing around with images. I hope
that the experience will be useful for future macros.

Best regards,



F Javier Diez-Guerra, PhD
Profesor Titular
Centro de Biologia Molecular Severo Ochoa
Facultad de Ciencias, Universidad Autónoma
Ctra Colmenar Viejo Km 15
Cantoblanco, 28049 Madrid
SPAIN

phone:  +34 91 4978051
Fax:      +34 91 4978087
e-mail: [hidden email]