c++ - OpenCV findHomography 问题

标签 c++ opencv

我正在 OpenCV 中开发全景图/全景图应用程序,但遇到了一个我真的无法弄清楚的问题。要了解全景照片的外观,请查看全景图维基百科文章:http://en.wikipedia.org/wiki/Panography

到目前为止,我可以拍摄多张图像,并将它们拼接在一起,同时制作我喜欢的任何图像作为引用图像;下面是我的意思的一点尝试。

An example Panography image I've created

但是,如您所见,它存在很多问题。我面临的主要问题是图像被剪切(关于:最右边的图像,图像的顶部)。为了强调发生这种情况的原因,我将绘制已匹配的点,并绘制转换结束位置的线:

The image matches

左图是引用图像,右图是翻译后的图像(下面是原图)——我画了绿线来突出显示图像。图像具有以下角点:

TL: [234.759, -117.696]
TR: [852.226, -38.9487]
BR: [764.368, 374.84]
BL: [176.381, 259.953]

所以我遇到的主要问题是透视图改变后:

Original Image

遭受这样的损失:

Cut up image

现在有足够的图像,一些代码。

我正在使用 cv::SurfFeatureDetector , cv::SurfDescriptorExtractorcv::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]);
    }
}

这是非常标准的,为此我遵循了此处的教程:http://opencv.itseez.com/trunk/doc/tutorials/features2d/feature_homography/feature_homography.html

要复制一张图片,我使用以下方法(其中 img1img2std::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);

因此,我知道了扭曲图像的最终位置的坐标,我知道了创建用于这些转换的齐次矩阵的点 - 但我不知道应该如何翻译这些图像所以他们不能被切断。非常感谢任何帮助或指点!

最佳答案

首先,为什么没有使用新添加的拼接模块?它做的正是你想做的事情。

其次,如果您想继续使用您的代码,更正它很容易。在单应矩阵中,翻译表示最后一列的值。

a11 a12 a13 t1
a21 a22 a23 t2
a31 a32 a33 t3
a41 a42 a43 1

(如果您有一个 3x3 矩阵,您将错过 a13..a43 列和 a41..1 行。a33 将(应该)变为 1)。

因此,您要做的是弄清楚您应该在最后一列中放置什么,以便您的图像对齐。

另请查看这篇文章,该文章解释了(不知何故相反的问题)当您知道相机参数时如何构建单应性。它将帮助您理解矩阵值的作用。

Opencv virtually camera rotating/translating for bird's eye view

请注意,我在上一栏中告诉您的所有内容都只是近似值,因为最后一栏中的值实际上是翻译加上一些(次要)因素。

关于c++ - OpenCV findHomography 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8089641/

相关文章:

c - 在 OpenCV 中显示一个非常简单的图像

python - 使用Python通过urllib下载图片并测试网站是否存在

c++ - SHGetFolderPath 返回带有问号的路径

c++ - 运行 winapi 程序时显示的控制台

c++ - 未找到 opencv .dll 文件

C++/CLI 将位图转换为 OpenCV Mat

python - 如何检测哪些圆圈被填充 + OpenCV + Python

c++ - 索引增量如何在 C++ 中工作

c++ - 一种类似 vector 但已排序的数据类型

c++ - 空字符行为 C++