在opencv中拼接2张图片 [英] Stitching 2 images in opencv

查看:2324
本文介绍了在opencv中拼接2张图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拼图2张图片只是为了开始panography。我已经找到关键点,发现使用RANSAC的单应性,但我不知道如何对齐这两个图像(我是新的在opencv)现在部分代码

 矢量< Point2f> points1,points2; 
for(int i = 0; i {
// - 从好的匹配中获取关键点
points1.push_back(keypoints1 [good_matches [i] .queryIdx] .pt);
points2.push_back(keypoints2 [good_matches [i] .trainIdx]。 pt);
}

/ *查找Homography * /
Mat H = findHomography(Mat(points2),Mat(points1),CV_RANSAC);

/ *扭曲图像* /
warpPerspective(mImg2,warpImage2,H,Size(mImg2.cols * 2,mImg2.rows * 2),INTER_CUBIC);

并且我需要拼接 Mat mImg1 其中加载了第一个图像和 Mat warpImage2 其中是扭曲的第二个图像你能告诉我怎么做吗?我也有扭曲的图像切割,我知道我必须改变单应矩阵,但现在我需要以对准这两个图像。谢谢你的帮助。



编辑:使用Martin Beckett的帮助我添加了此代码

  /指向一个cv :: Mat头部(没有分配)
Mat final(Size(mImg2.cols * 2 + mImg1.cols,mImg2.rows * 2),CV_8UC3);

// velikost img1
Mat roi1(final,Rect(0,0,mImg1.cols,mImg1.rows));
Mat roi2(final,Rect(0,0,warpImage2.cols,warpImage2.rows));
warpImage2.copyTo(roi2);
mImg1.copyTo(roi1);
imshow(final,final);

方案

您创建了一个大小正确的新大图片,然后在您希望他们在最终图像中的位置中创建现有图像的大小的ROI ,并将现有图像复制到ROI。


I'm trying to stitch 2 images just for start for panography. I"ve already found keypoints, found homography using RANSAC but I can't figure out how to align these 2 images (i'm new at opencv). Now part of code

vector<Point2f> points1, points2;
for( int i = 0; i < good_matches.size(); i++ )
{
    //-- Get the keypoints from the good matches
    points1.push_back( keypoints1[ good_matches[i].queryIdx ].pt );
    points2.push_back( keypoints2[ good_matches[i].trainIdx ].pt );
}

/* Find Homography */
Mat H = findHomography( Mat(points2), Mat(points1), CV_RANSAC );

/* warp the image */
warpPerspective(mImg2, warpImage2, H, Size(mImg2.cols*2, mImg2.rows*2), INTER_CUBIC);

and I need to stitch Mat mImg1 where is loaded the first image and Mat warpImage2 where is the warped second image. Can you pls show me how to do that? I also have the warped image cut up and I know I have to change the homography matrix but for now I need to align these two images. Thank you for helping.

Edit: With Martin Beckett's help I added this code

//Point a cv::Mat header at it (no allocation is done)
Mat final(Size(mImg2.cols*2 + mImg1.cols, mImg2.rows*2),CV_8UC3);

//velikost img1
Mat roi1(final, Rect(0, 0,  mImg1.cols, mImg1.rows));
Mat roi2(final, Rect(0, 0, warpImage2.cols, warpImage2.rows));
warpImage2.copyTo(roi2);
mImg1.copyTo(roi1);
imshow("final", final);

and it's working now

解决方案

You create a new larger image of the correct combined size, then make ROIs of the size of the existing images in the positions you want them in the final image and copy the existing images to the ROIs.

这篇关于在opencv中拼接2张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆