Ask Your Question
0

Reshape a matrix

asked 2013-11-26 02:48:10 -0600

residentelvio gravatar image

updated 2013-11-26 02:51:48 -0600

berak gravatar image

I have an image and I need to reshape it as a 3D matrix. Does any function exist to do it? I need to have a 3d matrix by rows-by-columns-by channels to obtain 3 differents components thanks to the differents channels

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2013-11-26 05:16:02 -0600

updated 2013-11-26 05:20:07 -0600

If you have an image and you want to "reshape" it as a 3D matrix, I think that your image is a grayscale one. So if you want to transform your image to a 3D matrix, you have to options:

Convert your image from grayscale to BGR:

Mat color_dst;
cvtColor(gray, color_dst, CV_GRAY2BGR);

Convert your image from grayscale to BGR but with one channel information. So depending on what channel you put the information, you'll get a red, green or blue image:

Mat img, g, color;
img = imread("test.jpg",CV_LOAD_IMAGE_GRAYSCALE);
vector<Mat> channels;

g = Mat::zeros(Size(img.cols, img.rows), CV_8UC1);
imshow("grayscale", img);

channels.push_back(g); //blue information
channels.push_back(g); //green information
channels.push_back(img); //red information 

merge(channels, color);
imshow("color_red", color);
//imwrite("result_color_red.jpg",color); //red image
edit flag offensive delete link more
0

answered 2014-01-08 09:27:56 -0600

residentelvio gravatar image

what I have to do is this operation in the picture:

C:\fakepath\Immagine.jpg

My code:

Mat img = imread("/home/elvio/workspace/aws/src/andaina.bmp"); 

if(img.empty()){
  cout<<"The image is empty \n";
 return -1;
}

rows=img.rows;
cols= img.cols;
nChannels= img.channels();
numColComp = nChannels ; 

inCompImg = img.reshape(numColComp,rows*cols);

imgDecorr = colorDecorrelation(inCompImg,numColComp); //function colorDecorrelation -obtain: decorrelated image, same dimensions as the original one

 rows=imgDecorr.rows;
 cols=imgDecorr.cols;
 nChannels= imgDecorr.channels();

  imgDecorr.convertTo(rgbDecorrImg,CV_8UC3); //to convert the image in colorDecorrelation function 
    //Infact in colorDecorrelaion I have to convert because of gemm function need a float
    //img.convertTo(img_f,CV_32FC3);
    //img_f=img_f.reshape(1,img.rows*img.cols); // 1 channel

Now I have that problem. I have to do this operation in Matlab:

rgbDecorr= reshape(imgDecorr, r, c, numColComp);

RGBDECORR is the last operation to obtain this part of the work in the image. Thanks helping

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-11-26 02:48:10 -0600

Seen: 3,002 times

Last updated: Jan 08 '14