我目前正在 Visual Studio 2010 上使用 OpenCV 2.3.1 进行图像拼接,但我遇到了一些问题。
问题描述 我正在尝试编写一个代码来拼接来自几个摄像头(大约 3~4 个)的多张图像,即代码应该继续执行图像拼接,直到我要求它停止。
以下是我到目前为止所做的: (为简化起见,我将仅用几句话替换部分代码)
1.Reading frames(images) from 2 cameras (Currently I'm just working on 2 cameras.)
2.Feature detection, descriptor calculation (SURF)
3.Feature matching using FlannBasedMatcher
4.Removing outliers and calculate the Homography with inliers using RANSAC.
5.Warp one of both images.
对于第 5 步,我遵循了以下线程中的答案,只是更改了一些参数: Stitching 2 images in opencv
然而,结果却很糟糕。 我刚刚将结果上传到 youtube 上,当然只有知道链接的人才能看到。
我的代码如下所示: (只显示关键部分)
VideoCapture cam1, cam2;
cam1.open(0);
cam2.open(1);
while(1)
{
Mat frm1, frm2;
cam1 >> frm1;
cam2 >> frm2;
//(SURF detection, descriptor calculation
//and matching using FlannBasedMatcher)
double max_dist = 0; double min_dist = 100;
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors_1.rows; i++ )
{
double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
(Draw only "good" matches
(i.e. whose distance is less than 3*min_dist ))
vector<Point2f> frame1;
vector<Point2f> frame2;
for( int i = 0; i < good_matches.size(); i++ )
{
//-- Get the keypoints from the good matches
frame1.push_back( keypoints_1[ good_matches[i].queryIdx ].pt );
frame2.push_back( keypoints_2[ good_matches[i].trainIdx ].pt );
}
Mat H = findHomography( Mat(frame1), Mat(frame2), CV_RANSAC );
cout << "Homography: " << H << endl;
/* warp the image */
Mat warpImage2;
warpPerspective(frm2, warpImage2,
H, Size(frm2.cols, frm2.rows), INTER_CUBIC);
Mat final(Size(frm2.cols*3 + frm1.cols, frm2.rows),CV_8UC3);
Mat roi1(final, Rect(frm1.cols, 0, frm1.cols, frm1.rows));
Mat roi2(final, Rect(2*frm1.cols, 0, frm2.cols, frm2.rows));
warpImage2.copyTo(roi2);
frm1.copyTo(roi1);
imshow("final", final);
我还应该怎么做才能使拼接效果更好?
此外,将Homography矩阵固定而不是一直计算它是否合理? 我的意思是自己指定2个相机之间的角度和位移,从而推导出一个满足我想要的Homography矩阵。
谢谢。 :)
最佳答案
听起来你的做法很明智,但如果你可以访问两个摄像头,并且它们将相对于彼此保持静止,那么离线校准,然后简单地在线应用转换将使你的应用程序更高效。
需要注意的一点是,您说您正在使用 findHomography来自 OpenCV 的函数。从文档中,这个函数:
Finds a perspective transformation between two planes.
但是,您的点并不局限于特定平面,因为它们正在对 3D 场景进行成像。如果你想离线校准,你可以用两个摄像头对一个棋盘进行成像,检测到的角点可以用在这个函数中。
或者,您可能想研究基本矩阵,它可以用 similar function 计算.这个矩阵描述了相机的相对位置,但需要一些工作(和一本好的教科书)来提取它们。
如果你能找到它,我强烈建议你看一下第二部分:“计算机视觉中的多 View 几何”一书中的“双 View 几何”,作者是 Richard Hartley 和 Andrew Zisserman,它经历了这个过程详细点。
关于opencv - 在使用 OpenCV 进行图像拼接时遇到一些困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10730794/