将一个矩阵复制到另一个矩阵的子集
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码不适合我编译。我在
R0.copyTo(P0.colRange(0,2));
处收到错误(如果我尝试R0.copyTo(P0.colRange(0,3));< /code> 具有正确的范围。)但这对我来说确实有用:
您几乎在上一个代码示例中包含了它,但您遗漏了
copyTo
(并且您的范围不正确)。Your code doesn't compile for me. I get an error at
R0.copyTo(P0.colRange(0,2));
(and also if I tryR0.copyTo(P0.colRange(0,3));
which has the correct range.) But this does work for me:You almost had it in your last code example, but you left out
copyTo
(and your range was incorrect).