我有5个x 4cols cv :: Mat:
int output_size[] = {5,4};
cv::Mat im1(2, output_size, CV_32FC1);
float* ptr = (float*)im1.data;
for (unsigned int r = 0; r < output_size[0]; r++) {
for (unsigned int c = 0; c < output_size[1]; c++) {
*ptr = (r*im1.size.p[1])+c;
std::cout << *ptr++ << ",";
}
std::cout << std::endl;
}
所以矩阵看起来像这样:
[ 0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15,
16, 17, 18, 19]
此外,我还有另外3次x 5rows x 4cols cv :: Mat:
int output_size2[] = {3,5,4};
cv::Mat im2(3, output_size2, CV_32FC1);
im2 = 0;
现在我想将im1复制到第二层im2中。我做了以下事情:
cv::Range rngs[] = {cv::Range(1,2), cv::Range::all(), cv::Range::all()};
cv::Mat dst = im2(rngs);
im1.copyTo(dst);
这似乎不起作用。 im1.copyTo(dst)对im2没有影响 - 操作后所有第二层值都保持为零。经过一些反省后,似乎opencv发现由于dst的大小是1x5x4而不是5x4,它会重新分配dst。
将矩形矩阵复制到3D矩阵的一个层中的正确方法是什么?
好的,这有效:
void* ptr = im2.data + im2.step[0]*1;
memcpy(ptr, (void*)im1.data, im1.total()*sizeof(float));
但是有一种“opencv”解决方法。
答案 0 :(得分:1)
如果您有n个2-dim图像,并且想要将它们用作尺寸为n的第三维尺寸的3D矩阵中的图层,则可以使用cv::merge
。见documentation。
另见:
cv:split
documentation