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

References align().

00673                 {
00674                         return align(this_img, to_img, "sqeuclidean", Dict());
00675                 }

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 709 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.

00711 {
00712         EMData *flip = params.set_default("flip", (EMData *) 0);
00713         int maxshift = params.set_default("maxshift", this_img->get_xsize()/8);
00714         if (maxshift < 2) throw InvalidParameterException("maxshift must be greater than or equal to 2");
00715 
00716         int ny = this_img->get_ysize();
00717         int xst = (int) floor(2 * M_PI * ny);
00718         xst = Util::calc_best_fft_size(xst);
00719 
00720         Dict d("n",2);
00721         EMData *to_shrunk_unwrapped = to->process("math.medianshrink",d);
00722 
00723         int to_copy_r2 = to_shrunk_unwrapped->get_ysize() / 2 - 2 - maxshift / 2;
00724         EMData *tmp = to_shrunk_unwrapped->unwrap(4, to_copy_r2, xst / 2, 0, 0, true);
00725         if( to_shrunk_unwrapped )
00726         {
00727                 delete to_shrunk_unwrapped;
00728                 to_shrunk_unwrapped = 0;
00729         }
00730         to_shrunk_unwrapped = tmp;
00731 
00732         EMData *to_shrunk_unwrapped_copy = to_shrunk_unwrapped->copy();
00733         EMData* to_unwrapped = to->unwrap(4, to->get_ysize() / 2 - 2 - maxshift, xst, 0, 0, true);
00734         EMData *to_unwrapped_copy = to_unwrapped->copy();
00735 
00736         bool delete_flipped = true;
00737         EMData *flipped = 0;
00738         if (flip) {
00739                 delete_flipped = false;
00740                 flipped = flip;
00741         }
00742         else {
00743                 flipped = to->process("xform.flip", Dict("axis", "x"));
00744         }
00745         EMData *to_shrunk_flipped_unwrapped = flipped->process("math.medianshrink",d);
00746         tmp = to_shrunk_flipped_unwrapped->unwrap(4, to_copy_r2, xst / 2, 0, 0, true);
00747         if( to_shrunk_flipped_unwrapped )
00748         {
00749                 delete to_shrunk_flipped_unwrapped;
00750                 to_shrunk_flipped_unwrapped = 0;
00751         }
00752         to_shrunk_flipped_unwrapped = tmp;
00753         EMData *to_shrunk_flipped_unwrapped_copy = to_shrunk_flipped_unwrapped->copy();
00754         EMData* to_flip_unwrapped = flipped->unwrap(4, to->get_ysize() / 2 - 2 - maxshift, xst, 0, 0, true);
00755         EMData* to_flip_unwrapped_copy = to_flip_unwrapped->copy();
00756 
00757         if (delete_flipped && flipped != 0) {
00758                 delete flipped;
00759                 flipped = 0;
00760         }
00761 
00762         EMData *this_shrunk_2 = this_img->process("math.medianshrink",d);
00763 
00764         float bestval = FLT_MAX;
00765         float bestang = 0;
00766         int bestflip = 0;
00767         float bestdx = 0;
00768         float bestdy = 0;
00769 
00770         int half_maxshift = maxshift / 2;
00771 
00772         int ur2 = this_shrunk_2->get_ysize() / 2 - 2 - half_maxshift;
00773         for (int dy = -half_maxshift; dy <= half_maxshift; dy += 1) {
00774                 for (int dx = -half_maxshift; dx <= half_maxshift; dx += 1) {
00775 #ifdef  _WIN32
00776                         if (_hypot(dx, dy) <= half_maxshift) {
00777 #else
00778                         if (hypot(dx, dy) <= half_maxshift) {
00779 #endif
00780                                 EMData *uw = this_shrunk_2->unwrap(4, ur2, xst / 2, dx, dy, true);
00781                                 EMData *uwc = uw->copy();
00782                                 EMData *a = uw->calc_ccfx(to_shrunk_unwrapped);
00783 
00784                                 uwc->rotate_x(a->calc_max_index());
00785                                 float cm = uwc->cmp(cmp_name, to_shrunk_unwrapped_copy, cmp_params);
00786                                 if (cm < bestval) {
00787                                         bestval = cm;
00788                                         bestang = (float) (2.0 * M_PI * a->calc_max_index() / a->get_xsize());
00789                                         bestdx = (float)dx;
00790                                         bestdy = (float)dy;
00791                                         bestflip = 0;
00792                                 }
00793 
00794 
00795                                 if( a )
00796                                 {
00797                                         delete a;
00798                                         a = 0;
00799                                 }
00800                                 if( uw )
00801                                 {
00802                                         delete uw;
00803                                         uw = 0;
00804                                 }
00805                                 if( uwc )
00806                                 {
00807                                         delete uwc;
00808                                         uwc = 0;
00809                                 }
00810                                 uw = this_shrunk_2->unwrap(4, ur2, xst / 2, dx, dy, true);
00811                                 uwc = uw->copy();
00812                                 a = uw->calc_ccfx(to_shrunk_flipped_unwrapped);
00813 
00814                                 uwc->rotate_x(a->calc_max_index());
00815                                 cm = uwc->cmp(cmp_name, to_shrunk_flipped_unwrapped_copy, cmp_params);
00816                                 if (cm < bestval) {
00817                                         bestval = cm;
00818                                         bestang = (float) (2.0 * M_PI * a->calc_max_index() / a->get_xsize());
00819                                         bestdx = (float)dx;
00820                                         bestdy = (float)dy;
00821                                         bestflip = 1;
00822                                 }
00823 
00824                                 if( a )
00825                                 {
00826                                         delete a;
00827                                         a = 0;
00828                                 }
00829 
00830                                 if( uw )
00831                                 {
00832                                         delete uw;
00833                                         uw = 0;
00834                                 }
00835                                 if( uwc )
00836                                 {
00837                                         delete uwc;
00838                                         uwc = 0;
00839                                 }
00840                         }
00841                 }
00842         }
00843         if( this_shrunk_2 )
00844         {
00845                 delete this_shrunk_2;
00846                 this_shrunk_2 = 0;
00847         }
00848         if( to_shrunk_unwrapped )
00849         {
00850                 delete to_shrunk_unwrapped;
00851                 to_shrunk_unwrapped = 0;
00852         }
00853         if( to_shrunk_unwrapped_copy )
00854         {
00855                 delete to_shrunk_unwrapped_copy;
00856                 to_shrunk_unwrapped_copy = 0;
00857         }
00858         if( to_shrunk_flipped_unwrapped )
00859         {
00860                 delete to_shrunk_flipped_unwrapped;
00861                 to_shrunk_flipped_unwrapped = 0;
00862         }
00863         if( to_shrunk_flipped_unwrapped_copy )
00864         {
00865                 delete to_shrunk_flipped_unwrapped_copy;
00866                 to_shrunk_flipped_unwrapped_copy = 0;
00867         }
00868         bestdx *= 2;
00869         bestdy *= 2;
00870         bestval = FLT_MAX;
00871 
00872         float bestdx2 = bestdx;
00873         float bestdy2 = bestdy;
00874         // Note I tried steps less than 1.0 (sub pixel precision) and it actually appeared detrimental
00875         // So my advice is to stick with dx += 1.0 etc unless you really are looking to fine tune this
00876         // algorithm
00877         for (float dy = bestdy2 - 3; dy <= bestdy2 + 3; dy += 1.0 ) {
00878                 for (float dx = bestdx2 - 3; dx <= bestdx2 + 3; dx += 1.0 ) {
00879 
00880 #ifdef  _WIN32
00881                         if (_hypot(dx, dy) <= maxshift) {
00882 #else
00883                         if (hypot(dx, dy) <= maxshift) {
00884 #endif
00885                                 EMData *uw = this_img->unwrap(4, this_img->get_ysize() / 2 - 2 - maxshift, xst, (int)dx, (int)dy, true);
00886                                 EMData *uwc = uw->copy();
00887                                 EMData *a = uw->calc_ccfx(to_unwrapped);
00888 
00889                                 uwc->rotate_x(a->calc_max_index());
00890                                 float cm = uwc->cmp(cmp_name, to_unwrapped_copy, cmp_params);
00891 
00892                                 if (cm < bestval) {
00893                                         bestval = cm;
00894                                         bestang = (float)(2.0 * M_PI * a->calc_max_index() / a->get_xsize());
00895                                         bestdx = dx;
00896                                         bestdy = dy;
00897                                         bestflip = 0;
00898                                 }
00899 
00900                                 if( a )
00901                                 {
00902                                         delete a;
00903                                         a = 0;
00904                                 }
00905                                 if( uw )
00906                                 {
00907                                         delete uw;
00908                                         uw = 0;
00909                                 }
00910                                 if( uwc )
00911                                 {
00912                                         delete uwc;
00913                                         uwc = 0;
00914                                 }
00915                                 uw = this_img->unwrap(4, this_img->get_ysize() / 2 - 2 - maxshift, xst, (int)dx, (int)dy, true);
00916                                 uwc = uw->copy();
00917                                 a = uw->calc_ccfx(to_flip_unwrapped);
00918 
00919                                 uwc->rotate_x(a->calc_max_index());
00920                                 cm = uwc->cmp(cmp_name, to_flip_unwrapped_copy, cmp_params);
00921 
00922                                 if (cm < bestval) {
00923                                         bestval = cm;
00924                                         bestang = (float)(2.0 * M_PI * a->calc_max_index() / a->get_xsize());
00925                                         bestdx = dx;
00926                                         bestdy = dy;
00927                                         bestflip = 1;
00928                                 }
00929 
00930                                 if( a )
00931                                 {
00932                                         delete a;
00933                                         a = 0;
00934                                 }
00935                                 if( uw )
00936                                 {
00937                                         delete uw;
00938                                         uw = 0;
00939                                 }
00940                                 if( uwc )
00941                                 {
00942                                         delete uwc;
00943                                         uwc = 0;
00944                                 }
00945                         }
00946                 }
00947         }
00948         if( to_unwrapped ) {delete to_unwrapped;to_unwrapped = 0;}
00949         if( to_shrunk_unwrapped ) {     delete to_shrunk_unwrapped;     to_shrunk_unwrapped = 0;}
00950         if (to_unwrapped_copy) { delete to_unwrapped_copy; to_unwrapped_copy = 0; }
00951         if (to_flip_unwrapped) { delete to_flip_unwrapped; to_flip_unwrapped = 0; }
00952         if (to_flip_unwrapped_copy) { delete to_flip_unwrapped_copy; to_flip_unwrapped_copy = 0;}
00953 
00954         bestang *= (float)EMConsts::rad2deg;
00955         Transform t(Dict("type","2d","alpha",(float)bestang));
00956         t.set_pre_trans(Vec2f(-bestdx,-bestdy));
00957         if (bestflip) {
00958                 t.set_mirror(true);
00959         }
00960 
00961         EMData* ret = this_img->process("xform",Dict("transform",&t));
00962         ret->set_attr("xform.align2d",&t);
00963 
00964         return ret;
00965 }

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

Implements EMAN::Aligner.

Definition at line 682 of file aligner.h.

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

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

00678                 {
00679                         return NAME;
00680                 }

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

Implements EMAN::Aligner.

Definition at line 692 of file aligner.h.

References EMAN::TypeDict::put().

00693                 {
00694                         TypeDict d;
00695 
00696                         d.put("flip", EMObject::EMDATA);
00697                         d.put("maxshift", EMObject::INT, "Maximum translation in pixels");
00698                         return d;
00699                 }

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

Definition at line 687 of file aligner.h.

00688                 {
00689                         return new RTFExhaustiveAligner();
00690                 }


Member Data Documentation

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

Definition at line 64 of file aligner.cpp.


The documentation for this class was generated from the following files:
Generated on Thu Dec 9 13:47:10 2010 for EMAN2 by  doxygen 1.3.9.1