【OpenCV4】cv::Mat.isContinuous() 函数判断内存是否连续(c++)

  • 官方文档说明:

Reports whether the matrix is continuous or not.

The method returns true if the matrix elements are stored continuously without gaps at the end of each row. Otherwise, it returns false. Obviously, 1x1 or 1xN matrices are always continuous. Matrices created with Mat::create are always continuous. But if you extract a part of the matrix using Mat::col, Mat::diag, and so on, or constructed a matrix header for externally allocated data, such matrices may no longer have this property.

The continuity flag is stored as a bit in the Mat::flags field and is computed automatically when you construct a matrix header.

  • 解析:

isContinuous() 方法可以判断一个 cv::Mat 对象是否在内存中是连续的。

如果是连续的返回 true,如果在每一行的结尾跳过一部分内存地址到达下一行,那么就会返回 false。

所以很显然,1x1 和 1xN 的对象一定是连续的,因为只有一行数据。

使用 cv::Mat::create 创建的对象也是连续的,表示直接开辟了一个连续的内存空间进行对象的创建。

但是,如果从一个 cv::Mat 对象中截取了一部分数据,或者构造数据来自外部存储的数据,那么就不一定是连续的了。

该只是为存储与 cv::Mat::flags 中,占据一位,构造 matrix 的时候会自动计算,所以获得这个标志位速度是很快的。

  • 案例:
    cv::Mat newM = cv::Mat::zeros(20, 20, CV_32FC1);

    cout << newM.isContinuous() << endl;

    cv::Mat segM = newM.colRange(10, 15);

    cout << segM.isContinuous() << endl;
  • 输出:
1
0

你可能感兴趣的:(计算机视觉,opencv)