# reshape matrix

I can achieve the following:

with the reshape command quite easy:

Mat a = Mat(5,1, CV_32FC3); //a is 5x1, 3 channels
Mat b = a.reshape(1); //b is 5x3, 1 channel


I was also trying with .reshape but I couldn't manage it. (bear with me but my brain is quite fried at the moment)

Update:

I have the following image where its channel is in one big image:

so what I want to do is take each part of the image (we know the dimensions) and put it in a different channel:

in order to get the final image:

I could do it with many ways but I thought that there should be a simple way to do it like using .reshape or something similar. Therefore, I tried with the .reshape and what @LorenaGdL suggested or .reshape(3, image.rows) but I do not get the result I want grrrrrr, actually I am not sure if I can do that with the reshape. (by the way at the end I will be seeing nightmares with pink panthers.....:-p )

edit retag close merge delete

reshape(3) should do the trick. btw, it's friday, stop working ;)

( 2016-01-08 13:07:13 -0500 )edit

that's correct that was what I was trying. But check my update to see what I want to achieve.

( 2016-01-08 13:27:20 -0500 )edit
1

Yep, it doesn't work because each consecutive column goes to a different channel. This is what you want:

Mat image = imread("panther.png", 0);
int num_subimages = 3;                  //number of subimages
int width = image.cols/num_subimages;   //width of the final image
vector<Mat>subimages;
for (int i = 0; i <= image.cols - width; i += width){
Mat subimage = image(Rect(i, 0, width, image.rows));
subimages.push_back(subimage.clone());
}
Mat result;
merge(subimages, result);
imshow("Result", result);   //enjoy your colored panther

( 2016-01-08 13:58:58 -0500 )edit
1

@LorenaGdL thanks that indeed would solve my problem. But as I said would be able to achieve the same in a neater way. By the way make your comment an answer in order to accept it ;-).

( 2016-01-08 14:06:49 -0500 )edit