#include <aligner.h>


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 () |
slow
| flip | ||
| maxshift | Maximum translation in pixels |
Definition at line 465 of file aligner.h.
| 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 506 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(), EMAN::Transform::set_mirror(), EMAN::Transform::set_pre_trans(), t, and EMAN::EMData::unwrap().
Referenced by align().
00508 { 00509 EMData *flip = params.set_default("flip", (EMData *) 0); 00510 int maxshift = params.set_default("maxshift", this_img->get_xsize()/8); 00511 if (maxshift < 2) throw InvalidParameterException("maxshift must be greater than or equal to 2"); 00512 00513 int ny = this_img->get_ysize(); 00514 int xst = (int) floor(2 * M_PI * ny); 00515 xst = Util::calc_best_fft_size(xst); 00516 00517 Dict d("n",2); 00518 EMData *to_shrunk_unwrapped = to->process("math.medianshrink",d); 00519 00520 int to_copy_r2 = to_shrunk_unwrapped->get_ysize() / 2 - 2 - maxshift / 2; 00521 EMData *tmp = to_shrunk_unwrapped->unwrap(4, to_copy_r2, xst / 2, 0, 0, true); 00522 if( to_shrunk_unwrapped ) 00523 { 00524 delete to_shrunk_unwrapped; 00525 to_shrunk_unwrapped = 0; 00526 } 00527 to_shrunk_unwrapped = tmp; 00528 00529 EMData *to_shrunk_unwrapped_copy = to_shrunk_unwrapped->copy(); 00530 EMData* to_unwrapped = to->unwrap(4, to->get_ysize() / 2 - 2 - maxshift, xst, 0, 0, true); 00531 EMData *to_unwrapped_copy = to_unwrapped->copy(); 00532 00533 bool delete_flipped = true; 00534 EMData *flipped = 0; 00535 if (flip) { 00536 delete_flipped = false; 00537 flipped = flip; 00538 } 00539 else { 00540 flipped = to->process("xform.flip", Dict("axis", "x")); 00541 } 00542 EMData *to_shrunk_flipped_unwrapped = flipped->process("math.medianshrink",d); 00543 tmp = to_shrunk_flipped_unwrapped->unwrap(4, to_copy_r2, xst / 2, 0, 0, true); 00544 if( to_shrunk_flipped_unwrapped ) 00545 { 00546 delete to_shrunk_flipped_unwrapped; 00547 to_shrunk_flipped_unwrapped = 0; 00548 } 00549 to_shrunk_flipped_unwrapped = tmp; 00550 EMData *to_shrunk_flipped_unwrapped_copy = to_shrunk_flipped_unwrapped->copy(); 00551 EMData* to_flip_unwrapped = flipped->unwrap(4, to->get_ysize() / 2 - 2 - maxshift, xst, 0, 0, true); 00552 EMData* to_flip_unwrapped_copy = to_flip_unwrapped->copy(); 00553 00554 if (delete_flipped && flipped != 0) { 00555 delete flipped; 00556 flipped = 0; 00557 } 00558 00559 EMData *this_shrunk_2 = this_img->process("math.medianshrink",d); 00560 00561 float bestval = FLT_MAX; 00562 float bestang = 0; 00563 int bestflip = 0; 00564 float bestdx = 0; 00565 float bestdy = 0; 00566 00567 int half_maxshift = maxshift / 2; 00568 00569 int ur2 = this_shrunk_2->get_ysize() / 2 - 2 - half_maxshift; 00570 for (int dy = -half_maxshift; dy <= half_maxshift; dy += 1) { 00571 for (int dx = -half_maxshift; dx <= half_maxshift; dx += 1) { 00572 #ifdef _WIN32 00573 if (_hypot(dx, dy) <= half_maxshift) { 00574 #else 00575 if (hypot(dx, dy) <= half_maxshift) { 00576 #endif 00577 EMData *uw = this_shrunk_2->unwrap(4, ur2, xst / 2, dx, dy, true); 00578 EMData *uwc = uw->copy(); 00579 EMData *a = uw->calc_ccfx(to_shrunk_unwrapped); 00580 00581 uwc->rotate_x(a->calc_max_index()); 00582 float cm = uwc->cmp(cmp_name, to_shrunk_unwrapped_copy, cmp_params); 00583 if (cm < bestval) { 00584 bestval = cm; 00585 bestang = (float) (2.0 * M_PI * a->calc_max_index() / a->get_xsize()); 00586 bestdx = (float)dx; 00587 bestdy = (float)dy; 00588 bestflip = 0; 00589 } 00590 00591 00592 if( a ) 00593 { 00594 delete a; 00595 a = 0; 00596 } 00597 if( uw ) 00598 { 00599 delete uw; 00600 uw = 0; 00601 } 00602 if( uwc ) 00603 { 00604 delete uwc; 00605 uwc = 0; 00606 } 00607 uw = this_shrunk_2->unwrap(4, ur2, xst / 2, dx, dy, true); 00608 uwc = uw->copy(); 00609 a = uw->calc_ccfx(to_shrunk_flipped_unwrapped); 00610 00611 uwc->rotate_x(a->calc_max_index()); 00612 cm = uwc->cmp(cmp_name, to_shrunk_flipped_unwrapped_copy, cmp_params); 00613 if (cm < bestval) { 00614 bestval = cm; 00615 bestang = (float) (2.0 * M_PI * a->calc_max_index() / a->get_xsize()); 00616 bestdx = (float)dx; 00617 bestdy = (float)dy; 00618 bestflip = 1; 00619 } 00620 00621 if( a ) 00622 { 00623 delete a; 00624 a = 0; 00625 } 00626 00627 if( uw ) 00628 { 00629 delete uw; 00630 uw = 0; 00631 } 00632 if( uwc ) 00633 { 00634 delete uwc; 00635 uwc = 0; 00636 } 00637 } 00638 } 00639 } 00640 if( this_shrunk_2 ) 00641 { 00642 delete this_shrunk_2; 00643 this_shrunk_2 = 0; 00644 } 00645 if( to_shrunk_unwrapped ) 00646 { 00647 delete to_shrunk_unwrapped; 00648 to_shrunk_unwrapped = 0; 00649 } 00650 if( to_shrunk_unwrapped_copy ) 00651 { 00652 delete to_shrunk_unwrapped_copy; 00653 to_shrunk_unwrapped_copy = 0; 00654 } 00655 if( to_shrunk_flipped_unwrapped ) 00656 { 00657 delete to_shrunk_flipped_unwrapped; 00658 to_shrunk_flipped_unwrapped = 0; 00659 } 00660 if( to_shrunk_flipped_unwrapped_copy ) 00661 { 00662 delete to_shrunk_flipped_unwrapped_copy; 00663 to_shrunk_flipped_unwrapped_copy = 0; 00664 } 00665 bestdx *= 2; 00666 bestdy *= 2; 00667 bestval = FLT_MAX; 00668 00669 float bestdx2 = bestdx; 00670 float bestdy2 = bestdy; 00671 // Note I tried steps less than 1.0 (sub pixel precision) and it actually appeared detrimental 00672 // So my advice is to stick with dx += 1.0 etc unless you really are looking to fine tune this 00673 // algorithm 00674 for (float dy = bestdy2 - 3; dy <= bestdy2 + 3; dy += 1.0 ) { 00675 for (float dx = bestdx2 - 3; dx <= bestdx2 + 3; dx += 1.0 ) { 00676 00677 #ifdef _WIN32 00678 if (_hypot(dx, dy) <= maxshift) { 00679 #else 00680 if (hypot(dx, dy) <= maxshift) { 00681 #endif 00682 EMData *uw = this_img->unwrap(4, this_img->get_ysize() / 2 - 2 - maxshift, xst, (int)dx, (int)dy, true); 00683 EMData *uwc = uw->copy(); 00684 EMData *a = uw->calc_ccfx(to_unwrapped); 00685 00686 uwc->rotate_x(a->calc_max_index()); 00687 float cm = uwc->cmp(cmp_name, to_unwrapped_copy, cmp_params); 00688 00689 if (cm < bestval) { 00690 bestval = cm; 00691 bestang = (float)(2.0 * M_PI * a->calc_max_index() / a->get_xsize()); 00692 bestdx = dx; 00693 bestdy = dy; 00694 bestflip = 0; 00695 } 00696 00697 if( a ) 00698 { 00699 delete a; 00700 a = 0; 00701 } 00702 if( uw ) 00703 { 00704 delete uw; 00705 uw = 0; 00706 } 00707 if( uwc ) 00708 { 00709 delete uwc; 00710 uwc = 0; 00711 } 00712 uw = this_img->unwrap(4, this_img->get_ysize() / 2 - 2 - maxshift, xst, (int)dx, (int)dy, true); 00713 uwc = uw->copy(); 00714 a = uw->calc_ccfx(to_flip_unwrapped); 00715 00716 uwc->rotate_x(a->calc_max_index()); 00717 cm = uwc->cmp(cmp_name, to_flip_unwrapped_copy, cmp_params); 00718 00719 if (cm < bestval) { 00720 bestval = cm; 00721 bestang = (float)(2.0 * M_PI * a->calc_max_index() / a->get_xsize()); 00722 bestdx = dx; 00723 bestdy = dy; 00724 bestflip = 1; 00725 } 00726 00727 if( a ) 00728 { 00729 delete a; 00730 a = 0; 00731 } 00732 if( uw ) 00733 { 00734 delete uw; 00735 uw = 0; 00736 } 00737 if( uwc ) 00738 { 00739 delete uwc; 00740 uwc = 0; 00741 } 00742 } 00743 } 00744 } 00745 if( to_unwrapped ) {delete to_unwrapped;to_unwrapped = 0;} 00746 if( to_shrunk_unwrapped ) { delete to_shrunk_unwrapped; to_shrunk_unwrapped = 0;} 00747 if (to_unwrapped_copy) { delete to_unwrapped_copy; to_unwrapped_copy = 0; } 00748 if (to_flip_unwrapped) { delete to_flip_unwrapped; to_flip_unwrapped = 0; } 00749 if (to_flip_unwrapped_copy) { delete to_flip_unwrapped_copy; to_flip_unwrapped_copy = 0;} 00750 00751 bestang *= (float)EMConsts::rad2deg; 00752 Transform t(Dict("type","2d","alpha",(float)bestang)); 00753 t.set_pre_trans(Vec2f(-bestdx,-bestdy)); 00754 if (bestflip) { 00755 t.set_mirror(true); 00756 } 00757 00758 EMData* ret = this_img->process("math.transform",Dict("transform",&t)); 00759 ret->set_attr("xform.align2d",&t); 00760 00761 return ret; 00762 }
| virtual EMData* EMAN::RTFExhaustiveAligner::align | ( | EMData * | this_img, | |
| EMData * | to_img | |||
| ) | const [inline, virtual] |
Implements EMAN::Aligner.
Definition at line 470 of file aligner.h.
References align().
00471 { 00472 return align(this_img, to_img, "sqeuclidean", Dict()); 00473 }
| virtual string EMAN::RTFExhaustiveAligner::get_name | ( | ) | const [inline, virtual] |
Get the Aligner's name.
Each Aligner is identified by a unique name.
Implements EMAN::Aligner.
Definition at line 475 of file aligner.h.
| virtual string EMAN::RTFExhaustiveAligner::get_desc | ( | ) | const [inline, virtual] |
Implements EMAN::Aligner.
Definition at line 480 of file aligner.h.
00481 { 00482 return "Experimental full 2D alignment with handedness check using semi-exhaustive search (not necessarily better than RTFBest)"; 00483 }
| static Aligner* EMAN::RTFExhaustiveAligner::NEW | ( | ) | [inline, static] |
| virtual TypeDict EMAN::RTFExhaustiveAligner::get_param_types | ( | ) | const [inline, virtual] |
Implements EMAN::Aligner.
Definition at line 490 of file aligner.h.
References EMAN::EMObject::EMDATA, EMAN::EMObject::INT, and EMAN::TypeDict::put().
00491 { 00492 TypeDict d; 00493 00494 d.put("flip", EMObject::EMDATA); 00495 d.put("maxshift", EMObject::INT, "Maximum translation in pixels"); 00496 return d; 00497 }
1.5.6