将一个矩阵复制到另一个矩阵的子集

发布于 2024-12-08 10:27:44 字数 1298 浏览 0 评论 0原文

我在使用 C++ 中的 OpenCV 将矩阵的子集复制到另一个更大的矩阵时遇到困难。

我尝试了以下代码:

#include <opencv2/opencv.hpp>

void printMatrix(const cv::Mat &M, std::string matrix)
{
    printf("Matrix \"%s\" is %i x %i\n", matrix.c_str(), M.rows, M.cols);
    std::cout << M << std::endl;
}

int main(int argc, char ** argv)
{
    cv::Mat P0(3, 4, CV_32F);
    printMatrix(P0, "P0 Initial");

    cv::Mat R0 = cv::Mat::eye(3,3,CV_32F);
    printMatrix(R0, "R0 I");

    R0.copyTo(P0.colRange(0,2));
    printMatrix(P0, "P0 with R");

    return 0;
}

产生以下输出:

Matrix "P0 Initial" is 3 x 4
[-4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008;
-4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008;
-4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008]

Matrix "R0 I" is 3 x 3
[1, 0, 0;
0, 1, 0;
0, 0, 1]

Matrix "P0 with R" is 3 x 4
[-4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008;
-4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008;
-4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008]

虽然表明复制操作没有执行任何操作。

我发现了类似的帖子

//R0.copyTo(P0.colRange(0,2));
cv::Mat dest = P0.colRange(0,2);
printMatrix(P0, "P0 with R");

I'm having difficulty copying a subset of a matrix into another larger matrix using OpenCV in C++.

I tried the following code:

#include <opencv2/opencv.hpp>

void printMatrix(const cv::Mat &M, std::string matrix)
{
    printf("Matrix \"%s\" is %i x %i\n", matrix.c_str(), M.rows, M.cols);
    std::cout << M << std::endl;
}

int main(int argc, char ** argv)
{
    cv::Mat P0(3, 4, CV_32F);
    printMatrix(P0, "P0 Initial");

    cv::Mat R0 = cv::Mat::eye(3,3,CV_32F);
    printMatrix(R0, "R0 I");

    R0.copyTo(P0.colRange(0,2));
    printMatrix(P0, "P0 with R");

    return 0;
}

Which produces the following output:

Matrix "P0 Initial" is 3 x 4
[-4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008;
-4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008;
-4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008]

Matrix "R0 I" is 3 x 3
[1, 0, 0;
0, 1, 0;
0, 0, 1]

Matrix "P0 with R" is 3 x 4
[-4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008;
-4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008;
-4.3160208e+008, -4.3160208e+008, -4.3160208e+008, -4.3160208e+008]

While suggests that the copy operation is not doing anything.

I found a similar post here but updating the relevant line to the following as suggested in that post still produces the same output.

//R0.copyTo(P0.colRange(0,2));
cv::Mat dest = P0.colRange(0,2);
printMatrix(P0, "P0 with R");

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

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

发布评论

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

评论(1

苏辞 2024-12-15 10:27:44

你的代码不适合我编译。我在 R0.copyTo(P0.colRange(0,2)); 处收到错误(如果我尝试 R0.copyTo(P0.colRange(0,3));< /code> 具有正确的范围。)但这对我来说确实有用:

    cv::Mat dest(P0.colRange(0,3));
    R0.copyTo(dest);
    printMatrix(P0, "P0 with R");

您几乎在上一个代码示例中包含了它,但您遗漏了 copyTo (并且您的范围不正确)。

Your code doesn't compile for me. I get an error at R0.copyTo(P0.colRange(0,2)); (and also if I try R0.copyTo(P0.colRange(0,3)); which has the correct range.) But this does work for me:

    cv::Mat dest(P0.colRange(0,3));
    R0.copyTo(dest);
    printMatrix(P0, "P0 with R");

You almost had it in your last code example, but you left out copyTo (and your range was incorrect).

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