Ask Your Question

different step size output for cv::Mat::step1

asked 2012-11-07 07:42:27 -0600

manmedia gravatar image


I am trying to understand why row and column cv::Mat objects have different outputs for when step1() is invoked. For Example, if I declare matrices and do the following:

cv::Mat rowMatrix(cv::Mat::ones(1,4,CV_8UC1)); // 1x4 matrix
cv::Mat columnMatrix(cv::Mat::ones(4,1,CV_8UC1)); //4x1 matrix

// knowing beforehand that the matrices have continous data
rowMatrix.step1() // outputs 4
columnMatrix.step1() // outputs 1

I got this problem when I was iterating through elements of my row matrix and tried to do some manipulation operation. I got an exception which was not supposed to happen. But that is when I found the disparity between my expected and received value of rowMatrix.step1(). I wonder why it was like that. If I was to do this:

rowMatrix = cv::Mat(rowMatrix.t().col(0));

It reveals 1 which is what I wanted originally. Could anyone explain how this is working? Because the manual seems to be saying something different here

I will try and find the source code and walk through it! But in case anyone has better understanding of it, please suggest an explanation.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-11-07 08:33:56 -0600

SR gravatar image

updated 2012-11-12 09:00:33 -0600

step1() returns (width of a single matrix row in bytes) / (element size in bytes).


cv::Mat m(7, 2, CV_UC1);
  m.elemSize():  1
  m.elemSize1(): 1
  m.step1():     2

cv::Mat m(7, 2, CV_UC3);
  m.elemSize():  3
  m.elemSize1(): 1
  m.step1():     6

cv::Mat m(7, 2, CV_32FC1);
  m.elemSize():  4
  m.elemSize1(): 4
  m.step1():     2

cv::Mat m(7, 2, CV_32FC3);
  m.elemSize():  12
  m.elemSize1(): 4
  m.step1():     6
edit flag offensive delete link more


@SR I think it is more likely to be the number of columns rather than element size :). Because if your matrix has (444X1) Unsigned Integers. Surely it cannot be element size in bytes. It is the number of columns. Just found out! Thanks for your answers :)

manmedia gravatar imagemanmedia ( 2012-11-07 08:59:23 -0600 )edit

Nope, see my example.

SR gravatar imageSR ( 2012-11-08 07:56:06 -0600 )edit

@SR okay I got it now

manmedia gravatar imagemanmedia ( 2012-11-09 19:11:04 -0600 )edit

Then what is the difference between Mat::step and Mat::step1() ?

Ramkumar Natarajan gravatar imageRamkumar Natarajan ( 2016-05-31 17:20:43 -0600 )edit

Question Tools


Asked: 2012-11-07 07:42:27 -0600

Seen: 8,530 times

Last updated: Nov 12 '12