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); } }
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); }
ResultAlign2D get_complete_alignment_with_centers_no_preprocessing( const algebra::Vector2D ¢er1, const algebra::Vector2D ¢er2, const cv::Mat &AUTOC_POLAR1, const cv::Mat &AUTOC_POLAR2) { // Align rotationally with FFT ResultAlign2D RA = get_rotational_alignment_no_preprocessing(AUTOC_POLAR1, AUTOC_POLAR2); double angle = RA.first.get_rotation().get_angle(); if (angle < 0) { angle += 2 * PI; } // Compute translation using the centers algebra::Rotation2D R(angle); algebra::Vector2D displacement = center1 - R.get_rotated(center2); algebra::Transformation2D t(R, displacement); return ResultAlign2D(t, RA.second); }
ResultAlign2D get_translational_alignment_no_preprocessing(const cv::Mat &M1, const cv::Mat &M2) { IMP_LOG_TERSE("starting 2D translational alignment with no preprocessing" << std::endl); IMP_USAGE_CHECK(((M1.rows == M2.rows) && (M1.cols == M2.cols)), "get_translational_alignment_no_preprocessing: " "Matrices have different size."); cv::Mat corr; corr.create(M1.rows, M1.cols, CV_64FC1); get_correlation2d_no_preprocessing(M1, M2, corr); // corr must be allocated! // Find the peak of the cross_correlation 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); IMP_LOG_VERBOSE(" Translational Transformation = " << t << " cross_correlation = " << max_cc << std::endl); return ResultAlign2D(t, max_cc); }