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