reshape matrix

asked 2016-01-08 13:00:13 -0500

theodore gravatar image

updated 2016-01-08 13:54:12 -0500

I can achieve the following:

image description

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

How about the opposite:

image description

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


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

image description

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

image description

in order to get the final image:

image description

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 flag offensive close merge delete


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

LorenaGdL gravatar imageLorenaGdL ( 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.

theodore gravatar imagetheodore ( 2016-01-08 13:27:20 -0500 )edit

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
for (int i = 0; i <= image.cols - width; i += width){
    Mat subimage = image(Rect(i, 0, width, image.rows));
Mat result;
merge(subimages, result);
imshow("Result", result);   //enjoy your colored panther
LorenaGdL gravatar imageLorenaGdL ( 2016-01-08 13:58:58 -0500 )edit

@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 ;-).

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