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

refine alignment. More...

#include <aligner.h>

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

List of all members.

Public Member Functions

virtual EMDataalign (EMData *this_img, EMData *to_img, const string &cmp_name="dot", const Dict &cmp_params=Dict()) const
 To align 'this_img' with another image passed in through its parameters.
virtual EMDataalign (EMData *this_img, EMData *to_img) const
virtual string get_name () const
 Get the Aligner's name.
virtual string get_desc () const
virtual TypeDict get_param_types () const

Static Public Member Functions

static AlignerNEW ()

Static Public Attributes

static const string NAME = "refine"

Detailed Description

refine alignment.

Refines a preliminary 2D alignment using a simplex algorithm. Subpixel precision.

Parameters:
xform.align2dThe Transform storing the starting guess. If unspecified the identity matrix is used
stepxThe x increment used to create the starting simplex. Default is 1
stepyThe y increment used to create the starting simplex. Default is 1
stepazThe rotational increment used to create the starting simplex. Default is 5
precisionThe precision which, if achieved, can stop the iterative refinement before reaching the maximum iterations. Default is 0.04
maxiterThe maximum number of iterations. default=28
maxshiftMaximum translation in pixels in any direction. If the solution yields a shift beyond this value in any direction, then the refinement is judged a failure and the original alignment is used as the solution
stepscaleIf set to any non-zero value, scale will be included in the alignment, and this will be the initial step. Images should be edgenormalized. If the scale goes beyond +-30% alignment will fail
verboseThis will cause debugging information to be printed on the screen for the iterative refinement. Larger numbers -> more info. default=0

Definition at line 1221 of file aligner.h.


Member Function Documentation

EMData * RefineAligner::align ( EMData this_img,
EMData to_img,
const string &  cmp_name = "dot",
const Dict cmp_params = Dict() 
) const [virtual]

To align 'this_img' with another image passed in through its parameters.

The alignment uses a user-given comparison method to compare the two images. If none is given, a default one is used.

Parameters:
this_imgThe image to be compared.
to_img'this_img" is aligned with 'to_img'.
cmp_nameThe comparison method to compare the two images.
cmp_paramsThe parameter dictionary for comparison method.
Returns:
The aligned image.

Implements EMAN::Aligner.

Definition at line 1688 of file aligner.cpp.

References EMAN::EMData::get_attr(), EMAN::Transform::get_params(), EMAN::EMData::get_xsize(), EMAN::Dict::has_key(), EMAN::Aligner::params, EMAN::EMData::process(), refalifn(), refalifnfast(), EMAN::EMData::set_attr(), EMAN::Dict::set_default(), EMAN::Transform::set_mirror(), EMAN::Transform::set_scale(), EMAN::Transform::set_trans(), status, t, and x.

Referenced by align().

{

        if (!to) {
                return 0;
        }

        EMData *result;
        int mode = params.set_default("mode", 0);
        float saz = 0.0;
        float sdx = 0.0;
        float sdy = 0.0;
        float sscale = 1.0;
        bool mirror = false;
        Transform* t;
        if (params.has_key("xform.align2d") ) {
                t = params["xform.align2d"];
                Dict params = t->get_params("2d");
                saz = params["alpha"];
                sdx = params["tx"];
                sdy = params["ty"];
                mirror = params["mirror"];
                sscale = params["scale"];
        } else {
                t = new Transform(); // is the identity
        }

        // We do this to prevent the GSL routine from crashing on an invalid alignment
        if ((float)(this_img->get_attr("sigma"))==0.0 || (float)(to->get_attr("sigma"))==0.0) {
                result = this_img->process("xform",Dict("transform",t));
                result->set_attr("xform.align2d",t);
                delete t;
                return result;
        }
        
        float stepx = params.set_default("stepx",1.0f);
        float stepy = params.set_default("stepy",1.0f);
        // Default step is 5 degree - note in EMAN1 it was 0.1 radians
        float stepaz = params.set_default("stepaz",5.0f);
        float stepscale = params.set_default("stepscale",0.0f);

        int np = 3;
        if (stepscale!=0.0) np++;
        Dict gsl_params;
        gsl_params["this"] = this_img;
        gsl_params["with"] = to;
        gsl_params["snr"]  = params["snr"];
        gsl_params["mirror"] = mirror;
        if (params.has_key("mask")) gsl_params["mask"]=params["mask"];
        
        const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
        gsl_vector *ss = gsl_vector_alloc(np);


        gsl_vector_set(ss, 0, stepx);
        gsl_vector_set(ss, 1, stepy);
        gsl_vector_set(ss, 2, stepaz);
        if (stepscale!=0.0) gsl_vector_set(ss,3,stepscale);
        
        gsl_vector *x = gsl_vector_alloc(np);
        gsl_vector_set(x, 0, sdx);
        gsl_vector_set(x, 1, sdy);
        gsl_vector_set(x, 2, saz);
        if (stepscale!=0.0) gsl_vector_set(x,3,1.0);
        
        Cmp *c = 0;

        gsl_multimin_function minex_func;
        if (mode == 2) {
                minex_func.f = &refalifnfast;
        }
        else {
                c = Factory < Cmp >::get(cmp_name, cmp_params);
                gsl_params["cmp"] = (void *) c;
                minex_func.f = &refalifn;
        }

        minex_func.n = np;
        minex_func.params = (void *) &gsl_params;

        gsl_multimin_fminimizer *s = gsl_multimin_fminimizer_alloc(T, np);
        gsl_multimin_fminimizer_set(s, &minex_func, x, ss);

        int rval = GSL_CONTINUE;
        int status = GSL_SUCCESS;
        int iter = 1;

        float precision = params.set_default("precision",0.04f);
        int maxiter = params.set_default("maxiter",28);

//      printf("Refine sx=%1.2f sy=%1.2f sa=%1.2f prec=%1.4f maxit=%d\n",stepx,stepy,stepaz,precision,maxiter);
//      printf("%1.2f %1.2f %1.1f  ->",(float)gsl_vector_get(s->x, 0),(float)gsl_vector_get(s->x, 1),(float)gsl_vector_get(s->x, 2));

        while (rval == GSL_CONTINUE && iter < maxiter) {
                iter++;
                status = gsl_multimin_fminimizer_iterate(s);
                if (status) {
                        break;
                }
                rval = gsl_multimin_test_size(gsl_multimin_fminimizer_size(s), precision);
        }

        int maxshift = params.set_default("maxshift",-1);

        if (maxshift <= 0) {
                maxshift = this_img->get_xsize() / 4;
        }
        float fmaxshift = static_cast<float>(maxshift);
        if ( fmaxshift >= fabs((float)gsl_vector_get(s->x, 0)) && fmaxshift >= fabs((float)gsl_vector_get(s->x, 1)) && (stepscale==0 || (((float)gsl_vector_get(s->x, 3))<1.3 && ((float)gsl_vector_get(s->x, 3))<0.7))  )
        {
//              printf(" Refine good %1.2f %1.2f %1.1f\n",(float)gsl_vector_get(s->x, 0),(float)gsl_vector_get(s->x, 1),(float)gsl_vector_get(s->x, 2));
                Transform  tsoln(Dict("type","2d","alpha",(float)gsl_vector_get(s->x, 2)));
                tsoln.set_mirror(mirror);
                tsoln.set_trans((float)gsl_vector_get(s->x, 0),(float)gsl_vector_get(s->x, 1));
                if (stepscale!=0.0) tsoln.set_scale((float)gsl_vector_get(s->x, 3));
                result = this_img->process("xform",Dict("transform",&tsoln));
                result->set_attr("xform.align2d",&tsoln);
        } else { // The refine aligner failed - this shift went beyond the max shift
//              printf(" Refine Failed %1.2f %1.2f %1.1f\n",(float)gsl_vector_get(s->x, 0),(float)gsl_vector_get(s->x, 1),(float)gsl_vector_get(s->x, 2));
                result = this_img->process("xform",Dict("transform",t));
                result->set_attr("xform.align2d",t);
        }

        delete t;
        t = 0;

        gsl_vector_free(x);
        gsl_vector_free(ss);
        gsl_multimin_fminimizer_free(s);

        if (c != 0) delete c;
        return result;
}
virtual EMData* EMAN::RefineAligner::align ( EMData this_img,
EMData to_img 
) const [inline, virtual]

Implements EMAN::Aligner.

Definition at line 1227 of file aligner.h.

References align().

                {
                        return align(this_img, to_img, "sqeuclidean", Dict());
                }
virtual string EMAN::RefineAligner::get_desc ( ) const [inline, virtual]

Implements EMAN::Aligner.

Definition at line 1237 of file aligner.h.

                {
                        return "Refines a preliminary 2D alignment using a simplex algorithm. Subpixel precision.";
                }
virtual string EMAN::RefineAligner::get_name ( ) const [inline, virtual]

Get the Aligner's name.

Each Aligner is identified by a unique name.

Returns:
The Aligner's name.

Implements EMAN::Aligner.

Definition at line 1232 of file aligner.h.

References NAME.

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

Implements EMAN::Aligner.

Definition at line 1247 of file aligner.h.

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

                {
                        TypeDict d;

                        d.put("mode", EMObject::INT, "Currently unused");
                        d.put("xform.align2d", EMObject::TRANSFORM, "The Transform storing the starting guess. If unspecified the identity matrix is used");
                        d.put("stepx", EMObject::FLOAT, "The x increment used to create the starting simplex. Default is 1");
                        d.put("stepy", EMObject::FLOAT, "The y increment used to create the starting simplex. Default is 1");
                        d.put("stepaz", EMObject::FLOAT, "The rotational increment used to create the starting simplex. Default is 5");
                        d.put("precision", EMObject::FLOAT, "The precision which, if achieved, can stop the iterative refinement before reaching the maximum iterations. Default is 0.04.");
                        d.put("maxiter", EMObject::INT,"The maximum number of iterations that can be performed by the Simplex minimizer. default=28");
                        d.put("maxshift", EMObject::INT,"Maximum translation in pixels in any direction. If the solution yields a shift beyond this value in any direction, then the refinement is judged a failure and the original alignment is used as the solution.");
                        d.put("stepscale", EMObject::FLOAT, "If set to any non-zero value, scale will be included in the alignment, and this will be the initial step. Images should be edgenormalized. If the scale goes beyond +-30% alignment will fail.");
                        d.put("mask", EMObject::EMDATA, "A mask to be applied to the image being aligned prior to each similarity comparison.");
                        d.put("verbose", EMObject::INT, "This will cause debugging information to be printed on the screen for the iterative refinement. Larger numbers -> more info. default=0");
                        return d;
                }
static Aligner* EMAN::RefineAligner::NEW ( ) [inline, static]

Definition at line 1242 of file aligner.h.

                {
                        return new RefineAligner();
                }

Member Data Documentation

const string RefineAligner::NAME = "refine" [static]

Definition at line 1265 of file aligner.h.

Referenced by get_name().


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