【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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值