2
我很難在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");
謝謝你,我明白我現在要去哪裏錯了! – Chris