Exemplo n.º 1
0
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);
  }
}
Exemplo n.º 2
0
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);
  }
}
Exemplo n.º 3
0
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);
  }