您的位置:首页 > 运维架构

opencv累加一个三通道矩阵的所有元素(结果绝对正确)

2013-05-04 20:13 405 查看
float sum(const CvMat* mat)
{
float s=0.0f;
for(int row=0;row<mat->rows;row++)
{
float* prt=(float*)(mat->data.ptr+row*mat->step);
for (int col=0;col<mat->cols;col++)
{
s+=prt[3*col]+prt[3*col+1]+prt[3*col+2];
}
}
return (s);
}

#include "stdafx.h"

#include <stdio.h>

#include "cv.h"

int _tmain(int argc, _TCHAR* argv[])

{

//累加一个三通道矩阵的所有元素
float sum( const CvMat* newmat );
CvMat *mat=cvCreateMat(3,4,CV_32FC3 );
cvZero(mat);//将矩阵置0
//为矩阵元素赋值

for(int i = 0; i < 3; i++)
for(int j = 0; j < 4; j++)
cvSet2D( mat, i, j, cvScalar(i*10,j*10,i*j*10) );
for(int i=0;i<3;i++)
for(int j=0;j<4;j++)
{
printf("%lf,%lf,%lf\t",cvGet2D( mat, i, j ).val[0],cvGet2D( mat, i, j ).val[1],cvGet2D( mat, i, j ).val[2]);
if ((i*4+j+1)%3==0)
printf("\n");
}
printf("\n");
double s = sum(mat);
printf("Sum of the 3 channel is %f\n",s);

getchar();
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  opencv
相关文章推荐