设置和取得CvMat的值(简单方法)

#include "stdafx.h" #include "cv.h" #include "highgui.h" #include <iostream> using namespace std; void access_data_in_easy_way() //!!The two macros discussed in "The easy way" are suitable only for accessing one- and //!!two-dimensional arrays { //Example 3-5. Setting a single value in a matrix using the CV_MAT_ELEM_PTR() macro CvMat* mat = cvCreateMat( 5, 5, CV_32FC1 ); float element_3_2 = 7.7; *( (float*)CV_MAT_ELEM_PTR( *mat, 3, 2 ) ) = element_3_2; //Example 3-4. Accessing a matrix with the CV_MAT_ELEM() macro //CvMat* mat = cvCreateMat( 5, 5, CV_32FC1 ); element_3_2 = CV_MAT_ELEM( *mat, float, 3, 2 ); cout<<"Accessing a matrix with the CV_MAT_ELEM() macro"<<endl; cout<<"element(3,2): "<<element_3_2<<endl; } void access_data_in_hard_way() { //this is too complicated, so I might cover this later } // access_data_in_right_way() float sum( const CvMat* mat ) { float s = 0.0f; for(int row=0; row<mat->rows; row++ ) { const float* ptr = (const float*)(mat->data.ptr + row * mat->step); for( col=0; col<mat->cols; col++ ) { s += *ptr++; } } return( s ); }  

你可能感兴趣的:(Arrays,Access,include,float,Matrix,macros)