iOS和OpenCV:图像注册/对齐

yon*_*hen 2 opencv image registration alignment ios

我正在做一个在iOS中组合类似于HDR的多个图像的项目.我已经设法通过相机获得3张不同曝光的图像,现在我想对齐它们,因为在拍摄过程中,一只手必须摇动并导致所有3张图像的对齐略有不同.

我已经导入了OpenCV框架,我一直在探索OpenCV中的函数来对齐/注册图像,但一无所获.在OpenCV中实际上有一个功能来实现吗?如果没有,还有其他选择吗?

谢谢!

Sat*_*ick 6

在OpenCV 3.0中,您可以使用findTransformECC.我已经从LearnOpenCV.com复制了这个ECC图像对齐代码,其中解决了用于对齐颜色通道的非常类似的问题.该帖子还包含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);
Run Code Online (Sandbox Code Playgroud)