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