- 操作系统:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 编程语言:C++11
算法描述
计算两幅图像之间的增强相关系数值 78
Enhanced Correlation Coefficient (ECC):增强相关系数是一种用于图像配准的技术,通过最大化两个图像之间的相关系数来找到最佳的仿射变换矩阵。ECC 相比传统的相关系数方法,具有更好的鲁棒性和准确性。
函数原型
double cv::computeECC
(InputArray templateImage,InputArray inputImage,InputArray inputMask = noArray()
)
参数
- 参数templateImage:模板图像(参考图像),通常是固定不变的。
- inputImage:输入图像(待对齐图像),需要通过仿射变换对齐到模板图像。
- inputMask:可选的掩码图像,用于指定哪些像素参与计算。默认值为 noArray(),表示没有掩码。
返回值
double:返回相关系数的最大值
代码示例
#include <iostream>
#include <opencv2/opencv.hpp>using namespace cv;
using namespace std;int main()
{// 读取两幅图像Mat src1 = imread( "/media/dingxin/data/study/OpenCV/sources/images/referrence.png", IMREAD_GRAYSCALE );Mat src2 = imread( "/media/dingxin/data/study/OpenCV/sources/images/target.png", IMREAD_GRAYSCALE );if ( src1.empty() || src2.empty() ){cerr << "Error: Could not read images." << endl;return -1;}// 预处理图像equalizeHist( src1, src1 );equalizeHist( src2, src2 );// 初始化仿射变换矩阵Mat warp_matrix = Mat::eye( 2, 3, CV_32F );// 设置终止条件TermCriteria criteria( TermCriteria::COUNT + TermCriteria::EPS, 5000, 1e-11 );// 计算 ECC 并找到最佳的仿射变换矩阵double correlation_coefficient = findTransformECC( src1, src2, warp_matrix, MOTION_AFFINE, criteria );// 检查是否成功if ( correlation_coefficient < 0 ){cerr << "Error: The algorithm stopped before its convergence. The correlation is going to be minimized. Images may be uncorrelated or non-overlapped." << endl;return -1;}// 输出结果cout << "Correlation Coefficient: " << correlation_coefficient << endl;cout << "Warp Matrix:\n" << warp_matrix << endl;// 应用仿射变换Mat aligned_image;warpAffine( src2, aligned_image, warp_matrix, src1.size() );// 显示结果imshow( "Reference Image", src1 );imshow( "Target Image", src2 );imshow( "Aligned Image", aligned_image );waitKey( 0 );return 0;
}