【问题标题】:OpenCV findHomography IssueOpenCV findHomography 问题
【发布时间】:2011-12-26 17:46:17
【问题描述】:

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

到目前为止,我可以拍摄多张图像,并将它们拼接在一起,同时制作任何我喜欢的图像作为参考图像;这里有一点我的意思。

但是,如您所见 - 它有很多问题。我面临的主要问题是图像被剪切(re:最右边的图像,图像的顶部)。为了突出为什么会发生这种情况,我将绘制已匹配的点,并绘制转换结束位置的线:

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

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

所以我遇到的主要问题是,在改变了视角之后,图像:

遭受这样的损失:

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

我使用cv::SurfFeatureDetectorcv::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&lt; cv::Point2f &gt;

/* 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);

所以,我有扭曲图像将要结束的坐标,我有创建用于这些变换的齐次矩阵的点 - 但我不知道应该如何翻译这些图像所以他们不能被切断。非常感谢任何帮助或指示!

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

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

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

    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

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

    【讨论】:

    • 啊,我明白了——所以在我运行 cv::warpPerspective 之前我编辑了全息图中的值?现在太明显了!非常感谢!另外,我将研究图像拼接模块并将其添加为一个选项(也使用 GPU 和 CPU 来比较差异)——只是想学习 OpenCV。 :-)
    • 很高兴看到你的好成绩!
    【解决方案2】:

    找到矩阵后,您只需计算角的变换并收集变换点的最小和最大 x 和 y 值。

    一旦你有了这个边界框,只需将所有矩阵转换为(-xmin,-ymin) 并为结果分配一个(xmax-xmin) 宽和(ymax-ymin) 高的图像,然后将所有转换后的图像绘制到其中。

    使用这种方法,您将在缝合周围有黑色区域,但没有剪裁。

    自动查找包含在拼接中的最大矩形(以获得没有黑色区域和最小剪裁的完整合并图像)实现起来相当烦人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-01
      • 2016-01-31
      • 2014-08-28
      • 1970-01-01
      • 2014-10-30
      • 2012-01-21
      • 1970-01-01
      • 2012-07-11
      相关资源
      最近更新 更多