Ask Your Question

Reshape function problem

asked 2013-10-07 01:12:38 -0600

updated 2013-10-07 05:23:06 -0600

When I use the reshape function with ROI then I encounter the following error.

Mat cur_img = imread(fileList.fileNames[i],0);
Mat res_img = cur_img(Rect(0,0,100,100)).reshape(1,1);

Error :Image step is wrong

I made a clone of ROI image then the error is resolved.

Mat cur_img = imread(fileList.fileNames[i],0);
Mat res_img = cur_img(Rect(0,0,100,100)).clone().reshape(1,1);

What is the reason?


I realize this topic that the reshape function in this step need to consecutive memory.My question is why the reshape function don't create new image when the Mat data is not consecutive memory and then it does not reshape it .This solution is better than facing the user with an error.

edit retag flag offensive close merge delete



rois don't have consecutive memory. you can't reshape them (without the clone())

berak gravatar imageberak ( 2013-10-07 03:50:24 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-10-07 04:20:21 -0600

updated 2013-10-07 04:25:20 -0600

it is the same as: (just for clarifying)

 Mat cur_img = imread(fileList.fileNames[i],0);
 Mat aux = cur_img(Rect(0,0,100,100));
 Mat res_img = aux.reshape(1,1);

When you do this operation:

 Mat aux =  image(Rect(0,0,100,100));

Your are calling this operator

  C++: Mat Mat::operator()(const Rect& roi) const

This operation makes a new header for the specified sub-array of *this, thus it is a 0(1) operation, that is, no matrix data is copied. So, matrix elements are no longer stored continuosly without gaps at the end of each row.

  Mat res_img = aux.reshape(1,1);

No data is copied. That is, this is an O(1) operation. Consequently, if you change the number of rows the matrix must be continuous

 bool myCheckMatContinuity(const Mat& m)
      //return (m.flags & Mat::CONTINUOUS_FLAG) != 0;
      return m.rows == 1 || m.step == m.cols*m.elemSize();

 Mat aux = image(Rect(0,0,100,100));
 bool cont= myCheckMatContinuity(aux); //false!
 Mat res_img = aux.reshape(1,1); //error
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2013-10-07 01:12:38 -0600

Seen: 3,731 times

Last updated: Oct 07 '13