Ask Your Question

Reshape function

asked 2013-06-19 11:04:08 -0600

residentelvio gravatar image

updated 2015-10-02 08:29:29 -0600

Hi I have to translate from MAtlav to C this lines:


I didn t understand well this function, but I tried with:

 x=img.reshape(numColComp,rows); //or x=img.reshape(0,rows)

I suppose I m doing it bad

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-06-19 12:08:11 -0600

Prasanna gravatar image

Hey there,

Lemme tell you one thing. Be it reshape in MATLAB or reshape in OpenCV or reshape anywhere, the only rule to keep in mind is the number of elements in img (= rows * cols * numChannels) must be the same before and after Reshaping
(i.e. x.rows * x.cols * x.channels() must be equal to img.rows * img.cols * img.channels() ).

No inclusion, no exclusion. If that's not possible then reshape cant be done.

It is readily available for 2D Mat with data in channels in OpenCV ( and i am afraid that it is not extended to n dimension Mat yet (though N-D Mat is supported in OpenCV)

And looks like you are doing it right (as long as you take care of that condition mentioned :) )

Hope this helps. Good Luck with your project.



edit flag offensive delete link more


ok so I have problems with the reshape function. before reshape:

              r=486, c=648, type=16,channels=3,type=16,depth=0

using newImg=img.reshape(numChannels,rows*cols);

to translate this code line from Matlab to C


and after printing newImg' s info I have :

                     r=314928, c=1, type=16,channels=3,type=16,depth=0
residentelvio gravatar imageresidentelvio ( 2013-06-20 08:25:37 -0600 )edit

Well, isnt that what you wanted? If you need the channels as columns then use img.reshape(1,rows * cols) => this will make your channels = 1, rows as rows * cols and cols as channels (I hope i am right)

Prasanna gravatar imagePrasanna ( 2013-06-20 11:50:09 -0600 )edit

Be careful, Matlabs reshape fills the data by filling the columns first, while OpenCVs reshape fills the rows first.

Elador gravatar imageElador ( 2014-09-22 05:57:03 -0600 )edit

Thanks Prasanna for this

Hemang gravatar imageHemang ( 2017-04-28 10:39:53 -0600 )edit

Question Tools

1 follower


Asked: 2013-06-19 11:04:08 -0600

Seen: 35,168 times

Last updated: Jun 19 '13