EMAN2
Public Member Functions | Static Public Member Functions | Static Public Attributes
EMAN::FlattenBackgroundProcessor Class Reference

Flattens the background by subtracting the local mean. More...

#include <processor.h>

Inheritance diagram for EMAN::FlattenBackgroundProcessor:
Inheritance graph
[legend]
Collaboration diagram for EMAN::FlattenBackgroundProcessor:
Collaboration graph
[legend]

List of all members.

Public Member Functions

void process_inplace (EMData *image)
 To process an image in-place.
string get_name () const
 Get the processor's name.
string get_desc () const
 Get the descrition of this specific processor.
TypeDict get_param_types () const
 Get processor parameter information in a dictionary.

Static Public Member Functions

static ProcessorNEW ()

Static Public Attributes

static const string NAME = "filter.flattenbackground"

Detailed Description

Flattens the background by subtracting the local mean.

Parameters:
mapan EMData object that defines the extent of the local neighbourhood - will be used for convolution
radiusexclusive of the mask parameter, this defines the radius of a circle/sphere that will be used for local mean subtraction
Author:
David Woolford
Date:
April 2008

Definition at line 3920 of file processor.h.


Member Function Documentation

string EMAN::FlattenBackgroundProcessor::get_desc ( ) const [inline, virtual]

Get the descrition of this specific processor.

This function must be overwritten by a subclass.

Returns:
The description of this processor.

Implements EMAN::Processor.

Definition at line 3935 of file processor.h.

                        {
                                return "Flattens the background by subtracting the local mean";
                        }
string EMAN::FlattenBackgroundProcessor::get_name ( ) const [inline, virtual]

Get the processor's name.

Each processor is identified by a unique name.

Returns:
The processor's name.

Implements EMAN::Processor.

Definition at line 3925 of file processor.h.

References NAME.

                        {
                                return NAME;
                        }
TypeDict EMAN::FlattenBackgroundProcessor::get_param_types ( ) const [inline, virtual]

Get processor parameter information in a dictionary.

Each parameter has one record in the dictionary. Each record contains its name, data-type, and description.

Returns:
A dictionary containing the parameter info.

Reimplemented from EMAN::Processor.

Definition at line 3940 of file processor.h.

References EMAN::EMObject::EMDATA, EMAN::EMObject::INT, and EMAN::TypeDict::put().

                        {
                                TypeDict d;
                                d.put("mask", EMObject::EMDATA, "A mask the defines the local neighborhood that will be used to find the local mean. Exclusive of the radius argument");
                                d.put("radius", EMObject::INT, "The radius of circle/sphere that defines the local neighborhood. Exclusive of the mask argument");
                                return d;
                        }
static Processor* EMAN::FlattenBackgroundProcessor::NEW ( ) [inline, static]

Definition at line 3930 of file processor.h.

                        {
                                return new FlattenBackgroundProcessor();
                        }
void FlattenBackgroundProcessor::process_inplace ( EMData image) [virtual]

To process an image in-place.

For those processors which can only be processed out-of-place, override this function to just print out some error message to remind user call the out-of-place version.

Parameters:
imageThe image to be processed.

Implements EMAN::Processor.

Definition at line 2692 of file processor.cpp.

References EMAN::EMData::clip_inplace(), EMAN::EMData::convolute(), EMAN::EMData::get_edge_mean(), EMAN::EMData::get_ndim(), EMAN::EMData::get_value_at(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), EMAN::EMData::get_zsize(), ImageDimensionException, InvalidParameterException, EMAN::EMData::mult(), EMAN::Processor::params, EMAN::EMData::process_inplace(), EMAN::Dict::set_default(), EMAN::EMData::set_size(), and EMAN::EMData::sub().

{

        EMData* mask = params.set_default("mask",(EMData*)0);
        int radius = params.set_default("radius",0);

        if (radius != 0 && mask != 0) throw InvalidParameterException("Error - the mask and radius parameters are mutually exclusive.");

        if (mask == 0 && radius == 0) throw InvalidParameterException("Error - you must specify either the mask or the radius parameter.");

        // If the radius isn't 0, then turn the mask into the thing we want...
        bool deletemask = false;
        if (radius != 0) {
                mask = new EMData;
                int n = image->get_ndim();
                if (n==1){
                        mask->set_size(2*radius+1);
                } else if (n==2) {
                        mask->set_size(2*radius+1,2*radius+1);
                }
                else /*n==3*/ {
                        mask->set_size(2*radius+1,2*radius+1,2*radius+1);
                }
                // assuming default behavior is to make a circle/sphere with using the radius of the mask
                mask->process_inplace("testimage.circlesphere");
        }

        // Double check that that mask isn't too big
        int mnx = mask->get_xsize(); int mny = mask->get_ysize(); int mnz = mask->get_zsize();
        int nx = image->get_xsize(); int ny = image->get_ysize(); int nz = image->get_zsize();
        int nxc = nx+mnx; int nyc = ny+mny; int nzc = nz+mnz;
        if (nz == 1) nzc = 1; // Sanity check
        if (ny == 1) nyc = 1; // Sanity check

        if ( mnx > nx || mny > ny || mnz > nz)
                throw ImageDimensionException("Can not flatten using a mask that is larger than the image.");

        // Get the normalization factor
        float normfac = 0.0;
        for (int i=0; i<mask->get_xsize()*mask->get_ysize()*mask->get_zsize(); ++i){
                normfac += mask->get_value_at(i);
        }
        // If the sum is zero the user probably doesn't understand that determining a measure of the mean requires
        // strictly positive numbers. The user has specified a mask that consists entirely of zeros, or the mask
        // has a mean of zero.
        if (normfac == 0) throw InvalidParameterException("Error - the pixels in the mask sum to zero. This breaks the flattening procedure");
        normfac = 1.0f/normfac;

        // The mask can now be automatically resized to the dimensions of the image
//      bool undoclip = false;

        Region r;
        if (ny == 1) r = Region((mnx-nxc)/2,nxc);
        else if (nz == 1) r = Region((mnx-nxc)/2, (mny-nyc)/2,nxc,nyc);
        else r = Region((mnx-nxc)/2, (mny-nyc)/2,(mnz-nzc)/2,nxc,nyc,nzc);
        mask->clip_inplace(r,0);
//      undoclip = true;
//      if ( mnx < nx || mny < ny || mnz < nz) {
//              Region r((mnx-nx)/2, (mny-ny)/2,(mnz-nz)/2,nx,ny,nz);
//              mask->clip_inplace(r);
//              undoclip = true;
//      }

        Region r2;
        if (ny == 1) r2 = Region((nx-nxc)/2,nxc);
        else if (nz == 1) r2 = Region((nx-nxc)/2, (ny-nyc)/2,nxc,nyc);
        else r2 = Region((nx-nxc)/2, (ny-nyc)/2,(nz-nzc)/2,nxc,nyc,nzc);
        image->clip_inplace(r2,image->get_edge_mean());
        // Finally do the convolution
        EMData* m = image->convolute(mask);
        // Normalize so that m is truly the local mean
        m->mult(normfac);
        // Before we can subtract, the mean must be phase shifted
        m->process_inplace("xform.phaseorigin.tocenter");
        // Subtract the local mean
//      image->write_image("a.mrc");
//      m->write_image("b.mrc");
        image->sub(*m); // WE'RE DONE!
        delete m;

        if (deletemask) {
                delete mask;
        } else { // I clipped it inplace, so undo this clipping so the user gets back what the put in
                Region r;
                if (ny == 1) r = Region((nxc-mnx)/2,mnx);
                else if (nz == 1) r = Region((nxc-mnx)/2, (nyc-mny)/2,mnx,mny);
                else r = Region((nxc-mnx)/2, (nyc-mny)/2,(nzc-mnz)/2,mnx,mny,mnz);
                mask->clip_inplace(r);
        }

        Region r3;
        if (ny == 1) r3 = Region((nxc-nx)/2,nx);
        else if (nz == 1) r3 = Region((nxc-nx)/2, (nyc-ny)/2,nx,ny);
        else r3 = Region((nxc-nx)/2, (nyc-ny)/2,(nzc-nz)/2,nx,ny,nz);
        image->clip_inplace(r3);
//      if ( undoclip ) {
//              Region r((nx-mnx)/2, (ny-mny)/2, (nz-mnz)/2,mnx,mny,mnz);
//              mask->clip_inplace(r);
//      }

}

Member Data Documentation

const string FlattenBackgroundProcessor::NAME = "filter.flattenbackground" [static]

Definition at line 3948 of file processor.h.

Referenced by get_name().


The documentation for this class was generated from the following files: