/
main.cpp
93 lines (68 loc) · 2.2 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
Opencv example code: basic algebra operations in Opencv
Enrique Marin
88enrique@gmail.com
*/
#include <iostream>
#include <stdio.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
using namespace std;
using namespace cv;
int main(){
// Matrix initialization
float a[] = {3,2,5,6,5,8,2,3,4};
Mat A = Mat(3, 3, CV_32FC1, a);
cout << "A: " << endl << A << endl << endl;
float b[] = {7,4,3,5,3,2,1,2,9};
Mat B = Mat(3, 3, CV_32FC1, b);
cout << "B: " << endl << B << endl << endl;
Mat C;
// Vector initialization
float d[] = {2,4,1};
Mat D = Mat(3, 1, CV_32FC1, d);
cout << "D: " << endl << D << endl << endl;
float e[] = {5,2,9};
Mat E = Mat(3, 1, CV_32FC1, e);
cout << "E: " << endl << E << endl << endl;
Mat F;
// Matrix and vector multiplication
cout << "Matrix-vector multiplication: " << A*D << endl << endl;
// Matrix zeros
C = Mat::zeros(3,3,CV_32FC1);
cout << "Matrix Zeros: " << endl << C << endl << endl;
// Matrix ones
C = Mat::ones(3,3,CV_32FC1);
cout << "Matrix Ones: " << endl << C << endl << endl;
// Matrix identity
C = Mat::eye(3,3,CV_32FC1);
cout << "Matrix Identity: " << endl << C << endl << endl;
// Matrix addition
C = A + B;
cout << "Addition: " << endl << C << endl << endl;
// Matrix multiplication
C = A * B;
cout << "Multiplication: " << endl << C << endl << endl;
// Matrix multiplication per element
C = A.mul(B);
cout << "Multiplication 1 to 1: " << endl << C << endl << endl;
// Cross product
F = D.cross(E);
cout << "Cross product: " << endl << F << endl << endl;
// Dot product
F = D.dot(E);
cout << "Dot product: " << endl << F << endl << endl;
// Matrix inverse
C = A.inv();
cout << "Inverse: " << endl << C << endl << endl;
// Matrix transpose
C = A.t();
cout << "Transpose: " << endl << C << endl << endl;
// Matrix determinant
cout << "Determinant: " << endl << determinant(A) << endl << endl;
// Vector normalization
normalize(D, F);
cout << "Normalization: " << endl << F << endl << endl;
return 0;
}