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