opencv - 拼接2张以上图像

发布于 2025-01-05 03:56:47 字数 1385 浏览 1 评论 0原文

我现在正在尝试使用 opencv 在 C++ 中将两个以上的图像对齐在一起。问题是当我拼接超过 2 个时,之前的图像无法加载。

例如,imageContainer 现在包含三个图像。

第一张图片: 第一张图片

第二张图片: 第二张图片

第三张图片: Third Image

循环的第一次迭代:(组合第一张和第二张图像) First iteration

循环的第二次迭代:(组合第一次迭代和第三张图像的结果) Second iteration

可以看到,第二次迭代后,结果图像不包含该对象。 (最后一张图像的左侧全黑),

在 main.cpp

cv::Mat result = *imageContainer.begin();
for(vector<cv::Mat>::iterator itr = imageContainer.begin(); itr != imageContainer.end(); itr++){
    if(itr == imageContainer.begin())
        continue;
    result = applySURF(result, *itr);
}

中 SURF.cpp

cv::Mat applySURF(cv::Mat object, cv::Mat image){

/* More codes here but it won't affect solving the problem */

    cv::Mat result;
    cv::warpPerspective(image, result, transformationMat, cv::Size(object.cols + image.cols, image.rows));

    cv::Mat half(result, cv::Rect(0, 0, image.cols, image.rows));
    object.copyTo(half);

    imshow("Object", object);
    imshow("Result", result);
    cvWaitKey(0);

    return result;
}

我猜问题与感兴趣区域(ROI)有关。我该如何解决?

非常感谢。

I am now trying to align more than two images together in C++ with opencv. The problem is when I stitch more than 2, the previous image cannot be loaded.

For example, imageContainer now contains three images.

First Image:
First image

Second Image:
Second Image

Third Image:
Third Image

First iteration of the loop: (Combining the first and second image)
First iteration

Second iteration of the loop: (Combining the result from first iteration and third image)
Second iteration

You can see that after the second iteration, the result image does not contain the object. (Left side of the last image is all black),

In main.cpp

cv::Mat result = *imageContainer.begin();
for(vector<cv::Mat>::iterator itr = imageContainer.begin(); itr != imageContainer.end(); itr++){
    if(itr == imageContainer.begin())
        continue;
    result = applySURF(result, *itr);
}

In SURF.cpp

cv::Mat applySURF(cv::Mat object, cv::Mat image){

/* More codes here but it won't affect solving the problem */

    cv::Mat result;
    cv::warpPerspective(image, result, transformationMat, cv::Size(object.cols + image.cols, image.rows));

    cv::Mat half(result, cv::Rect(0, 0, image.cols, image.rows));
    object.copyTo(half);

    imshow("Object", object);
    imshow("Result", result);
    cvWaitKey(0);

    return result;
}

I guess the problem is related to Region Of Interest (ROI). How can I solve it?

Many Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断桥再见 2025-01-12 03:56:47

尝试以下代码:)

我测试了一些情况并得出结论,如果目标图像的大小与源图像的大小不同,它将重新分配一个新的Mat进行粘贴。在您的情况下,ROI 的大小与对象不同,它分配一个新的Mat half,并且与结果无关
不再了。因此,您的 copyTo 函数将对象复制到新的 Mat half 中,而不是结果 的 ROI 中。

cv::Mat applySURF(cv::Mat object, cv::Mat image){

/* More codes here but it won't affect solving the problem */

    cv::Mat result;
    cv::warpPerspective(image, result, transformationMat, cv::Size(object.cols + image.cols, image.rows));

    cv::Mat half(result, cv::Rect(0, 0, object.cols, object.rows));
    object.copyTo(half);

    cv::imshow("Object", object);
    cv::imshow("Result", result);
    cv::WaitKey(0);

    return result;
}

Try the following code:)

I tested some cases and got a conclusion that if the size of target image is not same as the source image, it will reallocate a new Mat to be pasted. In your case the size of ROI is not same as object, it allocates a new Mat half and it is not related to result
anymore. So your copyTo function copies the object into the new Mat half instead of the ROI of result.

cv::Mat applySURF(cv::Mat object, cv::Mat image){

/* More codes here but it won't affect solving the problem */

    cv::Mat result;
    cv::warpPerspective(image, result, transformationMat, cv::Size(object.cols + image.cols, image.rows));

    cv::Mat half(result, cv::Rect(0, 0, object.cols, object.rows));
    object.copyTo(half);

    cv::imshow("Object", object);
    cv::imshow("Result", result);
    cv::WaitKey(0);

    return result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文