简体   繁体   English

如何在OpenCV函数中访问多维矩阵的子矩阵?

[英]How to access a sub-matrix of a multi-dimensional matrix in OpenCV function?

I have a multi-dimensional matrix A of size 100x100x100 and I want to get a sub-matrix of A, for example A[10:20, 20:30, 30:40] . 我有一个大小为100x100x100的多维矩阵A,我想获得A的子矩阵,例如A[10:20, 20:30, 30:40] When the original matrix has two dimensions, OpenCV has a Mat operator to access the sub-matrix , for example: A(Range(10,20), Range(20,30)) 当原始矩阵有两个维度时,OpenCV有一个Mat运算符来访问子矩阵,例如: A(Range(10,20), Range(20,30))

For a multi-dimensional matrix, is there any efficient way to do this access? 对于多维矩阵,有没有有效的方法来进行此访问? I am asking this because I need to copy the sub-matrix into another place. 我问这个是因为我需要将子矩阵复制到另一个地方。

Answer 1 答案1

If mat A is 3D 100 rows x 100 cols x 100 planes xn channels, you can use Mat Mat::operator()(const Range* ranges) const like this:: 如果mat A是3D 100行x 100 cols x 100 plane xn channels,你可以像这样使用Mat Mat :: operator()(const Range * range)const ::

std::vector<Range> ranges;
ranges.push_back(Range(10,20));
ranges.push_back(Range(20,30));
ranges.push_back(Range(30,40));

Mat B = A(&ranges[0]);

B will be 10x10x10 xn channels B将是10x10x10 xn通道


Answer 2 答案2

If however mat A is 100 rows x 100 cols x 100 channels, that is just a 100 channel 2D mat. 然而,如果垫子A是100行×100列×100个通道,那么这只是100通道的2D垫子。 You can do this: 你可以这样做:

Mat B = A(Range(10,20), Range(20,30));  // B will be 10x10x100

Now you need to select channels 30:40 from B, you would need to use void mixChannels(const Mat* src, size_t nsrcs, Mat* dst, size_t ndsts, const int* fromTo, size_t npairs) : 现在你需要从B中选择30:40的通道,你需要使用void mixChannels(const Mat * src,size_t nsrcs,Mat * dst,size_t ndsts,const int * fromTo,size_t npairs)

Mat C(10, 10, CV_MAKETYPE(A.depth(), 10));
int from_to[] = { 10,0, 11,1, 12,2, 13,3, 14,4,
                  15,5, 16,6, 17,7, 18,8, 19,9};
mixChannels(&B, 1, &C, 1, fromTo, 10);

C will then be 10 rows x 10 cols x 10 channels as required. 然后,根据需要,C将是10行×10×10×10个通道。 This is a bit messy but I don't know a better way. 这有点乱,但我不知道更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM