• 【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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 输出:
    1
    0
    
    • 1
    • 2
  • 相关阅读:
    DEX 的现状和去中心化交易的未来
    Newsletter 2022-11|HStreamDB 0.11 发布
    15天深度复习JavaWeb的详细笔记(五)——JavaScript
    DTSE Tech Talk 第13期:Serverless凭什么被誉为未来云计算范式?
    CKA 真题练习(十六)备份还原etcd
    Java现在还好找工作吗?
    Ubuntu安装MATLAB
    Redis 的 RDB 和 AOF
    Git --》Git的安装与配置
    深入分析JVM执行引擎
  • 原文地址:https://blog.csdn.net/qq_42067550/article/details/126162421