Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

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

AlignerNEW ()

Static Public Attributes

const string NAME = "rtf_exhaustive"

Detailed Description

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

slow

Parameters:
flip 
maxshift Maximum translation in pixels

Definition at line 770 of file aligner.h.


Member Function Documentation

virtual EMData* EMAN::RTFExhaustiveAligner::align EMData this_img,
EMData to_img
const [inline, virtual]
 

Implements EMAN::Aligner.

Definition at line 775 of file aligner.h.

References align().

00776                 {
00777                         return align(this_img, to_img, "sqeuclidean", Dict());
00778                 }

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_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 905 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::EMData::process(), EMAN::EMData::rotate_x(), EMAN::EMData::set_attr(), EMAN::Dict::set_default(), EMAN::Transform::set_mirror(), EMAN::Transform::set_pre_trans(), t, EMAN::EMData::unwrap(), and EMAN::Vec2f.

00907 {
00908         EMData *flip = params.set_default("flip", (EMData *) 0);
00909         int maxshift = params.set_default("maxshift", this_img->get_xsize()/8);
00910         if (maxshift < 2) throw InvalidParameterException("maxshift must be greater than or equal to 2");
00911 
00912         int ny = this_img->get_ysize();
00913         int xst = (int) floor(2 * M_PI * ny);
00914         xst = Util::calc_best_fft_size(xst);
00915 
00916         Dict d("n",2);
00917         EMData *to_shrunk_unwrapped = to->process("math.medianshrink",d);
00918 
00919         int to_copy_r2 = to_shrunk_unwrapped->get_ysize() / 2 - 2 - maxshift / 2;
00920         EMData *tmp = to_shrunk_unwrapped->unwrap(4, to_copy_r2, xst / 2, 0, 0, true);
00921         if( to_shrunk_unwrapped )
00922         {
00923                 delete to_shrunk_unwrapped;
00924                 to_shrunk_unwrapped = 0;
00925         }
00926         to_shrunk_unwrapped = tmp;
00927 
00928         EMData *to_shrunk_unwrapped_copy = to_shrunk_unwrapped->copy();
00929         EMData* to_unwrapped = to->unwrap(4, to->get_ysize() / 2 - 2 - maxshift, xst, 0, 0, true);
00930         EMData *to_unwrapped_copy = to_unwrapped->copy();
00931 
00932         bool delete_flipped = true;
00933         EMData *flipped = 0;
00934         if (flip) {
00935                 delete_flipped = false;
00936                 flipped = flip;
00937         }
00938         else {
00939                 flipped = to->process("xform.flip", Dict("axis", "x"));
00940         }
00941         EMData *to_shrunk_flipped_unwrapped = flipped->process("math.medianshrink",d);
00942         tmp = to_shrunk_flipped_unwrapped->unwrap(4, to_copy_r2, xst / 2, 0, 0, true);
00943         if( to_shrunk_flipped_unwrapped )
00944         {
00945                 delete to_shrunk_flipped_unwrapped;
00946                 to_shrunk_flipped_unwrapped = 0;
00947         }
00948         to_shrunk_flipped_unwrapped = tmp;
00949         EMData *to_shrunk_flipped_unwrapped_copy = to_shrunk_flipped_unwrapped->copy();
00950         EMData* to_flip_unwrapped = flipped->unwrap(4, to->get_ysize() / 2 - 2 - maxshift, xst, 0, 0, true);
00951         EMData* to_flip_unwrapped_copy = to_flip_unwrapped->copy();
00952 
00953         if (delete_flipped && flipped != 0) {
00954                 delete flipped;
00955                 flipped = 0;
00956         }
00957 
00958         EMData *this_shrunk_2 = this_img->process("math.medianshrink",d);
00959 
00960         float bestval = FLT_MAX;
00961         float bestang = 0;
00962         int bestflip = 0;
00963         float bestdx = 0;
00964         float bestdy = 0;
00965 
00966         int half_maxshift = maxshift / 2;
00967 
00968         int ur2 = this_shrunk_2->get_ysize() / 2 - 2 - half_maxshift;
00969         for (int dy = -half_maxshift; dy <= half_maxshift; dy += 1) {
00970                 for (int dx = -half_maxshift; dx <= half_maxshift; dx += 1) {
00971 #ifdef  _WIN32
00972                         if (_hypot(dx, dy) <= half_maxshift) {
00973 #else
00974                         if (hypot(dx, dy) <= half_maxshift) {
00975 #endif
00976                                 EMData *uw = this_shrunk_2->unwrap(4, ur2, xst / 2, dx, dy, true);
00977                                 EMData *uwc = uw->copy();
00978                                 EMData *a = uw->calc_ccfx(to_shrunk_unwrapped);
00979 
00980                                 uwc->rotate_x(a->calc_max_index());
00981                                 float cm = uwc->cmp(cmp_name, to_shrunk_unwrapped_copy, cmp_params);
00982                                 if (cm < bestval) {
00983                                         bestval = cm;
00984                                         bestang = (float) (2.0 * M_PI * a->calc_max_index() / a->get_xsize());
00985                                         bestdx = (float)dx;
00986                                         bestdy = (float)dy;
00987                                         bestflip = 0;
00988                                 }
00989 
00990 
00991                                 if( a )
00992                                 {
00993                                         delete a;
00994                                         a = 0;
00995                                 }
00996                                 if( uw )
00997                                 {
00998                                         delete uw;
00999                                         uw = 0;
01000                                 }
01001                                 if( uwc )
01002                                 {
01003                                         delete uwc;
01004                                         uwc = 0;
01005                                 }
01006                                 uw = this_shrunk_2->unwrap(4, ur2, xst / 2, dx, dy, true);
01007                                 uwc = uw->copy();
01008                                 a = uw->calc_ccfx(to_shrunk_flipped_unwrapped);
01009 
01010                                 uwc->rotate_x(a->calc_max_index());
01011                                 cm = uwc->cmp(cmp_name, to_shrunk_flipped_unwrapped_copy, cmp_params);
01012                                 if (cm < bestval) {
01013                                         bestval = cm;
01014                                         bestang = (float) (2.0 * M_PI * a->calc_max_index() / a->get_xsize());
01015                                         bestdx = (float)dx;
01016                                         bestdy = (float)dy;
01017                                         bestflip = 1;
01018                                 }
01019 
01020                                 if( a )
01021                                 {
01022                                         delete a;
01023                                         a = 0;
01024                                 }
01025 
01026                                 if( uw )
01027                                 {
01028                                         delete uw;
01029                                         uw = 0;
01030                                 }
01031                                 if( uwc )
01032                                 {
01033                                         delete uwc;
01034                                         uwc = 0;
01035                                 }
01036                         }
01037                 }
01038         }
01039         if( this_shrunk_2 )
01040         {
01041                 delete this_shrunk_2;
01042                 this_shrunk_2 = 0;
01043         }
01044         if( to_shrunk_unwrapped )
01045         {
01046                 delete to_shrunk_unwrapped;
01047                 to_shrunk_unwrapped = 0;
01048         }
01049         if( to_shrunk_unwrapped_copy )
01050         {
01051                 delete to_shrunk_unwrapped_copy;
01052                 to_shrunk_unwrapped_copy = 0;
01053         }
01054         if( to_shrunk_flipped_unwrapped )
01055         {
01056                 delete to_shrunk_flipped_unwrapped;
01057                 to_shrunk_flipped_unwrapped = 0;
01058         }
01059         if( to_shrunk_flipped_unwrapped_copy )
01060         {
01061                 delete to_shrunk_flipped_unwrapped_copy;
01062                 to_shrunk_flipped_unwrapped_copy = 0;
01063         }
01064         bestdx *= 2;
01065         bestdy *= 2;
01066         bestval = FLT_MAX;
01067 
01068         float bestdx2 = bestdx;
01069         float bestdy2 = bestdy;
01070         // Note I tried steps less than 1.0 (sub pixel precision) and it actually appeared detrimental
01071         // So my advice is to stick with dx += 1.0 etc unless you really are looking to fine tune this
01072         // algorithm
01073         for (float dy = bestdy2 - 3; dy <= bestdy2 + 3; dy += 1.0 ) {
01074                 for (float dx = bestdx2 - 3; dx <= bestdx2 + 3; dx += 1.0 ) {
01075 
01076 #ifdef  _WIN32
01077                         if (_hypot(dx, dy) <= maxshift) {
01078 #else
01079                         if (hypot(dx, dy) <= maxshift) {
01080 #endif
01081                                 EMData *uw = this_img->unwrap(4, this_img->get_ysize() / 2 - 2 - maxshift, xst, (int)dx, (int)dy, true);
01082                                 EMData *uwc = uw->copy();
01083                                 EMData *a = uw->calc_ccfx(to_unwrapped);
01084 
01085                                 uwc->rotate_x(a->calc_max_index());
01086                                 float cm = uwc->cmp(cmp_name, to_unwrapped_copy, cmp_params);
01087 
01088                                 if (cm < bestval) {
01089                                         bestval = cm;
01090                                         bestang = (float)(2.0 * M_PI * a->calc_max_index() / a->get_xsize());
01091                                         bestdx = dx;
01092                                         bestdy = dy;
01093                                         bestflip = 0;
01094                                 }
01095 
01096                                 if( a )
01097                                 {
01098                                         delete a;
01099                                         a = 0;
01100                                 }
01101                                 if( uw )
01102                                 {
01103                                         delete uw;
01104                                         uw = 0;
01105                                 }
01106                                 if( uwc )
01107                                 {
01108                                         delete uwc;
01109                                         uwc = 0;
01110                                 }
01111                                 uw = this_img->unwrap(4, this_img->get_ysize() / 2 - 2 - maxshift, xst, (int)dx, (int)dy, true);
01112                                 uwc = uw->copy();
01113                                 a = uw->calc_ccfx(to_flip_unwrapped);
01114 
01115                                 uwc->rotate_x(a->calc_max_index());
01116                                 cm = uwc->cmp(cmp_name, to_flip_unwrapped_copy, cmp_params);
01117 
01118                                 if (cm < bestval) {
01119                                         bestval = cm;
01120                                         bestang = (float)(2.0 * M_PI * a->calc_max_index() / a->get_xsize());
01121                                         bestdx = dx;
01122                                         bestdy = dy;
01123                                         bestflip = 1;
01124                                 }
01125 
01126                                 if( a )
01127                                 {
01128                                         delete a;
01129                                         a = 0;
01130                                 }
01131                                 if( uw )
01132                                 {
01133                                         delete uw;
01134                                         uw = 0;
01135                                 }
01136                                 if( uwc )
01137                                 {
01138                                         delete uwc;
01139                                         uwc = 0;
01140                                 }
01141                         }
01142                 }
01143         }
01144         if( to_unwrapped ) {delete to_unwrapped;to_unwrapped = 0;}
01145         if( to_shrunk_unwrapped ) {     delete to_shrunk_unwrapped;     to_shrunk_unwrapped = 0;}
01146         if (to_unwrapped_copy) { delete to_unwrapped_copy; to_unwrapped_copy = 0; }
01147         if (to_flip_unwrapped) { delete to_flip_unwrapped; to_flip_unwrapped = 0; }
01148         if (to_flip_unwrapped_copy) { delete to_flip_unwrapped_copy; to_flip_unwrapped_copy = 0;}
01149 
01150         bestang *= (float)EMConsts::rad2deg;
01151         Transform t(Dict("type","2d","alpha",(float)bestang));
01152         t.set_pre_trans(Vec2f(-bestdx,-bestdy));
01153         if (bestflip) {
01154                 t.set_mirror(true);
01155         }
01156 
01157         EMData* ret = this_img->process("xform",Dict("transform",&t));
01158         ret->set_attr("xform.align2d",&t);
01159 
01160         return ret;
01161 }

virtual string EMAN::RTFExhaustiveAligner::get_desc  )  const [inline, virtual]
 

Implements EMAN::Aligner.

Definition at line 785 of file aligner.h.

00786                 {
00787                         return "Experimental full 2D alignment with handedness check using semi-exhaustive search (not necessarily better than RTFBest)";
00788                 }

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

00781                 {
00782                         return NAME;
00783                 }

virtual TypeDict EMAN::RTFExhaustiveAligner::get_param_types  )  const [inline, virtual]
 

Implements EMAN::Aligner.

Definition at line 795 of file aligner.h.

References EMAN::TypeDict::put().

00796                 {
00797                         TypeDict d;
00798 
00799                         d.put("flip", EMObject::EMDATA);
00800                         d.put("maxshift", EMObject::INT, "Maximum translation in pixels");
00801                         return d;
00802                 }

Aligner* EMAN::RTFExhaustiveAligner::NEW  )  [inline, static]
 

Definition at line 790 of file aligner.h.

00791                 {
00792                         return new RTFExhaustiveAligner();
00793                 }


Member Data Documentation

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

Definition at line 71 of file aligner.cpp.


The documentation for this class was generated from the following files:
Generated on Tue Jul 12 13:51:06 2011 for EMAN2 by  doxygen 1.3.9.1