我正在开发OpenCV中的Panography/Panorama应用程序,我遇到了一个我真想不通的问题.有关全景照片的概念,请查看Panography Wikipedia文章:http://en.wikipedia.org/wiki/Panography
到目前为止,我可以拍摄多张图像,并将它们拼接在一起,同时制作任何我喜欢参考图像的图像; 这是我的意思.
但是,正如您所看到的 - 它有很多问题.我面临的主要问题是图像被切割(重新:最右边的图像,图像的顶部).为了突出显示这种情况的原因,我将绘制已匹配的点,并绘制转换结束位置的行:
左图像是参考图像,右图像是翻译后的图像(下面的原始图像) - 我绘制了绿线以突出显示图像.该图像具有以下角点:
TL: [234.759, -117.696]
TR: [852.226, -38.9487]
BR: [764.368, 374.84]
BL: [176.381, 259.953]
Run Code Online (Sandbox Code Playgroud)
所以我的主要问题是在透视图改变后的图像:
像这样遭受损失:
现在足够的图像,一些代码.
我正在使用a cv::SurfFeatureDetector
,cv::SurfDescriptorExtractor
并cv::FlannBasedMatcher
获得所有这些点,我通过执行以下操作来计算匹配和更重要的好匹配:
/* calculate the matches */
for(int i = 0; i < descriptors_thisImage.rows; i++) {
double dist = matches[i].distance;
if(dist < min_dist) min_dist = dist;
if(dist > max_dist) max_dist = dist;
}
/* calculate the good matches */
for(int i = 0; i < descriptors_thisImage.rows; i++) {
if(matches[i].distance < 3*min_dist) {
good_matches.push_back(matches[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
这是非常标准的,为此,我按照此处的教程进行了操作:http://opencv.itseez.com/trunk/doc/tutorials/features2d/feature_homography/feature_homography.html
要复制彼此的图像,我使用以下方法(在哪里img1
和img2
是std::vector< cv::Point2f >
)
/* set the keypoints from the good matches */
for( int i = 0; i < good_matches.size(); i++ ) {
img1.push_back( keypoints_thisImage[ good_matches[i].queryIdx ].pt );
img2.push_back( keypoints_referenceImage[ good_matches[i].trainIdx ].pt );
}
/* calculate the homography */
cv::Mat H = cv::findHomography(cv::Mat(img1), cv::Mat(img2), CV_RANSAC);
/* warp the image */
cv::warpPerspective(thisImage, thisTransformed, H, cv::Size(thisImage.cols * 2, thisImage.rows * 2), cv::INTER_CUBIC );
/* place the contents of thisImage in gsThisImage */
thisImage.copyTo(gsThisImage);
/* set the values of gsThisImage to 255 */
for(int i = 0; i < gsThisImage.rows; i++) {
cv::Vec3b *p = gsThisImage.ptr<cv::Vec3b>(i);
for(int j = 0; j < gsThisImage.cols; j++) {
for( int grb=0; grb < 3; grb++ ) {
p[j][grb] = cv::saturate_cast<uchar>( 255.0f );
}
}
}
/* convert the colour to greyscale */
cv::cvtColor(gsThisImage, gsThisImage, CV_BGR2GRAY);
/* warp the greyscale image to create an image mask */
cv::warpPerspective(gsThisImage, thisMask, H, cv::Size(thisImage.cols * 2, thisImage.rows * 2), cv::INTER_CUBIC );
/* stitch the transformed image to the reference image */
thisTransformed.copyTo(referenceImage, thisMask);
Run Code Online (Sandbox Code Playgroud)
所以,我有扭曲图像最终的坐标,我有点创建用于这些变换的均匀矩阵 - 但我无法弄清楚我应该如何翻译这些图像以便他们可以不会被切断.任何帮助或指示非常感谢!
首先,为什么不使用新添加的拼接模块?它确实是你想要做的事情.
其次,如果你想继续你的代码,要纠正它很容易.在单应矩阵中,平移表示最后一列的值.
a11 a12 a13 t1
a21 a22 a23 t2
a31 a32 a33 t3
a41 a42 a43 1
Run Code Online (Sandbox Code Playgroud)
(如果你有3x3矩阵,你会错过a13..a43列和a41..1行.a33将(应该)变为1).
所以,你要做的是弄清楚你应该把什么放在最后一列,以便你的图像对齐.
当你知道摄像机参数时,也检查这篇文章解释说明(不知何故相反的问题)如何建立单应性.它将帮助您了解矩阵值的作用.
并注意这一切,我告诉过你的最后一栏只是近似,因为在最后一列中的值实际上是翻译加上一些(未成年人)的因素.
归档时间: |
|
查看次数: |
15747 次 |
最近记录: |