且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在OpenCV中访问多维Mat的子矩阵

更新时间:2023-10-07 08:39:04

您可以使用rowRange,colRange甚至函数来轻松访问2d cv :: Mat的子矩阵

You can easily access sub-matrix of 2D cv::Mat using functions rowRange, colRange or even

cv::Mat subMat = originalMat(cv::Rect(x,y,width,height));

此外,您可以在矩阵构造函数中定义的矩阵中的通道数可以用作第三维(但我认为它限制为256或512).

Also, the number of channels in a matrix, that you can define in the matrix constructor, can be used as the third dimension (but it is limited to 256 or 512 i think).

还有模板化的cv :: Mat_类,您可以根据自己的目的进行调整

There is also the templated cv::Mat_ class that you can adapt to fit your purpose

我检查了二维矩阵的构造函数.运行它时,Mat的rows和cols字段设置为-1.实际的矩阵大小以int数组形式存储在Mat :: size中. 对于尺寸大于2的矩阵,不能使用cv :: Rect或rowRange/colRange的子矩阵构造函数.

I have checked the constructor for >2 dimensional matrices. When you run it the rows and cols field of Mat are set to -1. The actual matrix size is store in Mat::size as an array of int. For matrix of dimensions >2 you cannot use the submatrices constructors using a cv::Rect or rowRange/colRange.

恐怕您需要做一些工作来提取dim> 2的子矩阵,直接使用行数据.但是您可以使用存储在Mat :: step中的信息,该信息可以告诉您数组的布局. 官方文档中对此进行了解释.

I'm afraid you have to do a bit of work to extract submatrices for dim>2, working directly with the row data. But you can use the information stored in Mat::step which tells you the layout of the array. This is explained in the official documentation.