ResultAlign2D get_complete_alignment_no_preprocessing( const cv::Mat &input, const cv::Mat &INPUT, const cv::Mat &POLAR1, cv::Mat &m_to_align, const cv::Mat &POLAR2, bool apply) { IMP_LOG_TERSE("starting complete 2D alignment with no preprocessing" << std::endl); cv::Mat aux1, aux2, aux3, aux4; // auxiliary matrices cv::Mat AUX1, AUX2, AUX3; // ffts algebra::Transformation2D transformation1, transformation2; double angle1 = 0, angle2 = 0; ResultAlign2D RA = get_rotational_alignment_no_preprocessing(POLAR1, POLAR2); angle1 = RA.first.get_rotation().get_angle(); get_transformed(m_to_align, aux1, RA.first); // rotate get_fft_using_optimal_size(aux1, AUX1); RA = get_translational_alignment_no_preprocessing(INPUT, AUX1); algebra::Vector2D shift1 = RA.first.get_translation(); transformation1.set_rotation(angle1); transformation1.set_translation(shift1); get_transformed(m_to_align, aux2, transformation1); // rotate double ccc1 = get_cross_correlation_coefficient(input, aux2); // Check the opposed angle if (angle1 < PI) { angle2 = angle1 + PI; } else { angle2 = angle1 - PI; } algebra::Rotation2D R2(angle2); algebra::Transformation2D tr(R2); get_transformed(m_to_align, aux3, tr); // rotate get_fft_using_optimal_size(aux3, AUX3); RA = get_translational_alignment_no_preprocessing(INPUT, AUX3); algebra::Vector2D shift2 = RA.first.get_translation(); transformation2.set_rotation(angle2); transformation2.set_translation(shift2); get_transformed(m_to_align, aux3, transformation2); double ccc2 = get_cross_correlation_coefficient(input, aux3); if (ccc2 > ccc1) { if (apply) { aux3.copyTo(m_to_align); } IMP_LOG_VERBOSE(" Align2D complete Transformation= " << transformation2 << " cross_correlation = " << ccc2 << std::endl); return ResultAlign2D(transformation2, ccc2); } else { if (apply) { aux3.copyTo(m_to_align); } IMP_LOG_VERBOSE(" Align2D complete Transformation= " << transformation1 << " cross_correlation = " << ccc1 << std::endl); return ResultAlign2D(transformation1, ccc1); } }
IMPEM2D_BEGIN_NAMESPACE ResultAlign2D get_complete_alignment(const cv::Mat &input, cv::Mat &m_to_align, bool apply) { IMP_LOG_TERSE("starting complete 2D alignment " << std::endl); cv::Mat autoc1, autoc2, aux1, aux2, aux3; algebra::Transformation2D transformation1, transformation2; ResultAlign2D RA; get_autocorrelation2d(input, autoc1); get_autocorrelation2d(m_to_align, autoc2); RA = get_rotational_alignment(autoc1, autoc2, false); double angle1 = RA.first.get_rotation().get_angle(); get_transformed(m_to_align, aux1, RA.first); // rotate RA = get_translational_alignment(input, aux1); algebra::Vector2D shift1 = RA.first.get_translation(); transformation1.set_rotation(angle1); transformation1.set_translation(shift1); get_transformed(m_to_align, aux2, transformation1); double ccc1 = get_cross_correlation_coefficient(input, aux2); // Check for both angles that can be the solution double angle2; if (angle1 < PI) { angle2 = angle1 + PI; } else { angle2 = angle1 - PI; } // rotate algebra::Rotation2D R2(angle2); algebra::Transformation2D tr(R2); get_transformed(m_to_align, aux3, tr); RA = get_translational_alignment(input, aux3); algebra::Vector2D shift2 = RA.first.get_translation(); transformation2.set_rotation(angle2); transformation2.set_translation(shift2); get_transformed(m_to_align, aux3, transformation2); double ccc2 = get_cross_correlation_coefficient(input, aux3); if (ccc2 > ccc1) { if (apply) { aux3.copyTo(m_to_align); } IMP_LOG_VERBOSE(" Transformation= " << transformation2 << " cross_correlation = " << ccc2 << std::endl); return em2d::ResultAlign2D(transformation2, ccc2); } else { if (apply) { aux2.copyTo(m_to_align); } IMP_LOG_VERBOSE(" Transformation= " << transformation1 << " cross_correlation = " << ccc1 << std::endl); return em2d::ResultAlign2D(transformation1, ccc1); } }
void FitRestraint::resample() const { //TODO - first check that the bounding box of the particles //match the one of the sampled ones. //resample the map containing all non rigid body particles //this map has all of the non rigid body particles. if (not_part_of_rb_.size()>0) { none_rb_model_dens_map_->resample(); none_rb_model_dens_map_->calcRMS(); model_dens_map_->copy_map(none_rb_model_dens_map_); } else{ model_dens_map_->reset_data(0.); } for(unsigned int rb_i=0;rb_i<rbs_.size();rb_i++) { IMP_LOG(VERBOSE,"Rb model dens map size:"<< get_bounding_box(rb_model_dens_map_[rb_i],-1000.)<< "\n Target size:"<<get_bounding_box(target_dens_map_,-1000.)<<"\n"); algebra::Transformation3D rb_t= algebra::get_transformation_from_first_to_second( rbs_orig_rf_[rb_i], rbs_[rb_i].get_reference_frame()); Pointer<DensityMap> transformed = get_transformed( rb_model_dens_map_[rb_i], rb_t); IMP_LOG(VERBOSE,"transformed map size:"<< get_bounding_box(transformed,-1000.)<<std::endl); model_dens_map_->add(transformed); transformed->set_was_used(true); } }
float MapScorer::score(const IMP::algebra::Transformation3D& trans) const { float voxel_data_threshold = complex_map_.get_header()->dmin; IMP::em::DensityMap* transformed_complex_map = get_transformed(trans); float cc = IMP::em::CoarseCC::cross_correlation_coefficient( &complex_map_, transformed_complex_map, voxel_data_threshold); return cc; }
ResultAlign2D get_rotational_alignment(const cv::Mat &input, cv::Mat &m_to_align,bool apply) { IMP_LOG_TERSE( "starting 2D rotational alignment" << std::endl); IMP_USAGE_CHECK((input.rows==m_to_align.rows) && (input.cols==m_to_align.cols), "em2d::align_rotational: Matrices have different size."); cv::Mat polar1,polar2,corr; // Build maps for resampling PolarResamplingParameters polar_params(input.rows,input.cols); polar_params.set_estimated_number_of_angles(std::min(input.rows,input.cols)); polar_params.create_maps_for_resampling(); do_resample_polar(input,polar1,polar_params); // subject image do_resample_polar(m_to_align,polar2,polar_params); // projection image ResultAlign2D RA= get_translational_alignment(polar1,polar2); algebra::Vector2D shift=RA.first.get_translation(); // column shift[0] is the optimal angle to bring m_to_align to input double angle =shift[0]*polar_params.get_angle_step(); RA.first.set_rotation(angle); RA.first.set_translation(algebra::Vector2D(0.0,0.0)); // Apply the rotation if requested if(apply) { cv::Mat result; get_transformed(m_to_align,result,RA.first); result.copyTo(m_to_align); } IMP_LOG_VERBOSE("Rotational alingment: Transformation= " << RA.first << " cross_correlation = " << RA.second << std::endl); return RA; }
ResultAlign2D get_translational_alignment(const cv::Mat &input, cv::Mat &m_to_align, bool apply) { IMP_LOG_TERSE( "starting 2D translational alignment" << std::endl); IMP_USAGE_CHECK( (input.rows==m_to_align.rows) && (input.cols==m_to_align.cols), "em2d::align_translational: Matrices have different size."); cv::Mat corr; get_correlation2d(input,m_to_align,corr); double max_cc; algebra::Vector2D peak = internal::get_peak(corr,&max_cc); // Convert the pixel with the maximum to a shift respect to the center algebra::Vector2D shift(peak[0] - static_cast<double>(corr.cols)/2., peak[1] - static_cast<double>(corr.rows)/2.); algebra::Transformation2D t(shift); // Apply the shift if requested if(apply) { cv::Mat result; get_transformed(m_to_align,result,t); result.copyTo(m_to_align); } IMP_LOG_VERBOSE(" Transformation= " << t << " cross_correlation = " << max_cc << std::endl); return ResultAlign2D(t,max_cc); }
void FitRestraint::initialize_model_density_map( FloatKey weight_key) { //none_rb_model_dens_map_ will include all particles //that are not part of a rigid body none_rb_model_dens_map_ = new SampledDensityMap(*(target_dens_map_->get_header()),kt_); none_rb_model_dens_map_->set_name(get_name()+" scratch map"); none_rb_model_dens_map_->reset_data(0.0); if (use_rigid_bodies_) { for(core::RigidBodies::iterator it = rbs_.begin(); it != rbs_.end();it++) { core::RigidBody rb = *it; IMP_LOG(VERBOSE,"working on rigid body:"<< (*it)->get_name()<<std::endl); ParticlesTemp members=get_as<ParticlesTemp>(member_map_[*it]); //The rigid body may be outside of the density. This means //that the generated SampledDensityMap will be empty, //as it ignore particles outside of the boundaries. //To overcome that, we tranform the rb to the center of the //density map, resample in this transformation and then move //the rigid body back to its correct position. algebra::Vector3D rb_centroid = core::get_centroid(core::XYZs(members)); algebra::Transformation3D move2map_center( algebra::get_identity_rotation_3d(), target_dens_map_->get_centroid()-rb_centroid); core::transform(rb,move2map_center); rbs_orig_rf_.push_back(rb.get_reference_frame()); rb_model_dens_map_.push_back( new SampledDensityMap(*(target_dens_map_->get_header()),kt_)); rb_model_dens_map_.back()->set_was_used(true); rb_model_dens_map_.back()->set_name(get_name()+" internal rb map"); rb_model_dens_map_[rb_model_dens_map_.size()-1]-> set_particles(members,weight_key); rb_model_dens_map_[rb_model_dens_map_.size()-1]->resample(); rb_model_dens_map_[rb_model_dens_map_.size()-1]->calcRMS(); core::transform(rb,move2map_center.get_inverse()); } } //update the none rigid bodies map none_rb_model_dens_map_->set_particles(get_as<ParticlesTemp>(not_part_of_rb_), weight_key); if(not_part_of_rb_.size()>0){ none_rb_model_dens_map_->resample(); none_rb_model_dens_map_->calcRMS(); } //update the total model dens map if (not_part_of_rb_.size()>0) { model_dens_map_->copy_map(none_rb_model_dens_map_); } else{ model_dens_map_->reset_data(0.); } //add the rigid bodies maps for(unsigned int rb_i=0;rb_i<rbs_.size();rb_i++) { algebra::Transformation3D rb_t= algebra::get_transformation_from_first_to_second( rbs_orig_rf_[rb_i], rbs_[rb_i].get_reference_frame()); Pointer<DensityMap> transformed = get_transformed( rb_model_dens_map_[rb_i],rb_t); model_dens_map_->add(transformed); transformed->set_was_used(true); } }
void ProjectionFinder::get_coarse_registrations_for_subject( unsigned int i,RegistrationResults &coarse_RRs) { IMP_LOG_TERSE("ProjectionFinder: Coarse registration for subject " << i << std::endl); algebra::Transformation2D best_2d_transformation; double max_ccc=0.0; unsigned int projection_index = 0; coarse_RRs.resize(projections_.size()); for(unsigned long j=0;j<projections_.size();++j) { ResultAlign2D RA; // Method without preprocessing if(params_.coarse_registration_method == ALIGN2D_NO_PREPROCESSING) { RA=get_complete_alignment(subjects_[i]->get_data(), projections_[j]->get_data(),false); } // Methods with preprocessing and FFT alignment if(params_.coarse_registration_method == ALIGN2D_PREPROCESSING) { RA=get_complete_alignment_no_preprocessing(subjects_[i]->get_data(), SUBJECTS_[i], SUBJECTS_POLAR_AUTOC_[i], projections_[j]->get_data(), PROJECTIONS_POLAR_AUTOC_[j]); } // Method with centers of gravity alignment if(params_.coarse_registration_method == ALIGN2D_WITH_CENTERS) { RA = get_complete_alignment_with_centers_no_preprocessing( subjects_cog_[i], projections_cog_[j], SUBJECTS_POLAR_AUTOC_[i], PROJECTIONS_POLAR_AUTOC_[j]); // get_complete_alignment_with_centers_no_preprocessing returns a value of // Cross correlation from the rotational alignment but not the ccc. // compute the ccc here: cv::Mat aux; get_transformed(projections_[j]->get_data(),aux,RA.first); RA.second=get_cross_correlation_coefficient(subjects_[i]->get_data(),aux); } // Set result algebra::Vector2D shift(0.,0.); // Get values from the image algebra::Vector3D euler = projections_[j]->get_header().get_euler_angles(); algebra::Rotation3D R = algebra::get_rotation_from_fixed_zyz(euler[0], euler[1], euler[2]); RegistrationResult projection_result(R,shift,j,i); projection_result.set_ccc(RA.second); // The coarse registration is based on maximizing the // cross-correlation-coefficient, but any other score can be calculated // at this point. IMP_NEW(Image,aux,()); aux->set_was_used(true); get_transformed(projections_[j]->get_data(), aux->get_data(), RA.first); if(variances_.size() > 0) { score_function_->set_variance_image(variances_[i]); } double score = score_function_->get_score(subjects_[i], aux); projection_result.set_score(score); // add the 2D alignment transformation to the registration result // for the projection projection_result.add_in_plane_transformation(RA.first); // and store coarse_RRs[j]=projection_result; IMP_LOG_VERBOSE( "Coarse registration: " << coarse_RRs[j] << std::endl); if(RA.second>max_ccc) { max_ccc = RA.second; best_2d_transformation = RA.first; projection_index = j; } ///******/ // cv::Mat xx; // get_transformed(projections_[j]->get_data(),xx,RA.first); // std::ostringstream strmm; // strmm << "individual-" << i << "-" << j << ".spi"; // write_matrix(xx,strmm.str()); ///******/ } if(params_.save_match_images) { IMP_NEW(em2d::Image,match,()); get_transformed(projections_[projection_index]->get_data(), match->get_data(), best_2d_transformation); do_normalize(match,true); coarse_RRs[projection_index].set_in_image(match->get_header()); std::ostringstream strm; strm << "coarse_match-"; strm.fill('0'); strm.width(4); strm << i << ".spi"; IMP_NEW(em2d::SpiderImageReaderWriter, srw, ()); match->set_name(strm.str()); //// match->set_was_used(true); match->write(strm.str(),srw); }