我目前的图像拼接工作使用的OpenCV 2.3.1在Visual Studio 2010中,但我遇到了一些麻烦。
问题描述我试图写拼接从几台摄像机(3〜4)衍生多个图像的代码,I,E,代码应该继续执行图像拼接,直到我要求它停止。
下面是我到目前为止已经完成:(为简便起见,我将替换短短几句话的代码的某些部分)
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步,我跟着以下螺纹的答案,只是改变了一些参数: 拼接OpenCV中2个图像
然而,结果是可怕的,但。 我刚刚上传结果到YouTube上,当然,只有那些谁拥有的链接就能看到它。
http://youtu.be/Oy5z_7LeaMk
我的代码如下所示:(只有关键部件显示)
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);
还有什么应该怎么做,使拼接好?
此外,它是合理的,使固定的,而不是保持其计算的单应矩阵? 我的意思是由我自己来指定2个摄像头之间的角度和位移,从而推导出单应矩阵满足我想要的。
谢谢。 :)
这听起来像你理智地去了解这一点,但如果你有机会到双方的相机,他们会保持静止相对于彼此,然后校准下线,并简单地应用转换在线将让你的应用更加高效。
要注意的一点是,你说你正在使用的findHomography来自OpenCV的功能。 从文档,这个功能:
Finds a perspective transformation between two planes.
然而,因为它们是成像3D场景的点并不限于特定的平面。 如果你想校准下线,你可以像使用这两款相机,并将检测到的角落棋盘可在此功能中使用。
或者,你可能想调查的基础矩阵,其可以用计算类似的功能 。 该矩阵描述摄像机的相对位置,但一些工作(和良好的教材)将需要提取它们。
如果你能找到它,我会强烈建议在看看第二部分:“双视图几何”的书“计算机视觉的多视图几何”,由理查德·哈特利和安德鲁·齐塞尔曼,它经过详细的过程。
我已经在图像配准近来工作。 我的算法将两幅图像,计算SURF特点,找到对应,找到对应性矩阵,然后缝合两个图像一起,我用下面的代码做到了:
void stich(Mat base, Mat target,Mat homography, Mat& panorama){
Mat corners1(1, 4,CV_32F);
Mat corners2(1,4,CV_32F);
Mat corners(1,4,CV_32F);
vector<Mat> planes;
/* compute corners
of warped image
*/
corners1.at<float>(0,0)=0;
corners2.at<float>(0,0)=0;
corners1.at<float>(0,1)=0;
corners2.at<float>(0,1)=target.rows;
corners1.at<float>(0,2)=target.cols;
corners2.at<float>(0,2)=0;
corners1.at<float>(0,3)=target.cols;
corners2.at<float>(0,3)=target.rows;
planes.push_back(corners1);
planes.push_back(corners2);
merge(planes,corners);
perspectiveTransform(corners, corners, homography);
/* compute size of resulting
image and allocate memory
*/
double x_start = min( min( (double)corners.at<Vec2f>(0,0)[0], (double)corners.at<Vec2f> (0,1)[0]),0.0);
double x_end = max( max( (double)corners.at<Vec2f>(0,2)[0], (double)corners.at<Vec2f>(0,3)[0]), (double)base.cols);
double y_start = min( min( (double)corners.at<Vec2f>(0,0)[1], (double)corners.at<Vec2f>(0,2)[1]), 0.0);
double y_end = max( max( (double)corners.at<Vec2f>(0,1)[1], (double)corners.at<Vec2f>(0,3)[1]), (double)base.rows);
/*Creating image
with same channels, depth
as target
and proper size
*/
panorama.create(Size(x_end - x_start + 1, y_end - y_start + 1), target.depth());
planes.clear();
/*Planes should
have same n.channels
as target
*/
for (int i=0;i<target.channels();i++){
planes.push_back(panorama);
}
merge(planes,panorama);
// create translation matrix in order to copy both images to correct places
Mat T;
T=Mat::zeros(3,3,CV_64F);
T.at<double>(0,0)=1;
T.at<double>(1,1)=1;
T.at<double>(2,2)=1;
T.at<double>(0,2)=-x_start;
T.at<double>(1,2)=-y_start;
// copy base image to correct position within output image
warpPerspective(base, panorama, T,panorama.size(),INTER_LINEAR| CV_WARP_FILL_OUTLIERS);
// change homography to take necessary translation into account
gemm(T, homography,1,T,0,T);
// warp second image and copy it to output image
warpPerspective(target,panorama, T, panorama.size(),INTER_LINEAR);
//tidy
corners.release();
T.release();
}
如有任何问题,我会尝试