gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:17:57 28 4
gpt4 key购买 nike

我在使用 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]

虽然表明复制操作没有做任何事情。

我找到了类似的帖子 here但按照该帖子中的建议将相关行更新为以下内容仍然会产生相同的输出。

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

最佳答案

您的代码无法为我编译。我在 R0.copyTo(P0.colRange(0,2)); (如果我尝试 R0.copyTo(P0.colRange(0,3)); 具有正确的范围。)但这对我有用:

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

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

关于c++ - 将矩阵复制到另一个矩阵的子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7661147/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com