我正在做一个在 iOS 中组合类似于 HDR 的多个图像的项目。我已经设法通过相机获得了 3 张不同曝光的图像,现在我想对齐它们,因为在拍摄过程中,一个人的手一定在抖动,导致所有 3 张图像的对齐方式略有不同。
我已经导入了 OpenCV 框架,我一直在探索 OpenCV 中的功能来对齐/配准图像,但一无所获。 OpenCV 中实际上有一个函数可以实现这一点吗?如果没有,还有其他选择吗?
谢谢!
最佳答案
在 OpenCV 3.0 中,您可以使用 findTransformECC。我已经复制了这个ECC Image Alignment来自 LearnOpenCV.com 的代码其中解决了一个非常相似的问题来对齐颜色 channel 。该帖子还包含 Python 代码。希望这可以帮助。
// Read the images to be aligned
Mat im1 = imread("images/image1.jpg");
Mat im2 = imread("images/image2.jpg");
// Convert images to gray scale;
Mat im1_gray, im2_gray;
cvtColor(im1, im1_gray, CV_BGR2GRAY);
cvtColor(im2, im2_gray, CV_BGR2GRAY);
// Define the motion model
const int warp_mode = MOTION_EUCLIDEAN;
// Set a 2x3 or 3x3 warp matrix depending on the motion model.
Mat warp_matrix;
// Initialize the matrix to identity
if ( warp_mode == MOTION_HOMOGRAPHY )
warp_matrix = Mat::eye(3, 3, CV_32F);
else
warp_matrix = Mat::eye(2, 3, CV_32F);
// Specify the number of iterations.
int number_of_iterations = 5000;
// Specify the threshold of the increment
// in the correlation coefficient between two iterations
double termination_eps = 1e-10;
// Define termination criteria
TermCriteria criteria (TermCriteria::COUNT+TermCriteria::EPS, number_of_iterations, termination_eps);
// Run the ECC algorithm. The results are stored in warp_matrix.
findTransformECC(
im1_gray,
im2_gray,
warp_matrix,
warp_mode,
criteria
);
// Storage for warped image.
Mat im2_aligned;
if (warp_mode != MOTION_HOMOGRAPHY)
// Use warpAffine for Translation, Euclidean and Affine
warpAffine(im2, im2_aligned, warp_matrix, im1.size(), INTER_LINEAR + WARP_INVERSE_MAP);
else
// Use warpPerspective for Homography
warpPerspective (im2, im2_aligned, warp_matrix, im1.size(),INTER_LINEAR + WARP_INVERSE_MAP);
// Show final result
imshow("Image 1", im1);
imshow("Image 2", im2);
imshow("Image 2 Aligned", im2_aligned);
waitKey(0);
关于iOS 和 OpenCV : Image Registration/Alignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20584527/