Matrix questions from opencv??

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:

1
2
3
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:

1
2
3
4
5
6
7
8
9
10
11
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:

1
2
3
4
5
6
7
8
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
To combine matrixes in d3d you just times them together, what are you specifically doing?

I am unformilur with CvMat
however maby i can help who knows.

The saved matrix, looks like null pointer memory locations, just a thought.

There are also many ways to combine matrices each way achieves different things, for example, in direct3d if you times the individuals rotation matricies(x,y,z rotation) together you'll get a matrix for the total rotation.
Last edited on
Almost seems as if he is combining 5 vectors into a 5x4 matrix to me...

| x11 x12 x13 x14 x15 |
| x21 x22 x23 x24 x25 |
| x31 x32 x33 x34 x35 |
| x41 x42 x43 x44 x45 |

What values are you plugging in for you 5 matrices?
Thanks for the replies guys.. here is the complete code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "stdafx.h"
#include "stdio.h"
#include "cv.h"
#include "highgui.h"
//using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    IplImage *test, *resize, *test2, *resize2;
	int key = 0;
	test = cvLoadImage("c:/s1/1.pgm", CV_LOAD_IMAGE_UNCHANGED);
	test2 = cvLoadImage("c:/s1/10.pgm", CV_LOAD_IMAGE_UNCHANGED);
	//CvSize size;
	//size = cvSize(125,125);
	resize = cvCreateImage(cvSize(5,10), test->depth, test->nChannels);
	resize2 = cvCreateImage(cvSize(5,10), test->depth, test->nChannels);

	cvResize(test, resize, CV_INTER_LINEAR);
	cvResize(test2, resize2, CV_INTER_LINEAR);

	//now converting this image to matrix
	CvMat *mat, *mat2;
	mat = cvCreateMat(resize->height,resize->width,CV_32FC1 );
	cvConvert(resize, mat );

	mat2 = cvCreateMat(resize2->height,resize2->width,CV_32FC1 );
	cvConvert(resize2, mat2 );
	/*newmat = cvCreateMat(resize->height,resize->width,CV_32FC3);
	cvMulTransposed(mat, newmat, 1);

	newimage = cvCreateImage(cvSize(32,240), test->depth, test->nChannels);
	cvConvert(newmat, newimage);*/
	//just to confirm that the conversion is ok
	//newmat = cvCreateImage(cvSize(320,240), test->depth, test->nChannels);
	//cvConvert(mat, newmat);
	
	//now converting to 1 row matrix
	CvMat row_header, *row, *row2;
	row = cvReshape(mat, &row_header, 0, 1);
	row2 = cvReshape(mat2, &row_header, 0, 1);
	//it is now a 1 row matrix.. but the image is not shown the same way.. why I do not know??

	//trying to save to txt file now
	cvSave( "first_image_row.txt", row );
	cvSave( "second_image_row.txt", row2 );

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

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


row and row2 are mat and mat2 converted to single row matrices.. for now they are only 2 matrices.. when this works ill extend it to 5
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 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":

1
2
3
4
5
6
7
8
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 );
	}
Topic archived. No new replies allowed.