#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 1076 of file aligner.h.
virtual EMData* EMAN::RTFExhaustiveAligner::align | ( | EMData * | this_img, | |
EMData * | to_img | |||
) | const [inline, virtual] |
Implements EMAN::Aligner.
Definition at line 1081 of file aligner.h.
References align().
01082 { 01083 return align(this_img, to_img, "sqeuclidean", Dict()); 01084 }
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 1097 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().
01099 { 01100 EMData *flip = params.set_default("flip", (EMData *) 0); 01101 int maxshift = params.set_default("maxshift", this_img->get_xsize()/8); 01102 if (maxshift < 2) throw InvalidParameterException("maxshift must be greater than or equal to 2"); 01103 01104 int ny = this_img->get_ysize(); 01105 int xst = (int) floor(2 * M_PI * ny); 01106 xst = Util::calc_best_fft_size(xst); 01107 01108 Dict d("n",2); 01109 EMData *to_shrunk_unwrapped = to->process("math.medianshrink",d); 01110 01111 int to_copy_r2 = to_shrunk_unwrapped->get_ysize() / 2 - 2 - maxshift / 2; 01112 EMData *tmp = to_shrunk_unwrapped->unwrap(4, to_copy_r2, xst / 2, 0, 0, true); 01113 if( to_shrunk_unwrapped ) 01114 { 01115 delete to_shrunk_unwrapped; 01116 to_shrunk_unwrapped = 0; 01117 } 01118 to_shrunk_unwrapped = tmp; 01119 01120 EMData *to_shrunk_unwrapped_copy = to_shrunk_unwrapped->copy(); 01121 EMData* to_unwrapped = to->unwrap(4, to->get_ysize() / 2 - 2 - maxshift, xst, 0, 0, true); 01122 EMData *to_unwrapped_copy = to_unwrapped->copy(); 01123 01124 bool delete_flipped = true; 01125 EMData *flipped = 0; 01126 if (flip) { 01127 delete_flipped = false; 01128 flipped = flip; 01129 } 01130 else { 01131 flipped = to->process("xform.flip", Dict("axis", "x")); 01132 } 01133 EMData *to_shrunk_flipped_unwrapped = flipped->process("math.medianshrink",d); 01134 tmp = to_shrunk_flipped_unwrapped->unwrap(4, to_copy_r2, xst / 2, 0, 0, true); 01135 if( to_shrunk_flipped_unwrapped ) 01136 { 01137 delete to_shrunk_flipped_unwrapped; 01138 to_shrunk_flipped_unwrapped = 0; 01139 } 01140 to_shrunk_flipped_unwrapped = tmp; 01141 EMData *to_shrunk_flipped_unwrapped_copy = to_shrunk_flipped_unwrapped->copy(); 01142 EMData* to_flip_unwrapped = flipped->unwrap(4, to->get_ysize() / 2 - 2 - maxshift, xst, 0, 0, true); 01143 EMData* to_flip_unwrapped_copy = to_flip_unwrapped->copy(); 01144 01145 if (delete_flipped && flipped != 0) { 01146 delete flipped; 01147 flipped = 0; 01148 } 01149 01150 EMData *this_shrunk_2 = this_img->process("math.medianshrink",d); 01151 01152 float bestval = FLT_MAX; 01153 float bestang = 0; 01154 int bestflip = 0; 01155 float bestdx = 0; 01156 float bestdy = 0; 01157 01158 int half_maxshift = maxshift / 2; 01159 01160 int ur2 = this_shrunk_2->get_ysize() / 2 - 2 - half_maxshift; 01161 for (int dy = -half_maxshift; dy <= half_maxshift; dy += 1) { 01162 for (int dx = -half_maxshift; dx <= half_maxshift; dx += 1) { 01163 #ifdef _WIN32 01164 if (_hypot(dx, dy) <= half_maxshift) { 01165 #else 01166 if (hypot(dx, dy) <= half_maxshift) { 01167 #endif 01168 EMData *uw = this_shrunk_2->unwrap(4, ur2, xst / 2, dx, dy, true); 01169 EMData *uwc = uw->copy(); 01170 EMData *a = uw->calc_ccfx(to_shrunk_unwrapped); 01171 01172 uwc->rotate_x(a->calc_max_index()); 01173 float cm = uwc->cmp(cmp_name, to_shrunk_unwrapped_copy, cmp_params); 01174 if (cm < bestval) { 01175 bestval = cm; 01176 bestang = (float) (2.0 * M_PI * a->calc_max_index() / a->get_xsize()); 01177 bestdx = (float)dx; 01178 bestdy = (float)dy; 01179 bestflip = 0; 01180 } 01181 01182 01183 if( a ) 01184 { 01185 delete a; 01186 a = 0; 01187 } 01188 if( uw ) 01189 { 01190 delete uw; 01191 uw = 0; 01192 } 01193 if( uwc ) 01194 { 01195 delete uwc; 01196 uwc = 0; 01197 } 01198 uw = this_shrunk_2->unwrap(4, ur2, xst / 2, dx, dy, true); 01199 uwc = uw->copy(); 01200 a = uw->calc_ccfx(to_shrunk_flipped_unwrapped); 01201 01202 uwc->rotate_x(a->calc_max_index()); 01203 cm = uwc->cmp(cmp_name, to_shrunk_flipped_unwrapped_copy, cmp_params); 01204 if (cm < bestval) { 01205 bestval = cm; 01206 bestang = (float) (2.0 * M_PI * a->calc_max_index() / a->get_xsize()); 01207 bestdx = (float)dx; 01208 bestdy = (float)dy; 01209 bestflip = 1; 01210 } 01211 01212 if( a ) 01213 { 01214 delete a; 01215 a = 0; 01216 } 01217 01218 if( uw ) 01219 { 01220 delete uw; 01221 uw = 0; 01222 } 01223 if( uwc ) 01224 { 01225 delete uwc; 01226 uwc = 0; 01227 } 01228 } 01229 } 01230 } 01231 if( this_shrunk_2 ) 01232 { 01233 delete this_shrunk_2; 01234 this_shrunk_2 = 0; 01235 } 01236 if( to_shrunk_unwrapped ) 01237 { 01238 delete to_shrunk_unwrapped; 01239 to_shrunk_unwrapped = 0; 01240 } 01241 if( to_shrunk_unwrapped_copy ) 01242 { 01243 delete to_shrunk_unwrapped_copy; 01244 to_shrunk_unwrapped_copy = 0; 01245 } 01246 if( to_shrunk_flipped_unwrapped ) 01247 { 01248 delete to_shrunk_flipped_unwrapped; 01249 to_shrunk_flipped_unwrapped = 0; 01250 } 01251 if( to_shrunk_flipped_unwrapped_copy ) 01252 { 01253 delete to_shrunk_flipped_unwrapped_copy; 01254 to_shrunk_flipped_unwrapped_copy = 0; 01255 } 01256 bestdx *= 2; 01257 bestdy *= 2; 01258 bestval = FLT_MAX; 01259 01260 float bestdx2 = bestdx; 01261 float bestdy2 = bestdy; 01262 // Note I tried steps less than 1.0 (sub pixel precision) and it actually appeared detrimental 01263 // So my advice is to stick with dx += 1.0 etc unless you really are looking to fine tune this 01264 // algorithm 01265 for (float dy = bestdy2 - 3; dy <= bestdy2 + 3; dy += 1.0 ) { 01266 for (float dx = bestdx2 - 3; dx <= bestdx2 + 3; dx += 1.0 ) { 01267 01268 #ifdef _WIN32 01269 if (_hypot(dx, dy) <= maxshift) { 01270 #else 01271 if (hypot(dx, dy) <= maxshift) { 01272 #endif 01273 EMData *uw = this_img->unwrap(4, this_img->get_ysize() / 2 - 2 - maxshift, xst, (int)dx, (int)dy, true); 01274 EMData *uwc = uw->copy(); 01275 EMData *a = uw->calc_ccfx(to_unwrapped); 01276 01277 uwc->rotate_x(a->calc_max_index()); 01278 float cm = uwc->cmp(cmp_name, to_unwrapped_copy, cmp_params); 01279 01280 if (cm < bestval) { 01281 bestval = cm; 01282 bestang = (float)(2.0 * M_PI * a->calc_max_index() / a->get_xsize()); 01283 bestdx = dx; 01284 bestdy = dy; 01285 bestflip = 0; 01286 } 01287 01288 if( a ) 01289 { 01290 delete a; 01291 a = 0; 01292 } 01293 if( uw ) 01294 { 01295 delete uw; 01296 uw = 0; 01297 } 01298 if( uwc ) 01299 { 01300 delete uwc; 01301 uwc = 0; 01302 } 01303 uw = this_img->unwrap(4, this_img->get_ysize() / 2 - 2 - maxshift, xst, (int)dx, (int)dy, true); 01304 uwc = uw->copy(); 01305 a = uw->calc_ccfx(to_flip_unwrapped); 01306 01307 uwc->rotate_x(a->calc_max_index()); 01308 cm = uwc->cmp(cmp_name, to_flip_unwrapped_copy, cmp_params); 01309 01310 if (cm < bestval) { 01311 bestval = cm; 01312 bestang = (float)(2.0 * M_PI * a->calc_max_index() / a->get_xsize()); 01313 bestdx = dx; 01314 bestdy = dy; 01315 bestflip = 1; 01316 } 01317 01318 if( a ) 01319 { 01320 delete a; 01321 a = 0; 01322 } 01323 if( uw ) 01324 { 01325 delete uw; 01326 uw = 0; 01327 } 01328 if( uwc ) 01329 { 01330 delete uwc; 01331 uwc = 0; 01332 } 01333 } 01334 } 01335 } 01336 if( to_unwrapped ) {delete to_unwrapped;to_unwrapped = 0;} 01337 if( to_shrunk_unwrapped ) { delete to_shrunk_unwrapped; to_shrunk_unwrapped = 0;} 01338 if (to_unwrapped_copy) { delete to_unwrapped_copy; to_unwrapped_copy = 0; } 01339 if (to_flip_unwrapped) { delete to_flip_unwrapped; to_flip_unwrapped = 0; } 01340 if (to_flip_unwrapped_copy) { delete to_flip_unwrapped_copy; to_flip_unwrapped_copy = 0;} 01341 01342 bestang *= (float)EMConsts::rad2deg; 01343 Transform t(Dict("type","2d","alpha",(float)bestang)); 01344 t.set_pre_trans(Vec2f(-bestdx,-bestdy)); 01345 if (bestflip) { 01346 t.set_mirror(true); 01347 } 01348 01349 EMData* ret = this_img->process("xform",Dict("transform",&t)); 01350 ret->set_attr("xform.align2d",&t); 01351 01352 return ret; 01353 }
virtual string EMAN::RTFExhaustiveAligner::get_desc | ( | ) | const [inline, virtual] |
Implements EMAN::Aligner.
Definition at line 1091 of file aligner.h.
01092 { 01093 return "Experimental full 2D alignment with handedness check using semi-exhaustive search (not necessarily better than RTFBest)"; 01094 }
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 1101 of file aligner.h.
References EMAN::EMObject::EMDATA, EMAN::EMObject::INT, and EMAN::TypeDict::put().
01102 { 01103 TypeDict d; 01104 01105 d.put("flip", EMObject::EMDATA); 01106 d.put("maxshift", EMObject::INT, "Maximum translation in pixels"); 01107 return d; 01108 }
static Aligner* EMAN::RTFExhaustiveAligner::NEW | ( | ) | [inline, static] |
const string RTFExhaustiveAligner::NAME = "rtf_exhaustive" [static] |