标签:cvmat opencv
程序:
代码:
#include "cv.h" #include "cxcore.h" #include "highgui.h" #include <iostream> float MatSum(CvMat* mat) //读取CvMat的恰当方法 { float sum=0; for(int rows=0;rows<mat->rows;rows++) { const float* data=(const float*)(mat->data.ptr+rows*mat->step); //因为行数据元素使用字节来计算的,所以指针最好用字节计算,然后分配恰当的类型,比如浮点型;每次都进行初始化因为比如有ROI区域那么可能不是连续存储的 for(int cols=0;cols<mat->cols;cols++) { sum+=*data++; } } return sum; } float MatSum2(CvMat* mat) //读取CvMat的简单方法:CV_MAT_ELEM { float sum=0; for(int rows=0;rows<mat->rows;rows++) { for(int cols=0;cols<mat->cols;cols++) { CV_MAT_ELEM(*mat,float,rows,cols)+=1; //修改CvMat元素 int x=CV_MAT_ELEM(*mat,float,rows,cols); sum+=x; std::cout<<"rows:"<<rows<<"cols:"<<cols<<"x:"<<x<<std::endl; } } return sum; } int Mat(int argc,char** argv) { CvMat mat; float val[]={1,2,3, 4,5,6, 7,8,9 }; cvInitMatHeader(&mat,3,3,CV_32FC1,val); //初始化CvMat float sum1=MatSum(&mat); float sum2=MatSum2(&mat); std::cout<<"sum1:"<<sum1<<std::endl; std::cout<<"sum2:"<<sum2<<std::endl; return 0; }
本文出自 “flyclc” 博客,请务必保留此出处http://flyclc.blog.51cto.com/1385758/1539985
CvMat的初始化,读取和修改,布布扣,bubuko.com
标签:cvmat opencv
原文地址:http://flyclc.blog.51cto.com/1385758/1539985