OpenCV's cv::Mat & CvMat row alignment

前端 未结 1 1915
滥情空心
滥情空心 2021-01-03 05:57

could someone pls explain to me how the row alignment of OpenCV\'s CvMat (or its C++ version cv::Mat) works? For instance, let\'s assume I have a m

相关标签:
1条回答
  • 2021-01-03 06:38

    It's not safe to assume the continuity. cv::Mat has a member function isContinuous that you can check for continuity (the C API has a macro for that, as the comment says):

    // returns true iff the matrix data is continuous
    // (i.e. when there are no gaps between successive rows).
    // similar to CV_IS_MAT_CONT(cvmat->type)
    bool isContinuous() const;
    

    There's also a member step that tells you the offset between consecutive rows:

    // a distance between successive rows in bytes; includes the gap if any
    size_t step;
    

    So, assuming you have an 8-bit single channel image, the pixel at (x, y) is at offset y * step + x.

    There's a number of situations where you end up with non-continuous memory, and they are not restricted to memory alignment. E.g., if you say cv::Mat r = mat.col(0), the data is not copied, but r points to the same memory region as mat, just with a different "header", so the "gap" that you have there is the data from the matrix that is not in column 0.

    0 讨论(0)
提交回复
热议问题