#include <aligner.h>
Inheritance diagram for EMAN::RefineAligner:
Public Member Functions | |
virtual EMData * | align (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 EMData * | align (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 Aligner * | NEW () |
Static Public Attributes | |
static const string | NAME = "refine" |
Refines a preliminary 2D alignment using a simplex algorithm. Subpixel precision.
Definition at line 1206 of file aligner.h.
virtual EMData* EMAN::RefineAligner::align | ( | EMData * | this_img, | |
EMData * | to_img | |||
) | const [inline, virtual] |
Implements EMAN::Aligner.
Definition at line 1212 of file aligner.h.
References align().
01213 { 01214 return align(this_img, to_img, "sqeuclidean", Dict()); 01215 }
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.
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. |
Implements EMAN::Aligner.
Definition at line 1642 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().
01644 { 01645 01646 if (!to) { 01647 return 0; 01648 } 01649 01650 EMData *result; 01651 int mode = params.set_default("mode", 0); 01652 float saz = 0.0; 01653 float sdx = 0.0; 01654 float sdy = 0.0; 01655 float sscale = 1.0; 01656 bool mirror = false; 01657 Transform* t; 01658 if (params.has_key("xform.align2d") ) { 01659 t = params["xform.align2d"]; 01660 Dict params = t->get_params("2d"); 01661 saz = params["alpha"]; 01662 sdx = params["tx"]; 01663 sdy = params["ty"]; 01664 mirror = params["mirror"]; 01665 sscale = params["scale"]; 01666 } else { 01667 t = new Transform(); // is the identity 01668 } 01669 01670 // We do this to prevent the GSL routine from crashing on an invalid alignment 01671 if ((float)(this_img->get_attr("sigma"))==0.0 || (float)(to->get_attr("sigma"))==0.0) { 01672 result = this_img->process("xform",Dict("transform",t)); 01673 result->set_attr("xform.align2d",t); 01674 delete t; 01675 return result; 01676 } 01677 01678 float stepx = params.set_default("stepx",1.0f); 01679 float stepy = params.set_default("stepy",1.0f); 01680 // Default step is 5 degree - note in EMAN1 it was 0.1 radians 01681 float stepaz = params.set_default("stepaz",5.0f); 01682 float stepscale = params.set_default("stepscale",0.0f); 01683 01684 int np = 3; 01685 if (stepscale!=0.0) np++; 01686 Dict gsl_params; 01687 gsl_params["this"] = this_img; 01688 gsl_params["with"] = to; 01689 gsl_params["snr"] = params["snr"]; 01690 gsl_params["mirror"] = mirror; 01691 01692 const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex; 01693 gsl_vector *ss = gsl_vector_alloc(np); 01694 01695 01696 gsl_vector_set(ss, 0, stepx); 01697 gsl_vector_set(ss, 1, stepy); 01698 gsl_vector_set(ss, 2, stepaz); 01699 if (stepscale!=0.0) gsl_vector_set(ss,3,stepscale); 01700 01701 gsl_vector *x = gsl_vector_alloc(np); 01702 gsl_vector_set(x, 0, sdx); 01703 gsl_vector_set(x, 1, sdy); 01704 gsl_vector_set(x, 2, saz); 01705 if (stepscale!=0.0) gsl_vector_set(x,3,1.0); 01706 01707 Cmp *c = 0; 01708 01709 gsl_multimin_function minex_func; 01710 if (mode == 2) { 01711 minex_func.f = &refalifnfast; 01712 } 01713 else { 01714 c = Factory < Cmp >::get(cmp_name, cmp_params); 01715 gsl_params["cmp"] = (void *) c; 01716 minex_func.f = &refalifn; 01717 } 01718 01719 minex_func.n = np; 01720 minex_func.params = (void *) &gsl_params; 01721 01722 gsl_multimin_fminimizer *s = gsl_multimin_fminimizer_alloc(T, np); 01723 gsl_multimin_fminimizer_set(s, &minex_func, x, ss); 01724 01725 int rval = GSL_CONTINUE; 01726 int status = GSL_SUCCESS; 01727 int iter = 1; 01728 01729 float precision = params.set_default("precision",0.04f); 01730 int maxiter = params.set_default("maxiter",28); 01731 01732 // printf("Refine sx=%1.2f sy=%1.2f sa=%1.2f prec=%1.4f maxit=%d\n",stepx,stepy,stepaz,precision,maxiter); 01733 // 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)); 01734 01735 while (rval == GSL_CONTINUE && iter < maxiter) { 01736 iter++; 01737 status = gsl_multimin_fminimizer_iterate(s); 01738 if (status) { 01739 break; 01740 } 01741 rval = gsl_multimin_test_size(gsl_multimin_fminimizer_size(s), precision); 01742 } 01743 01744 int maxshift = params.set_default("maxshift",-1); 01745 01746 if (maxshift <= 0) { 01747 maxshift = this_img->get_xsize() / 4; 01748 } 01749 float fmaxshift = static_cast<float>(maxshift); 01750 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)) ) 01751 { 01752 // 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)); 01753 Transform tsoln(Dict("type","2d","alpha",(float)gsl_vector_get(s->x, 2))); 01754 tsoln.set_mirror(mirror); 01755 tsoln.set_trans((float)gsl_vector_get(s->x, 0),(float)gsl_vector_get(s->x, 1)); 01756 if (stepscale!=0.0) tsoln.set_scale((float)gsl_vector_get(s->x, 3)); 01757 result = this_img->process("xform",Dict("transform",&tsoln)); 01758 result->set_attr("xform.align2d",&tsoln); 01759 } else { // The refine aligner failed - this shift went beyond the max shift 01760 // 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)); 01761 result = this_img->process("xform",Dict("transform",t)); 01762 result->set_attr("xform.align2d",t); 01763 } 01764 01765 delete t; 01766 t = 0; 01767 01768 gsl_vector_free(x); 01769 gsl_vector_free(ss); 01770 gsl_multimin_fminimizer_free(s); 01771 01772 if (c != 0) delete c; 01773 return result; 01774 }
virtual string EMAN::RefineAligner::get_desc | ( | ) | const [inline, virtual] |
Implements EMAN::Aligner.
Definition at line 1222 of file aligner.h.
01223 { 01224 return "Refines a preliminary 2D alignment using a simplex algorithm. Subpixel precision."; 01225 }
virtual string EMAN::RefineAligner::get_name | ( | ) | const [inline, virtual] |
virtual TypeDict EMAN::RefineAligner::get_param_types | ( | ) | const [inline, virtual] |
Implements EMAN::Aligner.
Definition at line 1232 of file aligner.h.
References EMAN::EMObject::FLOAT, EMAN::EMObject::INT, EMAN::TypeDict::put(), and EMAN::EMObject::TRANSFORM.
01233 { 01234 TypeDict d; 01235 01236 d.put("mode", EMObject::INT, "Currently unused"); 01237 d.put("xform.align2d", EMObject::TRANSFORM, "The Transform storing the starting guess. If unspecified the identity matrix is used"); 01238 d.put("stepx", EMObject::FLOAT, "The x increment used to create the starting simplex. Default is 1"); 01239 d.put("stepy", EMObject::FLOAT, "The y increment used to create the starting simplex. Default is 1"); 01240 d.put("stepaz", EMObject::FLOAT, "The rotational increment used to create the starting simplex. Default is 5"); 01241 d.put("precision", EMObject::FLOAT, "The precision which, if achieved, can stop the iterative refinement before reaching the maximum iterations. Default is 0.04."); 01242 d.put("maxiter", EMObject::INT,"The maximum number of iterations that can be performed by the Simplex minimizer"); 01243 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."); 01244 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."); 01245 return d; 01246 }
static Aligner* EMAN::RefineAligner::NEW | ( | ) | [inline, static] |
const string RefineAligner::NAME = "refine" [static] |