Sorry, this content is no longer available

Ask Your Question
1

Reshape function

asked Jun 19 '13

residentelvio gravatar image

updated Oct 2 '15

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

 x=reshape(img,r*c,numChannels);
 x=reshape(img,r,c,numChannels);
 x=reshape(img,r,c);
 x=reshape(img,r*c,scalesNumber);

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

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

I suppose I m doing it bad

Preview: (hide)

1 answer

Sort by » oldest newest most voted
3

answered Jun 19 '13

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 (http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-reshape) 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.

Regards,

Prasanna

Preview: (hide)

Comments

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

                      newImg=reshape(img,r*c,numChannels)

and after printing newImg' s info I have :

                     r=314928, c=1, type=16,channels=3,type=16,depth=0
residentelvio gravatar imageresidentelvio (Jun 20 '13)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 (Jun 20 '13)edit

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

Elador gravatar imageElador (Sep 22 '14)edit

Thanks Prasanna for this

Hemang gravatar imageHemang (Apr 28 '17)edit

Question Tools

1 follower

Stats

Asked: Jun 19 '13

Seen: 36,739 times

Last updated: Jun 19 '13