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.

Definition at line 900 of file aligner.h.


Member Function Documentation

virtual EMData* EMAN::RefineAligner::align ( EMData this_img,
EMData to_img 
) const [inline, virtual]

Implements EMAN::Aligner.

Definition at line 906 of file aligner.h.

References align().

00907                 {
00908                         return align(this_img, to_img, "sqeuclidean", Dict());
00909                 }

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_img The image to be compared.
to_img 'this_img" is aligned with 'to_img'.
cmp_name The comparison method to compare the two images.
cmp_params The parameter dictionary for comparison method.
Returns:
The aligned image.

Implements EMAN::Aligner.

Definition at line 1458 of file aligner.cpp.

References EMAN::EMData::get_attr(), 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().

01460 {
01461 
01462         if (!to) {
01463                 return 0;
01464         }
01465 
01466         EMData *result;
01467         int mode = params.set_default("mode", 0);
01468         float saz = 0.0;
01469         float sdx = 0.0;
01470         float sdy = 0.0;
01471         float sscale = 1.0;
01472         bool mirror = false;
01473         Transform* t;
01474         if (params.has_key("xform.align2d") ) {
01475                 t = params["xform.align2d"];
01476                 Dict params = t->get_params("2d");
01477                 saz = params["alpha"];
01478                 sdx = params["tx"];
01479                 sdy = params["ty"];
01480                 mirror = params["mirror"];
01481                 sscale = params["scale"];
01482         } else {
01483                 t = new Transform(); // is the identity
01484         }
01485 
01486         // We do this to prevent the GSL routine from crashing on an invalid alignment
01487         if ((float)(this_img->get_attr("sigma"))==0.0 || (float)(to->get_attr("sigma"))==0.0) {
01488                 result = this_img->process("xform",Dict("transform",t));
01489                 result->set_attr("xform.align2d",t);
01490                 delete t;
01491                 return result;
01492         }
01493         
01494         float stepx = params.set_default("stepx",1.0f);
01495         float stepy = params.set_default("stepy",1.0f);
01496         // Default step is 5 degree - note in EMAN1 it was 0.1 radians
01497         float stepaz = params.set_default("stepaz",5.0f);
01498         float stepscale = params.set_default("stepscale",0.0f);
01499 
01500         int np = 3;
01501         if (stepscale!=0.0) np++;
01502         Dict gsl_params;
01503         gsl_params["this"] = this_img;
01504         gsl_params["with"] = to;
01505         gsl_params["snr"]  = params["snr"];
01506         gsl_params["mirror"] = mirror;
01507 
01508         const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
01509         gsl_vector *ss = gsl_vector_alloc(np);
01510 
01511 
01512         gsl_vector_set(ss, 0, stepx);
01513         gsl_vector_set(ss, 1, stepy);
01514         gsl_vector_set(ss, 2, stepaz);
01515         if (stepscale!=0.0) gsl_vector_set(ss,3,stepscale);
01516         
01517         gsl_vector *x = gsl_vector_alloc(np);
01518         gsl_vector_set(x, 0, sdx);
01519         gsl_vector_set(x, 1, sdy);
01520         gsl_vector_set(x, 2, saz);
01521         if (stepscale!=0.0) gsl_vector_set(x,3,1.0);
01522         
01523         Cmp *c = 0;
01524 
01525         gsl_multimin_function minex_func;
01526         if (mode == 2) {
01527                 minex_func.f = &refalifnfast;
01528         }
01529         else {
01530                 c = Factory < Cmp >::get(cmp_name, cmp_params);
01531                 gsl_params["cmp"] = (void *) c;
01532                 minex_func.f = &refalifn;
01533         }
01534 
01535         minex_func.n = np;
01536         minex_func.params = (void *) &gsl_params;
01537 
01538         gsl_multimin_fminimizer *s = gsl_multimin_fminimizer_alloc(T, np);
01539         gsl_multimin_fminimizer_set(s, &minex_func, x, ss);
01540 
01541         int rval = GSL_CONTINUE;
01542         int status = GSL_SUCCESS;
01543         int iter = 1;
01544 
01545         float precision = params.set_default("precision",0.04f);
01546         int maxiter = params.set_default("maxiter",28);
01547 
01548 //      printf("Refine sx=%1.2f sy=%1.2f sa=%1.2f prec=%1.4f maxit=%d\n",stepx,stepy,stepaz,precision,maxiter);
01549 //      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));
01550 
01551         while (rval == GSL_CONTINUE && iter < maxiter) {
01552                 iter++;
01553                 status = gsl_multimin_fminimizer_iterate(s);
01554                 if (status) {
01555                         break;
01556                 }
01557                 rval = gsl_multimin_test_size(gsl_multimin_fminimizer_size(s), precision);
01558         }
01559 
01560         int maxshift = params.set_default("maxshift",-1);
01561 
01562         if (maxshift <= 0) {
01563                 maxshift = this_img->get_xsize() / 4;
01564         }
01565         float fmaxshift = static_cast<float>(maxshift);
01566         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))  )
01567         {
01568 //              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));
01569                 Transform  tsoln(Dict("type","2d","alpha",(float)gsl_vector_get(s->x, 2)));
01570                 tsoln.set_mirror(mirror);
01571                 tsoln.set_trans((float)gsl_vector_get(s->x, 0),(float)gsl_vector_get(s->x, 1));
01572                 if (stepscale!=0.0) tsoln.set_scale((float)gsl_vector_get(s->x, 3));
01573                 result = this_img->process("xform",Dict("transform",&tsoln));
01574                 result->set_attr("xform.align2d",&tsoln);
01575         } else { // The refine aligner failed - this shift went beyond the max shift
01576 //              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));
01577                 result = this_img->process("xform",Dict("transform",t));
01578                 result->set_attr("xform.align2d",t);
01579         }
01580 
01581         delete t;
01582         t = 0;
01583 
01584         gsl_vector_free(x);
01585         gsl_vector_free(ss);
01586         gsl_multimin_fminimizer_free(s);
01587 
01588         if ( c != 0 ) delete c;
01589         return result;
01590 }

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

Implements EMAN::Aligner.

Definition at line 916 of file aligner.h.

00917                 {
00918                         return "Refines a preliminary 2D alignment using a simplex algorithm. Subpixel precision.";
00919                 }

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 911 of file aligner.h.

References NAME.

00912                 {
00913                         return NAME;
00914                 }

virtual TypeDict EMAN::RefineAligner::get_param_types (  )  const [inline, virtual]

Implements EMAN::Aligner.

Definition at line 926 of file aligner.h.

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

00927                 {
00928                         TypeDict d;
00929 
00930                         d.put("mode", EMObject::INT, "Currently unused");
00931                         d.put("xform.align2d", EMObject::TRANSFORM, "The Transform storing the starting guess. If unspecified the identity matrix is used");
00932                         d.put("stepx", EMObject::FLOAT, "The x increment used to create the starting simplex. Default is 1");
00933                         d.put("stepy", EMObject::FLOAT, "The y increment used to create the starting simplex. Default is 1");
00934                         d.put("stepaz", EMObject::FLOAT, "The rotational increment used to create the starting simplex. Default is 5");
00935                         d.put("precision", EMObject::FLOAT, "The precision which, if achieved, can stop the iterative refinement before reaching the maximum iterations. Default is 0.04.");
00936                         d.put("maxiter", EMObject::INT,"The maximum number of iterations that can be performed by the Simplex minimizer");
00937                         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.");
00938                         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.");
00939                         return d;
00940                 }

static Aligner* EMAN::RefineAligner::NEW (  )  [inline, static]

Definition at line 921 of file aligner.h.

00922                 {
00923                         return new RefineAligner();
00924                 }


Member Data Documentation

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

Definition at line 942 of file aligner.h.

Referenced by get_name().


The documentation for this class was generated from the following files:
Generated on Thu Nov 17 12:45:10 2011 for EMAN2 by  doxygen 1.4.7