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