Hello,

Ive been working on learning opencv by example and have learned lots but have some confusions/confusion in some parts, and would appreciate if you guys could help me out..

First off I have 5 matrices, and I want to combine them into one big matrix..

for this reason I converted them to row matrices, using the code:

CvMat row_header, *row, *row2;
	row = cvReshape(mat, &row_header, 0, 1);
	row2 = cvReshape(mat2, &row_header, 0, 1);

I then want to combine these into a single matrix, and for that I used:

cv::Mat M1(2,50,CV_32FC1);
	
	M1.row(1) = mat + 0;
	M1.row(2) = mat2 + 0;

	IplImage imgc = M1;
	CvMat *M2;
	M2 = cvCreateMat(2,50,CV_32FC1 );
	cvConvert(&imgc, M2);
	
	cvSave( "combined.txt", M2 );

but this does not seem to work.. I tried different variations of the code and none of them work, wont even compile but with the code i wrote above it does compile but then gives some runtime errors, when I ignore that it continues and executes, but below is the saved matrix:

rows: 2
   cols: 10
   dt: f
   data: [ -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080. ]

it does not look anything like the original matrices :(

help please.. how to i properly combine the matrices?

Thank you

P.S. mat and mat2 are just 2 simple matrices having 5 rows and 10 colums each.. im pretty sure my matrix combination method is at fault

Update:

The strange runtime error is no more.. The problem was here:
I changed to M1.row(0) and M1.row(1), before it was giving error because I was telling the compiler that M1 has a third row as well with M1.row(2) and that is why i got the runtime error.

//now creating a combined matrix and copying these 2 into it
	//CvMat *M1;
	cv::Mat M1(2,row->width,CV_32FC1);
	//M1 = cvCreateMat(2,50,CV_32FC1 );
	
	//row.copyTo(M1.row(1));
	//row2.copyTo(M1.row(2));
	M1.row(0) = row + 0;
	M1.row(1) = row2 + 0;

	IplImage imgc = M1;
	//CvMat matc = M1;
	CvMat *M2;
	M2 = cvCreateMat(2,row->width,CV_32FC1 );
	cvConvert(&imgc, M2);
	//cvConvert(M1, M2 );
	cvSave( "combined.txt", M2 );

this fixed the strange runtime error I used to get, and the program executes without any problems.. but the matrix and combined.txt is still filled with junk values, here is the combined.txt after the above changes:

rows: 2
   cols: 50
   dt: f
   data: [ -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080., -431602080., -431602080., -431602080., -431602080.,
       -431602080. ]

just for reference.. when the files transferred to the combined matrix rows are exported they show actual values and not junk.. here is the exported txt file of the matrix "row":

rows: 1
   cols: 50
   dt: f
   data: [ 60., 89., 86., 102., 58., 51., 143., 187., 140., 64., 80.,
       169., 184., 172., 67., 90., 174., 191., 175., 41., 97., 86., 171.,
       104., 87., 145., 164., 176., 157., 119., 176., 177., 180., 179.,
       150., 44., 182., 148., 182., 38., 48., 197., 171., 171., 39., 48.,
       169., 163., 141., 39. ]

I checked my code again and could not find out as to why the combined matrix is filled with junk values.. help here would be appreciated

just wanted to update here for people having similar problems.. I fixed it with the following code:

CvMat *M1;
	//cv::Mat M1(2,row->width,CV_32FC1);
	M1 = cvCreateMat(5,row->width,CV_32FC1 );
	//row.copyTo(M1.row(0));
	//row2.copyTo(M1.row(2));
	//M1.row(0) = 0;
	//M1.row(0) = row + 0;
	//M1.row(1) = 0;
	//M1.row(1) = row2 + 0;
	cvSetZero(M1);

	/*for(int i=0;i<5;i++)
	{
		for(int j=0;j<row->width;j++)
		{
			CV_MAT_ELEM( *M1, float, i, j ) = CV_MAT_ELEM( *rowi, float, 0, i );*/

	for(int i=0;i<row->width;i++)
	{
		CV_MAT_ELEM( *M1, float, 0, i ) = CV_MAT_ELEM( *row, float, 0, i );
	}
	
	for(int j=0;j<row2->width;j++)
	{
		CV_MAT_ELEM( *M1, float, 1, j ) = CV_MAT_ELEM( *row2, float, 0, j );
	}
	for(int k=0;k<row2->width;k++)
	{
		CV_MAT_ELEM( *M1, float, 2, k ) = CV_MAT_ELEM( *row3, float, 0, k );
	}
	for(int l=0;l<row2->width;l++)
	{
		CV_MAT_ELEM( *M1, float, 3, l ) = CV_MAT_ELEM( *row3, float, 0, l );
	}
	for(int m=0;m<row2->width;m++)
	{
		CV_MAT_ELEM( *M1, float, 4, m ) = CV_MAT_ELEM( *row4, float, 0, m );
	}

the matrix M1 is a 5 row matrix having 50 columns.. each row represents a seperate matrix.. row, row2, row3, row4 and row5

This article has been dead for over six months. Start a new discussion instead.