EMAN2
Public Member Functions | Static Public Member Functions | Static Public Attributes
EMAN::FRM2DAligner Class Reference

#include <aligner.h>

Inheritance diagram for EMAN::FRM2DAligner:
Inheritance graph
[legend]
Collaboration diagram for EMAN::FRM2DAligner:
Collaboration graph
[legend]

List of all members.

Public Member Functions

virtual EMDataalign (EMData *this_img, EMData *to_img, const string &cmp_name, const Dict &cmp_params=Dict()) const
 To align 'this_img' with another image passed in through its parameters.
virtual EMDataalign (EMData *this_img, EMData *to_img) const
string get_name () const
 Get the Aligner's name.
string get_desc () const
virtual TypeDict get_param_types () const

Static Public Member Functions

static AlignerNEW ()

Static Public Attributes

static const string NAME = "frm2d"

Detailed Description

Definition at line 1722 of file aligner.h.


Member Function Documentation

EMData * FRM2DAligner::align ( EMData this_img,
EMData to_img,
const string &  cmp_name,
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.

Parameters:
this_imgThe image to be compared.
to_img'this_img" is aligned with 'to_img'.
cmp_nameThe comparison method to compare the two images.
cmp_paramsThe parameter dictionary for comparison method.
Returns:
The aligned image.

Implements EMAN::Aligner.

Definition at line 3068 of file aligner.cpp.

References EMAN::Util::calc_best_fft_size(), EMAN::EMData::calc_center_of_mass(), EMAN::EMData::copy(), EMAN::EMData::do_fft(), EMAN::EMData::get_data(), EMAN::EMData::get_xsize(), EMAN::EMData::get_ysize(), ImageDimensionException, EMAN::EMUtil::is_same_size(), nx, EMAN::EMData::oneDfftPolar(), EMAN::EMData::set_complex(), EMAN::EMData::set_ri(), EMAN::EMData::set_size(), sqrt(), EMAN::EMData::translate(), and EMAN::EMData::unwrap_largerR().

Referenced by align().

{
        if (!this_img) {
                return 0;
        }
        if (to && !EMUtil::is_same_size(this_img, to))
                throw ImageDimensionException("Images must be the same size to perform translational alignment");

        int nx=this_img->get_xsize();
        int ny=this_img->get_ysize();
        int size =(int)floor(M_PI*ny/4.0);
        size =Util::calc_best_fft_size(size);//ming   bestfftsize(size);
        int MAXR=ny/2;
        //int MAXR=size;
        EMData *this_temp=this_img->copy(); // ming change avg to to
        FloatPoint com_test,com_test1;
        com_test=this_temp->calc_center_of_mass();//ming add
        float com_this_x=com_test[0];
        float com_this_y=com_test[1];
        delete this_temp;


        EMData *that_temp=to->copy();
        com_test1=that_temp->calc_center_of_mass();
        float com_with_x=com_test1[0];
        float com_with_y=com_test1[1];
        delete that_temp;

        EMData *avg_frm=to->copy();
        float dx,dy;
        //float dx=-(com_with_x-nx/2); //ming
        //float dy=-(com_with_y-ny/2); //ming
        //avg_frm->translate(dx,dy,0.0);
        EMData *withpcs=avg_frm->unwrap_largerR(0,MAXR,size,float(MAXR)); // ming, something wrong inside this subroutine
        //EMData *withpcs=avg_frm->unwrap(-1,-1,-1,0,0,1);
        EMData *withpcsfft=withpcs->oneDfftPolar(size, float(MAXR), float(MAXR));

        float *sampl_fft=withpcsfft->get_data(); //
        delete avg_frm;
        delete withpcs;

        int bw=size/2;
        unsigned long ind1=0, ind2=0, ind3=0, ind4=0, ind41=0;
        float pi2=2.0*M_PI, r2;

        EMData *data_in=new EMData;
        data_in->set_complex(true);
        data_in->set_ri(1);
        data_in->set_size(2*size,1,1);
        float * comp_in=data_in->get_data();

        int p_max=3;
        float *frm2dhhat=0;

        if( (frm2dhhat=(float *)malloc((p_max+1)*(size+2)*bw*size*2* sizeof(float)))==NULL){
                cout <<"Error in allocating memory 13. \n";
                exit(1);
        }
        //printf("p_max=%d\n",p_max);
        float *sb=0, *cb=0;             // sin(beta) and cos(beta) for get h_hat, 300>size
        if((sb=new float[size])==NULL||(cb=new float[size])==NULL) {
                cout <<"can't allocate more memory, terminating. \n";
                exit(1);
        }
        for(int i=0;i<size;++i) {        // beta sampling, to calculate beta' and r'
                float beta=i*M_PI/bw;
                sb[i]=sin(beta);
                cb[i]=cos(beta);
        }

        for(int p=0; p<=p_max; ++p){
                ind1=p*size*bw;
        float pp2=(float)(p*p);
                for(int n=0;n<bw;++n){         /* loop for n */
                ind2=ind1+n;
                for(int r=0;r<=MAXR;++r) {
                                ind3=(ind2+r*bw)*size;
                        float rr2=(float)(r*r);
                                float rp2=(float)(r*p);
                        for(int i=0;i<size;++i){                            // beta sampling, to get beta' and r'
                                r2=std::sqrt((float)(rr2+pp2-2.0*rp2*cb[i]));   // r2->r'
                                int r1=(int)floor(r2+0.5f);                        // for computing gn(r')
                                if(r1>MAXR){
                                        comp_in[2*i]=0.0f;
                                        comp_in[2*i+1]=0.0f;
                                }
                                else{
                                        float gn_r=sampl_fft[2*n+r1*(size+2)];           // real part of gn(r')
                                        float gn_i=sampl_fft[2*n+1+r1*(size+2)];           // imaginary part of gn(r')
                                                float sb2, cb2, cn, sn;
                                                if(n!=0){
                                                        if(r2 != 0.0){
                                                                sb2=r*sb[i]/r2;
                                                                cb2=(r*cb[i]-p)/r2;
                                                        }
                                                else{
                                                                sb2=0.0;
                                                                cb2=1.0;
                                                        }
                                                if(sb2>1.0) sb2= 1.0f;
                                                if(sb2<-1.0)sb2=-1.0f;
                                                if(cb2>1.0) cb2= 1.0f;
                                                if(cb2<-1.0)cb2=-1.0f;
                                                float beta2=atan2(sb2,cb2);
                                                if(beta2<0.0) beta2+=pi2;
                                                float nb2=n*beta2;
                                                cn=cos(nb2);
                                                        sn=sin(nb2);
                                                }
                                        else{
                                                        cn=1.0f; sn=0.0f;
                                                }
                                                comp_in[2*i]=cn*gn_r-sn*gn_i;
                                                comp_in[2*i+1]=-(cn*gn_i+sn*gn_r);
                                }
                        }
                        EMData *data_out;
                        data_out=data_in->do_fft();
                        float * comp_out=data_out->get_data();
                        for(int m=0;m<size;m++){                                     // store hat{h(n,r,p)}(m)
                                        ind4=(ind3+m)*2;
                                        ind41=ind4+1;
                                        frm2dhhat[ind4]=comp_out[2*m];
                                        frm2dhhat[ind41]=comp_out[2*m+1];
                                }
                        delete data_out;
                        }
                }
        }

        delete[] sb;
        delete[] cb;
        delete data_in;
        delete withpcsfft;

        float dot_frm0=0.0f, dot_frm1=0.0f;
        EMData *da_nFlip=0, *da_yFlip=0, *dr_frm=0;
        //dr_frm=this_img->copy();
        for (int iFlip=0;iFlip<=1;++iFlip){
                if (iFlip==0){dr_frm=this_img->copy();  da_nFlip=this_img->copy();}
                else {dr_frm=this_img->copy(); da_yFlip=this_img->copy();}
                if(iFlip==1) {com_this_x=nx-com_this_x; } //ming   // image mirror about Y axis, so y keeps the same

                dx=-(com_this_x-nx/2); //ming
                dy=-(com_this_y-ny/2); //ming
                dr_frm->translate(dx,dy,0.0); // this
                EMData *selfpcs = dr_frm->unwrap_largerR(0,MAXR,size, (float)MAXR);
                //EMData *selfpcs=dr_frm->unwrap(-1,-1,-1,0,0,1);
                EMData *selfpcsfft = selfpcs->oneDfftPolar(size, (float)MAXR, (float)MAXR);
                delete selfpcs;
                delete dr_frm;
                if(iFlip==0)
                        dot_frm0=frm_2d_Align(da_nFlip,to, frm2dhhat, selfpcsfft, p_max, size, com_this_x, com_this_y, com_with_x, com_with_y,cmp_name,cmp_params);
                else
                        dot_frm1=frm_2d_Align(da_yFlip,to, frm2dhhat, selfpcsfft, p_max, size, com_this_x, com_this_y, com_with_x, com_with_y,cmp_name,cmp_params);
                delete selfpcsfft;
        }

        delete[] frm2dhhat;
        if(dot_frm0 <=dot_frm1) {
#ifdef DEBUG
                printf("best_corre=%f, no flip\n",dot_frm0);
#endif
                delete da_yFlip;
                return da_nFlip;
        }
        else {
#ifdef DEBUG
                printf("best_corre=%f, flipped\n",dot_frm1);
#endif
                delete da_nFlip;
                return da_yFlip;
        }
}
virtual EMData* EMAN::FRM2DAligner::align ( EMData this_img,
EMData to_img 
) const [inline, virtual]

Implements EMAN::Aligner.

Definition at line 1728 of file aligner.h.

References align().

                                        {
                                                return align(this_img, to_img, "frc", Dict());
                                        }
string EMAN::FRM2DAligner::get_desc ( ) const [inline, virtual]

Implements EMAN::Aligner.

Definition at line 1738 of file aligner.h.

                                        {
                                                return "FRM2D uses two rotational parameters and one translational parameter";
                                        }
string EMAN::FRM2DAligner::get_name ( ) const [inline, virtual]

Get the Aligner's name.

Each Aligner is identified by a unique name.

Returns:
The Aligner's name.

Implements EMAN::Aligner.

Definition at line 1733 of file aligner.h.

References NAME.

                                        {
                                                return NAME;
                                        }
virtual TypeDict EMAN::FRM2DAligner::get_param_types ( ) const [inline, virtual]

Implements EMAN::Aligner.

Definition at line 1747 of file aligner.h.

References EMAN::EMObject::INT, and EMAN::TypeDict::put().

                                        {
                                                        TypeDict d;
                                                        d.put("maxshift", EMObject::INT,"Maximum translation in pixels in any direction. If the solution yields a shift beyond this value in any direction, then the refinement is judged a failure and the original alignment is used as the solution.");

                                                        //d.put("p_max", EMObject::FLOAT,"p_max is");
                                                        return d;
                                        }
static Aligner* EMAN::FRM2DAligner::NEW ( ) [inline, static]

Definition at line 1743 of file aligner.h.

                                        {
                                                return new FRM2DAligner();
                                        }

Member Data Documentation

const string FRM2DAligner::NAME = "frm2d" [static]

Definition at line 1756 of file aligner.h.

Referenced by get_name().


The documentation for this class was generated from the following files: