- //function: the mat operation
- // data:12 /20
- #include <cv.h>
- #include <highgui.h>
- #include <iostream.h>
- void PrintMat(CvMat *mat)
- {
- int i;
- int j;
- for (i=0; i<mat->rows; i++)
- {
- // the type of mat_depth
- switch (CV_MAT_DEPTH(mat->type))
- {
- case CV_32F:
- case CV_64F:
- //printf("aa");
- for (j=0; j<mat->cols; j++)
- {
- printf("%5.2f", (float)cvGetReal2D(mat, i,j));
- //printf("%5.5f", (float)CV_MAT_ELEM(*mat, double, i,j));
- }
- break;
- case CV_8U:
- case CV_16U:
- for (j=0; j<mat->cols; j++)
- printf("%d", (int)cvGetReal2D(mat,i,j));
- break;
- }
- printf("/n");
- }
- }
- int main(int argc, char* argv[])
- {
- // create mat
- CvMat *myMat=cvCreateMat(2,2, CV_32FC1);
- float mata[] = {
- 1,2,3,
- 4,5,6,7,
- 8,9,10};
- float matb[] = {1,1,1,
- 1,1,1,
- 1,1,1};
- float matc[9];
- CvMat Mata;
- CvMat Matb;
- CvMat Matc;
- // initial mat header
- cvInitMatHeader(&Mata, 3,3, CV_32FC1, mata, CV_AUTOSTEP);
- cvInitMatHeader(&Matb, 3,3, CV_32FC1, matb, CV_AUTOSTEP);
- cvInitMatHeader( &Matc, 3, 3, CV_32FC1, matc, CV_AUTOSTEP );
- // print mat's element
- printf("print the mata:/n");
- PrintMat(&Mata);
- printf("print the matb:/n");
- PrintMat(&Matb);
- // Mat add function
- cvAdd(&Mata,&Matb, &Matc, NULL);
- //print the result
- printf("print the matc:/n");
- PrintMat(&Matc);
- int i;
- int j;
- // enter the numbers to the mat
- for ( i = 0; i<myMat->rows; i++ )
- for ( j = 0; j <myMat->cols; j++)
- {
- scanf("%f", &CV_MAT_ELEM(*myMat, float , i , j));
- //cin >> CV_MAT_ELEM(*myMat, float , i , j);
- }
- // print mat element
- PrintMat(myMat);
- printf("Hello World!/n");
- return 0;
- }