-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_cvMat_demo.cpp
71 lines (51 loc) · 2.04 KB
/
3_cvMat_demo.cpp
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
63
64
65
66
67
68
69
70
71
#include "cv.h"
#include <stdio.h>
int main()
{
// Create an OpenCV Matrix containing some fixed data.
//
float vals[] = { 0.866025, -0.500000, 0.500000, 0.866025};
CvMat rotmat;
// cvInitMatHeader Method (mat, rows, cols, type, data, step)
cvInitMatHeader(
&rotmat, // mat
2, // rows
2, // cols
CV_32FC1, // type
vals // data
);
printf("Ex 3_3 matrix initialized\n");
// Matrix created (init all 0 ) and accessed [3,2]
// cvCreateMat(int rows, int cols, int type)
CvMat* mat = cvCreateMat( 5, 5, CV_32FC1 );
// CV_MAT_ELEM( matrix, elemtype, row, col )
float element_3_2 = CV_MAT_ELEM( *mat, float, 3, 2 );
printf("Exercise 3_4, matrix created and accessed [3,2]=%f\n",element_3_2);
// cvCreateMat(int rows, int cols, int type)
CvMat* matTwo = cvCreateMat( 5, 5, CV_32FC1 );
// ////////////////////////////////////////////////
// Good to Know But Should Not Use ( Not Efficient )
// ////////////////////////////////////////////////
// Different Method of Setting Value in cvMat:
// Method 1
float Element_3_2 = 7.7;
*( (float*)CV_MAT_ELEM_PTR( *matTwo, 3, 2 ) ) = Element_3_2;
// Different Method of Setting Value in cvMat:
// Method 2
cvmSet( matTwo, 2, 2, 0.5000 );
// Different Method of Setting Value in cvMat:
// Method 3
cvSetReal2D( matTwo, 3, 3, 0.3300 );
printf("Exercise 3_5, matrix created and accessed [3,2]=%f, [2,2]=%f, [3,3]=%f\n",CV_MAT_ELEM( *mat, float, 3, 2 ),CV_MAT_ELEM( *mat, float, 2, 2 ),CV_MAT_ELEM( *mat, float, 3, 3 ));
// ////////////////////////////////////////////////
// Access Mat Value (Sum Up All Val in cvMat)
// ////////////////////////////////////////////////
float s = 0.0f;
for( int row=0; row<mat->height; row++ ) {
float* ptr = mat->data.fl + row * mat->step/4;
for( int col=0; col<mat->width; col++ ) {
s += *ptr++;
}
}
printf("Sum of cvMat is %f\n", s);
}