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

rotational, translational and flip alignment using exhaustive search. More...

#include <aligner.h>

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

List of all members.

Public Member Functions

virtual EMDataalign (EMData *this_img, EMData *to_img, const string &cmp_name, const Dict &cmp_params) 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 = "rtf_slow_exhaustive"

Detailed Description

rotational, translational and flip alignment using exhaustive search.

This is very slow but can ensure localization of the global maximum

Parameters:
flipOptional. This is the flipped version of the images that is being aligned. If specified it will be used for the handedness check, if not a flipped copy of the image will be made
maxshiftThe maximum length of the detectable translational shift
transtepThe translation step to take when honing the alignment, which occurs after coarse alignment
angstepThe angular step (in degrees) to take in the exhaustive search for the solution angle. Typically very small i.e. 3 or smaller

Definition at line 1126 of file aligner.h.


Member Function Documentation

EMData * RTFSlowExhaustiveAligner::align ( EMData this_img,
EMData to_img,
const string &  cmp_name,
const Dict cmp_params 
) 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 1372 of file aligner.cpp.

References EMAN::EMData::cmp(), EMAN::EMConsts::deg2rad, EMAN::EMData::get_xsize(), InvalidParameterException, nx, EMAN::Aligner::params, EMAN::EMData::process(), EMAN::EMConsts::rad2deg, EMAN::EMData::set_attr(), EMAN::Dict::set_default(), EMAN::Transform::set_mirror(), EMAN::Transform::set_trans(), t, EMAN::EMData::transform(), and v.

Referenced by align().

{

        EMData *flip = params.set_default("flip", (EMData *) 0);
        int maxshift = params.set_default("maxshift", -1);

        EMData *flipped = 0;

        bool delete_flipped = true;
        if (flip) {
                delete_flipped = false;
                flipped = flip;
        }
        else {
                flipped = to->process("xform.flip", Dict("axis", "x"));
        }

        int nx = this_img->get_xsize();

        if (maxshift < 0) {
                maxshift = nx / 10;
        }

        float angle_step =  params.set_default("angstep", 0.0f);
        if ( angle_step == 0 ) angle_step = atan2(2.0f, (float)nx);
        else {
                angle_step *= (float)EMConsts::deg2rad; //convert to radians
        }
        float trans_step =  params.set_default("transtep",1.0f);

        if (trans_step <= 0) throw InvalidParameterException("transstep must be greater than 0");
        if (angle_step <= 0) throw InvalidParameterException("angstep must be greater than 0");


        Dict shrinkfactor("n",2);
        EMData *this_img_shrink = this_img->process("math.medianshrink",shrinkfactor);
        EMData *to_shrunk = to->process("math.medianshrink",shrinkfactor);
        EMData *flipped_shrunk = flipped->process("math.medianshrink",shrinkfactor);

        int bestflip = 0;
        float bestdx = 0;
        float bestdy = 0;

        float bestang = 0;
        float bestval = FLT_MAX;

        int half_maxshift = maxshift / 2;


        for (int dy = -half_maxshift; dy <= half_maxshift; ++dy) {
                for (int dx = -half_maxshift; dx <= half_maxshift; ++dx) {
                        if (hypot(dx, dy) <= maxshift) {
                                for (float ang = -angle_step * 2.0f; ang <= (float)2 * M_PI; ang += angle_step * 4.0f) {
                                        EMData v(*this_img_shrink);
                                        Transform t(Dict("type","2d","alpha",static_cast<float>(ang*EMConsts::rad2deg)));
                                        t.set_trans((float)dx,(float)dy);
                                        v.transform(t);
//                                      v.rotate_translate(ang*EMConsts::rad2deg, 0.0f, 0.0f, (float)dx, (float)dy, 0.0f);

                                        float lc = v.cmp(cmp_name, to_shrunk, cmp_params);

                                        if (lc < bestval) {
                                                bestval = lc;
                                                bestang = ang;
                                                bestdx = (float)dx;
                                                bestdy = (float)dy;
                                                bestflip = 0;
                                        }

                                        lc = v.cmp(cmp_name,flipped_shrunk , cmp_params);
                                        if (lc < bestval) {
                                                bestval = lc;
                                                bestang = ang;
                                                bestdx = (float)dx;
                                                bestdy = (float)dy;
                                                bestflip = 1;
                                        }
                                }
                        }
                }
        }

        if( to_shrunk )
        {
                delete to_shrunk;
                to_shrunk = 0;
        }
        if( flipped_shrunk )
        {
                delete flipped_shrunk;
                flipped_shrunk = 0;
        }
        if( this_img_shrink )
        {
                delete this_img_shrink;
                this_img_shrink = 0;
        }

        bestdx *= 2;
        bestdy *= 2;
        bestval = FLT_MAX;

        float bestdx2 = bestdx;
        float bestdy2 = bestdy;
        float bestang2 = bestang;

        for (float dy = bestdy2 - 3; dy <= bestdy2 + 3; dy += trans_step) {
                for (float dx = bestdx2 - 3; dx <= bestdx2 + 3; dx += trans_step) {
                        if (hypot(dx, dy) <= maxshift) {
                                for (float ang = bestang2 - angle_step * 6.0f; ang <= bestang2 + angle_step * 6.0f; ang += angle_step) {
                                        EMData v(*this_img);
                                        Transform t(Dict("type","2d","alpha",static_cast<float>(ang*EMConsts::rad2deg)));
                                        t.set_trans(dx,dy);
                                        v.transform(t);
//                                      v.rotate_translate(ang*EMConsts::rad2deg, 0.0f, 0.0f, (float)dx, (float)dy, 0.0f);

                                        float lc = v.cmp(cmp_name, to, cmp_params);

                                        if (lc < bestval) {
                                                bestval = lc;
                                                bestang = ang;
                                                bestdx = dx;
                                                bestdy = dy;
                                                bestflip = 0;
                                        }

                                        lc = v.cmp(cmp_name, flipped, cmp_params);

                                        if (lc < bestval) {
                                                bestval = lc;
                                                bestang = ang;
                                                bestdx = dx;
                                                bestdy = dy;
                                                bestflip = 1;
                                        }
                                }
                        }
                }
        }

        if (delete_flipped) { delete flipped; flipped = 0; }

        bestang *= (float)EMConsts::rad2deg;
        Transform t(Dict("type","2d","alpha",(float)bestang));
        t.set_trans(bestdx,bestdy);

        if (bestflip) {
                t.set_mirror(true);
        }

        EMData* rslt = this_img->process("xform",Dict("transform",&t));
        rslt->set_attr("xform.align2d",&t);

        return rslt;
}
virtual EMData* EMAN::RTFSlowExhaustiveAligner::align ( EMData this_img,
EMData to_img 
) const [inline, virtual]

Implements EMAN::Aligner.

Definition at line 1131 of file aligner.h.

References align().

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

Implements EMAN::Aligner.

Definition at line 1140 of file aligner.h.

                {
                        return "Experimental full 2D alignment with handedness check using more exhaustive search (not necessarily better than RTFBest)";
                }
virtual string EMAN::RTFSlowExhaustiveAligner::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 1135 of file aligner.h.

References NAME.

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

Implements EMAN::Aligner.

Definition at line 1150 of file aligner.h.

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

                {
                        TypeDict d;
                        d.put("flip", EMObject::EMDATA,"Optional. This is the flipped version of the images that is being aligned. If specified it will be used for the handedness check, if not a flipped copy of the image will be made");
                        d.put("maxshift", EMObject::INT,"The maximum length of the detectable translational shift");
                        d.put("transtep", EMObject::FLOAT,"The translation step to take when honing the alignment, which occurs after coarse alignment");
                        d.put("angstep", EMObject::FLOAT,"The angular step (in degrees) to take in the exhaustive search for the solution angle. Typically very small i.e. 3 or smaller.");
                        return d;
                }
static Aligner* EMAN::RTFSlowExhaustiveAligner::NEW ( ) [inline, static]

Definition at line 1145 of file aligner.h.

                {
                        return new RTFSlowExhaustiveAligner();
                }

Member Data Documentation

const string RTFSlowExhaustiveAligner::NAME = "rtf_slow_exhaustive" [static]

Definition at line 1160 of file aligner.h.

Referenced by get_name().


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