opencv求教 求矩阵中整数和
#include"stdio.h"#include"highgui.h"#include"cv.h"voidmain(){inta[]={1,2,3,4,5,6};int...
#include"stdio.h"
#include"highgui.h"
#include"cv.h"
void main()
{
int a[]={1,2,3,4,5,6};
int s;
CvMat* mat=cvCreateMatHeader(2,3,CV_64FC1);
cvInitMatHeader(mat,2,3,CV_8SC1,a);
int *p,row,col;
for(row=0;row<mat->rows;row++)
{
p=(int*)(mat->data.ptr+row*mat->step);
for(col=0;col<mat->cols;col++)
s+=*p++;
}
printf("%d",s);
} 展开
#include"highgui.h"
#include"cv.h"
void main()
{
int a[]={1,2,3,4,5,6};
int s;
CvMat* mat=cvCreateMatHeader(2,3,CV_64FC1);
cvInitMatHeader(mat,2,3,CV_8SC1,a);
int *p,row,col;
for(row=0;row<mat->rows;row++)
{
p=(int*)(mat->data.ptr+row*mat->step);
for(col=0;col<mat->cols;col++)
s+=*p++;
}
printf("%d",s);
} 展开
展开全部
//如果是求a的和,如下:
void main()
{
int a[]={1,2,3,4,5,6};
CvMat mat=cvMat(2, 3, CV_8UC1,a);//由数组创建mat
CvScalar sum;
cvScale(&mat,&mat,6,0);//先让每个数都乘以6::(1*6+2*6+3*6+4*6+5*6+6*6)
sum=cvAvg(&mat);//求平均,即(1*6+2*6+3*6+4*6+5*6+6*6)/6
printf("%d",sum.val[0]);
}
void main()
{
int a[]={1,2,3,4,5,6};
CvMat mat=cvMat(2, 3, CV_8UC1,a);//由数组创建mat
CvScalar sum;
cvScale(&mat,&mat,6,0);//先让每个数都乘以6::(1*6+2*6+3*6+4*6+5*6+6*6)
sum=cvAvg(&mat);//求平均,即(1*6+2*6+3*6+4*6+5*6+6*6)/6
printf("%d",sum.val[0]);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询