Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5848015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:48:02+00:00 2026-05-22T12:48:02+00:00

could someone pls explain to me how the row alignment of OpenCV’s CvMat (or

  • 0

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 matrix

CvMat *cvmat = cvCreateMat(2,3,CV_8UC1);
cvSet2D( cvmat, 0, 0, cvScalar(1) );
cvSet2D( cvmat, 0, 1, cvScalar(2) );
cvSet2D( cvmat, 0, 2, cvScalar(3) );
cvSet2D( cvmat, 1, 0, cvScalar(4) );
cvSet2D( cvmat, 1, 1, cvScalar(5) );
cvSet2D( cvmat, 1, 2, cvScalar(6) );

According to the documentation of CvMat, rows should be aligned by 4 bytes, i.e. first row of the matrix should be padded by one zero and the second should start at the offset +4). However, if I debug this piece of code, the data are continuous (i.e. cvmat->data is [1,2,3,4,5,6]) and I don’t see any 4-byte alignment. Is this a bug in documentation and is it always safe to assume the continuity of the data of CvMat or cv::Mat (in the case that the matrix is not part of another ofc)? Or are there some special configurations in which there could be some gaps in the data as a result of memory alignment?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:48:02+00:00Added an answer on May 22, 2026 at 12:48 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please explain why this is happening? var y = new int[]{1,2}; Console.WriteLine(y
Could someone please explain this weird phenomenon: http://jsfiddle.net/sPA2V/ For some reason, the box footer
Could someone please explain what this code exactly means: for i,j in [(i,j) for
Could someone explain why I get 0, instead of 1 ?? - (id)outlineView:(NSOutlineView *)outlineView
Could someone explain differences and relations between Python/PHP, cakePHP and wordpress? Maybe draw some
Could someone please explain to me how to draw a string using UIStringDrawing instead
Could someone please explain to me what an un/managed DLL is? And what is
Could someone explain the meaning of the following portion of code : private event
Could someone please explain to me why recursive-descent parsers can't work with a grammar
Could someone please explain to me why I'm getting an ActionView::TemplateError when I try

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.