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

rotational, translational and flip alignment using real-space methods. More...

#include <aligner.h>

Inheritance diagram for EMAN::RTFExhaustiveAligner:
Inheritance graph
[legend]
Collaboration diagram for EMAN::RTFExhaustiveAligner:
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 = "rtf_exhaustive"

Detailed Description

rotational, translational and flip alignment using real-space methods.

slow

Parameters:
flip
maxshiftMaximum translation in pixels

Definition at line 1082 of file aligner.h.


Member Function Documentation

EMData * RTFExhaustiveAligner::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 1113 of file aligner.cpp.

References EMAN::Util::calc_best_fft_size(), EMAN::EMData::calc_ccfx(), EMAN::EMData::calc_max_index(), EMAN::EMData::cmp(), EMAN::EMData::copy(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), InvalidParameterException, ny, EMAN::Aligner::params, EMAN::EMData::process(), EMAN::EMConsts::rad2deg, EMAN::EMData::rotate_x(), EMAN::EMData::set_attr(), EMAN::Dict::set_default(), EMAN::Transform::set_mirror(), EMAN::Transform::set_pre_trans(), t, and EMAN::EMData::unwrap().

Referenced by align().

{
        EMData *flip = params.set_default("flip", (EMData *) 0);
        int maxshift = params.set_default("maxshift", this_img->get_xsize()/8);
        if (maxshift < 2) throw InvalidParameterException("maxshift must be greater than or equal to 2");

        int ny = this_img->get_ysize();
        int xst = (int) floor(2 * M_PI * ny);
        xst = Util::calc_best_fft_size(xst);

        Dict d("n",2);
        EMData *to_shrunk_unwrapped = to->process("math.medianshrink",d);

        int to_copy_r2 = to_shrunk_unwrapped->get_ysize() / 2 - 2 - maxshift / 2;
        EMData *tmp = to_shrunk_unwrapped->unwrap(4, to_copy_r2, xst / 2, 0, 0, true);
        if( to_shrunk_unwrapped )
        {
                delete to_shrunk_unwrapped;
                to_shrunk_unwrapped = 0;
        }
        to_shrunk_unwrapped = tmp;

        EMData *to_shrunk_unwrapped_copy = to_shrunk_unwrapped->copy();
        EMData* to_unwrapped = to->unwrap(4, to->get_ysize() / 2 - 2 - maxshift, xst, 0, 0, true);
        EMData *to_unwrapped_copy = to_unwrapped->copy();

        bool delete_flipped = true;
        EMData *flipped = 0;
        if (flip) {
                delete_flipped = false;
                flipped = flip;
        }
        else {
                flipped = to->process("xform.flip", Dict("axis", "x"));
        }
        EMData *to_shrunk_flipped_unwrapped = flipped->process("math.medianshrink",d);
        tmp = to_shrunk_flipped_unwrapped->unwrap(4, to_copy_r2, xst / 2, 0, 0, true);
        if( to_shrunk_flipped_unwrapped )
        {
                delete to_shrunk_flipped_unwrapped;
                to_shrunk_flipped_unwrapped = 0;
        }
        to_shrunk_flipped_unwrapped = tmp;
        EMData *to_shrunk_flipped_unwrapped_copy = to_shrunk_flipped_unwrapped->copy();
        EMData* to_flip_unwrapped = flipped->unwrap(4, to->get_ysize() / 2 - 2 - maxshift, xst, 0, 0, true);
        EMData* to_flip_unwrapped_copy = to_flip_unwrapped->copy();

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

        EMData *this_shrunk_2 = this_img->process("math.medianshrink",d);

        float bestval = FLT_MAX;
        float bestang = 0;
        int bestflip = 0;
        float bestdx = 0;
        float bestdy = 0;

        int half_maxshift = maxshift / 2;

        int ur2 = this_shrunk_2->get_ysize() / 2 - 2 - half_maxshift;
        for (int dy = -half_maxshift; dy <= half_maxshift; dy += 1) {
                for (int dx = -half_maxshift; dx <= half_maxshift; dx += 1) {
#ifdef  _WIN32
                        if (_hypot(dx, dy) <= half_maxshift) {
#else
                        if (hypot(dx, dy) <= half_maxshift) {
#endif
                                EMData *uw = this_shrunk_2->unwrap(4, ur2, xst / 2, dx, dy, true);
                                EMData *uwc = uw->copy();
                                EMData *a = uw->calc_ccfx(to_shrunk_unwrapped);

                                uwc->rotate_x(a->calc_max_index());
                                float cm = uwc->cmp(cmp_name, to_shrunk_unwrapped_copy, cmp_params);
                                if (cm < bestval) {
                                        bestval = cm;
                                        bestang = (float) (2.0 * M_PI * a->calc_max_index() / a->get_xsize());
                                        bestdx = (float)dx;
                                        bestdy = (float)dy;
                                        bestflip = 0;
                                }


                                if( a )
                                {
                                        delete a;
                                        a = 0;
                                }
                                if( uw )
                                {
                                        delete uw;
                                        uw = 0;
                                }
                                if( uwc )
                                {
                                        delete uwc;
                                        uwc = 0;
                                }
                                uw = this_shrunk_2->unwrap(4, ur2, xst / 2, dx, dy, true);
                                uwc = uw->copy();
                                a = uw->calc_ccfx(to_shrunk_flipped_unwrapped);

                                uwc->rotate_x(a->calc_max_index());
                                cm = uwc->cmp(cmp_name, to_shrunk_flipped_unwrapped_copy, cmp_params);
                                if (cm < bestval) {
                                        bestval = cm;
                                        bestang = (float) (2.0 * M_PI * a->calc_max_index() / a->get_xsize());
                                        bestdx = (float)dx;
                                        bestdy = (float)dy;
                                        bestflip = 1;
                                }

                                if( a )
                                {
                                        delete a;
                                        a = 0;
                                }

                                if( uw )
                                {
                                        delete uw;
                                        uw = 0;
                                }
                                if( uwc )
                                {
                                        delete uwc;
                                        uwc = 0;
                                }
                        }
                }
        }
        if( this_shrunk_2 )
        {
                delete this_shrunk_2;
                this_shrunk_2 = 0;
        }
        if( to_shrunk_unwrapped )
        {
                delete to_shrunk_unwrapped;
                to_shrunk_unwrapped = 0;
        }
        if( to_shrunk_unwrapped_copy )
        {
                delete to_shrunk_unwrapped_copy;
                to_shrunk_unwrapped_copy = 0;
        }
        if( to_shrunk_flipped_unwrapped )
        {
                delete to_shrunk_flipped_unwrapped;
                to_shrunk_flipped_unwrapped = 0;
        }
        if( to_shrunk_flipped_unwrapped_copy )
        {
                delete to_shrunk_flipped_unwrapped_copy;
                to_shrunk_flipped_unwrapped_copy = 0;
        }
        bestdx *= 2;
        bestdy *= 2;
        bestval = FLT_MAX;

        float bestdx2 = bestdx;
        float bestdy2 = bestdy;
        // Note I tried steps less than 1.0 (sub pixel precision) and it actually appeared detrimental
        // So my advice is to stick with dx += 1.0 etc unless you really are looking to fine tune this
        // algorithm
        for (float dy = bestdy2 - 3; dy <= bestdy2 + 3; dy += 1.0 ) {
                for (float dx = bestdx2 - 3; dx <= bestdx2 + 3; dx += 1.0 ) {

#ifdef  _WIN32
                        if (_hypot(dx, dy) <= maxshift) {
#else
                        if (hypot(dx, dy) <= maxshift) {
#endif
                                EMData *uw = this_img->unwrap(4, this_img->get_ysize() / 2 - 2 - maxshift, xst, (int)dx, (int)dy, true);
                                EMData *uwc = uw->copy();
                                EMData *a = uw->calc_ccfx(to_unwrapped);

                                uwc->rotate_x(a->calc_max_index());
                                float cm = uwc->cmp(cmp_name, to_unwrapped_copy, cmp_params);

                                if (cm < bestval) {
                                        bestval = cm;
                                        bestang = (float)(2.0 * M_PI * a->calc_max_index() / a->get_xsize());
                                        bestdx = dx;
                                        bestdy = dy;
                                        bestflip = 0;
                                }

                                if( a )
                                {
                                        delete a;
                                        a = 0;
                                }
                                if( uw )
                                {
                                        delete uw;
                                        uw = 0;
                                }
                                if( uwc )
                                {
                                        delete uwc;
                                        uwc = 0;
                                }
                                uw = this_img->unwrap(4, this_img->get_ysize() / 2 - 2 - maxshift, xst, (int)dx, (int)dy, true);
                                uwc = uw->copy();
                                a = uw->calc_ccfx(to_flip_unwrapped);

                                uwc->rotate_x(a->calc_max_index());
                                cm = uwc->cmp(cmp_name, to_flip_unwrapped_copy, cmp_params);

                                if (cm < bestval) {
                                        bestval = cm;
                                        bestang = (float)(2.0 * M_PI * a->calc_max_index() / a->get_xsize());
                                        bestdx = dx;
                                        bestdy = dy;
                                        bestflip = 1;
                                }

                                if( a )
                                {
                                        delete a;
                                        a = 0;
                                }
                                if( uw )
                                {
                                        delete uw;
                                        uw = 0;
                                }
                                if( uwc )
                                {
                                        delete uwc;
                                        uwc = 0;
                                }
                        }
                }
        }
        if( to_unwrapped ) {delete to_unwrapped;to_unwrapped = 0;}
        if( to_shrunk_unwrapped ) {     delete to_shrunk_unwrapped;     to_shrunk_unwrapped = 0;}
        if (to_unwrapped_copy) { delete to_unwrapped_copy; to_unwrapped_copy = 0; }
        if (to_flip_unwrapped) { delete to_flip_unwrapped; to_flip_unwrapped = 0; }
        if (to_flip_unwrapped_copy) { delete to_flip_unwrapped_copy; to_flip_unwrapped_copy = 0;}

        bestang *= (float)EMConsts::rad2deg;
        Transform t(Dict("type","2d","alpha",(float)bestang));
        t.set_pre_trans(Vec2f(-bestdx,-bestdy));
        if (bestflip) {
                t.set_mirror(true);
        }

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

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

Implements EMAN::Aligner.

Definition at line 1087 of file aligner.h.

References align().

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

Implements EMAN::Aligner.

Definition at line 1097 of file aligner.h.

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

References NAME.

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

Implements EMAN::Aligner.

Definition at line 1107 of file aligner.h.

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

                {
                        TypeDict d;

                        d.put("flip", EMObject::EMDATA);
                        d.put("maxshift", EMObject::INT, "Maximum translation in pixels");
                        return d;
                }
static Aligner* EMAN::RTFExhaustiveAligner::NEW ( ) [inline, static]

Definition at line 1102 of file aligner.h.

                {
                        return new RTFExhaustiveAligner();
                }

Member Data Documentation

const string RTFExhaustiveAligner::NAME = "rtf_exhaustive" [static]

Definition at line 1116 of file aligner.h.

Referenced by get_name().


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